Objective c 替换NSMutableArray中的指定词典

Objective c 替换NSMutableArray中的指定词典,objective-c,cocoa,Objective C,Cocoa,我正在尝试替换可变数组中的字典。 第1步到第4步应该很好,但是第5步到第6步我遇到了一些问题。您能告诉我如何使该功能正常工作: - (void) updatePlist { // 1: String with plist path: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDom

我正在尝试替换可变数组中的字典。 第1步到第4步应该很好,但是第5步到第6步我遇到了一些问题。您能告诉我如何使该功能正常工作:

- (void) updatePlist {
     // 1: String with plist path:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory
             stringByAppendingPathComponent:@"Object.plist"];
    // 2: Create a mutable dictionary containing current object and write 
    // "Yes" to the "Favorite" string:
     NSMutableDictionary *mutDict = [NSMutableDictionary
      dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
     [mutDict setObject:@"Yes" forKey:@"Favorite"];
     // 3: Make a string containing current object name:
     NSString *nameString = [[detailsDataSource objectAtIndex:detailIndex]
                                 valueForKey:@"Name"];
    // 4: Make a mutable array containing all objects:          
     NSArray *allObjectsArray = [[NSArray alloc] initWithContentsOfFile:path];
     NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray];
    // 5: Search for the dictionary in tmpMutArr with "Name" value matching nameString:
     int *index;        
     for(int i=0;i<[tmpMutArr count];i++) 
     {
         if([[tmpMutArr  objectAtIndex:i] isKindOfClass:[NSDictionary class]])
         {
             NSMutableDictionary *tempDict = [tmpMutArr  objectAtIndex:i];
             if([[tempDict valueForKey:@"Name"] isEqualToString:[NSString
                           stringWithFormat:@"%@", nameString]])nameString];)
             {
                 index = i;
             }
         }
     }
    // 6: Replace the old dictionary with the new one and write array to plist:
     [tmpMutArr replaceObjectAtIndex:index withObject:
                 [NSDictionary dictionaryWithDictionary:mutDict]];
     allObjectsArray = nil;
     allObjectsArray = [[NSArray alloc] initWithArray:tmpMutArr];
     [allObjectsArray writeToFile:path atomically:YES];
     }
-(无效)更新列表{
//1:具有plist路径的字符串:
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*path=[documentsDirectory
stringByAppendingPathComponent:@“Object.plist”];
//2:创建包含当前对象的可变字典并写入
//“收藏夹”字符串的“是”:
NSMutableDictionary*mutDict=[NSMutableDictionary]
dictionaryWithDictionary:[DetailsDataSourceObjectAtIndex:detailIndex]];
[mutDict setObject:@“是”forKey:@“收藏”];
//3:生成包含当前对象名称的字符串:
NSString*nameString=[[detailsDataSource对象索引:detailIndex]
valueForKey:@“名称”];
//4:创建包含所有对象的可变数组:
NSArray*allObjectsArray=[[NSArray alloc]initWithContentsOfFile:path];
NSMUTABLEARRY*tmpMutArr=[NSMUTABLEARRY arrayWithArray:allObjectsArray];
//5:在tmpMutArr中搜索“Name”值与nameString匹配的字典:
int*索引;
对于(int i=0;i替换该行:

if([tempDict valueForKey:@"Name" == [NSString stringWithFormat:@"", nameString];)
这一行:

if([[tempDict valueForKey:@"Name"] isEqualToString: [NSString stringWithFormat:@"%@", nameString]])

因为这里使用的是字符串比较。

正如Martin R在评论中指出的那样,您正在尝试将一个int值保存到int指针变量中

int index; //instead of 'int *index;'
这种改变应该会奏效

如果您先编写了
int*index;
,然后编写了
index=&i;
,这本可以工作的,因为您正在以这种方式保存指向整数的指针。但这似乎不是您想要做的

第二个错误是由于您为此方法提供了int指针而不是整数:

[tmpMutArr replaceObjectAtIndex:index withObject:
              [NSDictionary dictionaryWithDictionary:mutDict]];

但将索引声明为整数已经解决了这两个错误。

if(int i=0;我知道这不是应该的,我使用了一些示例代码将其组合在一起,这就是为什么我需要一些帮助来纠正它,因为这已经超过了我的经验水平。“i”甚至没有声明。但我想我应该是某种“for”循环,是的。字面上用“for”
int*index
应该替换“if”e> 整数索引
[tmpMutArr replaceObjectAtIndex:index withObject:
              [NSDictionary dictionaryWithDictionary:mutDict]];