0
votes

I'm trying to replace a dictionary in a mutable array. Steps 1 through 4 should be good, but I'm having some trouble in step 5 - 6. Can you show me what has to be done to make this function work:

- (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];
     }

EDIT:

Now the problem is:

 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: Incompatible integer to pointer conversion assigning to 'int' from 'int'; take the adress with &

2: Incompatible pointer to integer conversion sending 'int *' to parameter of type NSUInteger' (aka 'unsigned int')

2
if(int i=0;i<[tmpMutArr count];i++) should probably be a for loop? How did you get that through the compiler? - Please try to indent the source code better. - Martin R
I know this is not working as it should, I use some sample codes to put it together that's why I need some help making it right as this is way passed my experience level. 'i' isn't even declared. But I guess I should be some kind of 'for' loop instead, yes. - ingenspor
Literally just replace the 'if' with a 'for' - Dustin
int *index should be int index. - Martin R

2 Answers

2
votes

Replace the line:

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

with this line:

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

since you are using string comparison here.

0
votes

As pointed out in the comments by Martin R, you are trying to save an int value into a int pointer variable.

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

That change should do the trick.

It could have worked if you had written int *index; and then index = &i;, since you are saving a pointer to an integer that way. But it doesn't seem to be what you are trying to do.

The second error is due you are providing an int pointer instead of an integer to this method:

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

But declaring index as an integer solves both errors already.