2012-08-14 140 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