objective-c - 替换 NSMutableArray 中指定的字典

标签 objective-c cocoa

我正在尝试替换可变数组中的字典。 步骤 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”)

最佳答案

替换行:

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

用这一行:

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

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

关于objective-c - 替换 NSMutableArray 中指定的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11957377/

相关文章:

ios - 替换NSDictionary中的键值

iphone - 如何对选择器的引用进行编码/解码?

objective-c - 如何填充 NSTableView?

objective-c - 加载后指向 NSWindow Xib 的指针?

objective-c - 在哪里可以找到 Objective-C 2.0 运行时的最新源代码?

ios - Xcode 4.6.2 : Variable Doesn't Show Up In Outlets

objective-c - 将 NSTextField 输入限制为仅数字? NSNumberformatter

iphone - 在 iPhone 应用程序中下载多个文件( objective-c )

cocoa - Cocoa 中具有 Z 深度的 GUI

ios - 使用 CVPixelBufferCreate 复制/复制 CVPixelBufferRef