2012-08-14 117 views
1

指数这是代码,我试图完成获取数组对象

-(IBAction)theButtonIsSelected:(id)sender { 

    NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]]; 
    [mutDict setObject:@"Yes" forKey:@"Favorite"]; 

    NSString *nameString = [mutDict valueForKey:@"Name"]; 

    NSArray *allObjects; 
    allObjects = [[NSArray alloc] initWithContentsOfFile:path]; 

    NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjects]; 
    int index; 
    //I think I just need a little piece right here to set the current allObjectsIndex to match nameString? 
    [tmpMutArr replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:mutDict]]; 

    allObjects = nil; 
    allObjects = [[NSArray alloc] initWithArray:tmpMutArr]; 

    [allObjects writeToFile:path atomically:YES]; 
    } 

这是我的问题:

if (what I'm trying to do above can be done) { 
How to finish it? 
} else { 
How to make a function to change the "Favorite" key's value of plist object, 
when detailsDataSource not always containing the complete list of objects? 
That's why I'm trying to include allObjects and index in this code. 
} 

编辑:

代码现在看起来是这样的:

 NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]]; 
    [mutDict setObject:@"Yes" forKey:@"Favorite"]; 

    NSString *nameString = [[detailsDataSource objectAtIndex:detailIndex] valueForKey:@"Name"]; 

    NSArray *allObjectsArray = [[NSArray alloc] initWithContentsOfFile:path]; 

    NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray]; 


    if(int i=0;i<[tmpMutArr count];i++) 
//Errors ^here    and here^ 
    { 
    if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]]) 
    { 
    NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i]; 
    if([tempDict valueForKey:@"Name" == [NSString stringWithFormat:@"@%", nameString];) //Is this correct? 
    { 
    index = i; //index of dictionary 
    } 
    } 
    } 

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

    allObjectsArray = nil; 
    allObjectsArray = [[NSArray alloc] initWithArray:tmpMutArr]; 

    [allObjectsArray writeToFile:path atomically:YES]; 

错误:1期望的表达式2未申报的标识符'我'如何申报我并修复其他错误?

+0

你可能要考虑使用'NSNumber'为心仪的对象,通过使用'[NSNumber的numberWithBool:YES]'而不是字符串“是“ – Daniel 2012-08-14 03:00:25

+0

是的,但我稍后会解决这个问题,现在我专注于获得正确的plist副本。 – ingenspor 2012-08-14 13:57:19

回答

3

你可以得到解释的指标是这样:

if(int i=0;i<[tmpMutArr count];i++) 
{ 
    if([[tmpMutArr objectAtIndex:i] isKindOfClass:[NSDictionary class]]) 
    { 
     NSMutableDictionary *tempDict = [tmpMutArr objectAtIndex:i]; 
     if([tempDict objectForKey:@"Favorite") 
     { 
     index = i; // here u have your index of dictionary 
     } 
    } 
} 
+0

看起来像它会去做,但你能告诉我如何解决问题编辑中的错误? – ingenspor 2012-08-14 13:51:42