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:タイプNSUInteger'(別名' unsigned int')のパラメーターに'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;、それからを書いていればうまくいく可能性があります。index = &i;しかし、それはあなたがやろうとしていることではないようです。

2番目のエラーは、このメソッドに整数ではなくintポインターを指定しているためです。

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

しかし、インデックスを整数として宣言すると、両方のエラーがすでに解決されています。

于 2013-04-19T08:56:23.947 に答える