2012-10-05 2 views
0

NSString을 NSMutableArray에 추가하려고합니다 (NSString 만 포함). 어떤 오류도 발생하지 않지만 배열의 count을 쿼리 할 때 값을 추가하지 않았다는 것을 나타내는 0을 반환합니다. 이 NSLog가 제대로보고NSString이 NSMutableArray에 자신을 추가하지 않습니다.

// get an array of genres for this game 
NSArray *genreList = [self getAllItems:@"//genre" fileName:[NSURL URLWithString:queryURL]]; 
int numberOfGenres = [genreList count]; 
NSLog(@"This game has %u genres", numberOfGenres); 

의 XML 결과의 항목 수, 보통 1 ~ 5 사이 : 여기에 내 코드입니다 (그 확실히 100 % 작업 일부 XML을 받고).

// put the genre names from that array into the current game's object 
if (numberOfGenres > 0) { 
    for (int i = 0; i < numberOfGenres; i++) { 
     NSString *currentGenreName = [NSString stringWithString:[[genreList objectAtIndex:i] objectForKey:@"name"]]; 
     [currentGame.gameGenres addObject: currentGenreName]; 
     NSLog(@"I added a genre to %@, called %@.", currentGame.gameName, currentGenreName); 
    } 
} 

이 NSLog는 게임의 제목과 XML 트리의 내용도 올바르게보고합니다.

// save current game to game list 
NSLog(@"I'm about to save %@ to the array, with its %u genres", currentGame.gameName, [currentGame.gameGenres count]); 
[_listOfGamesReturnedFromSearch addObject:currentGame]; 

이 마지막 NSLog는 장르의 수를 항상 0으로보고합니다. _listOfGamesReturnedFromSearch 배열을 나중에 쿼리하면 괜찮은 (이름) 속성이 저장되었지만 장르 수가 0인데 어떤 이유로 든 저장하지 못했습니다.

아이디어가 있으십니까?

+0

배열을 선언하고 초기화 하시겠습니까? – DrummerB

+0

@DrummerB 분명히 아무데도. –

+0

obj-c를 수행 한 지 오래되었지만 currentGame.gameGenres가 nil로 설정되지 않았습니까? –

답변

2

이것은 배열을 할당하고 초기화하지 않는 전형적인 증상입니다. 따라서 nil을 유지하고 보낸 모든 메시지를 무시한 채로 유지합니다. Alloc-init 그것과 그것을 작동합니다.

+0

객체 자체에 대해 alloc 초기화를했는데 그 속성도 모두 초기화해야합니까? 그렇다면 객체 클래스의 init 메소드에서이 작업을 수행해야합니까? – Luke

+0

예, 속성을 할당/초기화해야하며 일반적으로 init 메소드에서이를 수행해야합니다. – DrummerB

+1

@lukech 오브젝트를 alloc-init하면 모든 인스턴스 변수가 0으로 초기화됩니다 ... Objective-C 런타임 참조를 지금 읽으십시오. –

관련 문제