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

+0

네,하지만 나중에 고칠 것입니다, 지금 나는 정확한 plist 복사본을 얻는 데 집중하고 있습니다. – ingenspor

답변

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