0

我正在尝试替换可变数组中的字典。第 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];
     }

编辑

现在的问题是:

 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;   // Here 1
         }
     }
 }
 // ---------------------------- v And here 2
 [tmpMutArr replaceObjectAtIndex:index withObject:
              [NSDictionary dictionaryWithDictionary:mutDict]];

1:从 'int' 分配给 'int' 的整数到指针转换不兼容;用 & 取地址

2:指向整数转换的不兼容指针将“int *”发送到 NSUInteger 类型的参数(又名“unsigned int”)

4

2 回答 2

2

换行:

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

用这条线:

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

因为您在这里使用字符串比较。

于 2012-08-14T17:31:13.893 回答
0

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

int index; //instead of 'int *index;'

这种改变应该可以解决问题。

int *index;如果您编写了then ,它可能会起作用index = &i;,因为您正在以这种方式保存指向整数的指针。但这似乎不是你想要做的。

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

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

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

于 2013-04-19T08:56:23.947 回答