문제

좋아, 나는 OBJ-C를 처음 접한다는 것을 알고 있지만, 모든 의도와 목적을 위해 다음은 다음과 같이 작동하는 것 같습니다.

songCollection = [[NSMutableArray alloc] init];
    [songCollection addObject:@"test"];
    //Array is init, and I can see it in the debugger.
    songCollection = [GeneralFunctions getJSONAsArray:@"library"];
    // I can see the expected data in the debugger after this.
    [songCollection retain];
    NSLog(@"%@", [songCollection objectAtIndex:0]);
        // Crashes here due to the array not responding to the selector. Also, the array is now empty.
    //NSLog(@"%@", songCollection);
    NSArray * songList = [songCollection objectAtIndex:1];
    NSLog(@"%@", songList);

누군가가 나를 도와 줄 수 있기를 바라고 있습니다. 나는 벽에 머리를 두드리고 있습니다!

도움이 되었습니까?

해결책

songCollection 원래 nsmutablearray 였지만, 당신은 반환 된 모든 것을 덮었습니다. [GeneralFunctions getJSONAsArray:@"library"]. 그것이 무엇이든, 아마 배열이 아닐 것입니다.

덧붙여서, 당신은 여기서 배열을 새고 있습니다.

다른 팁

코드를 단계별로 분리하겠습니다.

songCollection = [[NSMutableArray alloc] init];

새로운 빈 nsmutablearray를 할당합니다.

[songCollection addObject:@"test"];

nsmutableAreray songcollection에 nsstring @"test"를 추가합니다.

songCollection = [GeneralFunctions getJSONAsArray:@"library"];

당신이 만든 돌연변이 배열 (따라서 메모리 누출)에 대한 참조를 버리고 아직 소유하지 않은 것에 대한 새로운 포인터를 제공합니다.

[songCollection retain];

좋아요, 당신은 songcollection의 소유권을 얻습니다. 그리고 이것이 효과가 있기 때문에, 당신은 getjsonasarray가 nil 또는 nsobject를 반환했다는 것을 알고 있습니다.

NSLog(@"%@", [songCollection objectAtIndex:0]);
// Crashes here due to the array not responding to the selector. Also, the array is now empty.

따라서 분명히 SongCollection은 NIL이나 NSARRAY (돌연변이 또는 다른)도 아닙니다. GetJsonAsArray generaltections의 문서 또는 서명을 확인하고 실제로 반환되는 내용을 확인하십시오.

//NSLog(@"%@", songCollection);

이 출력은 무엇을합니까?

getjsonasarray가 nsarray를 반환하지 않는 이유를 알아 내면 nsarray를 nsmutablearray로 변환 할 수 있습니다.

songCollection = [[GeneralFunctions getJSONAsArray:@"library"] mutableCopy];
// You now own songCollection

또는

songCollection = [[NSMutableArray alloc] init];
// You now own songCollection
[songCollection addObjectsFromArray:[GeneralFunctions getJSONAsArray:@"library"];

GeneralFunctions getJsonasArray :@"Library"]는 실제로 NSARRAY를 반환합니까?

또한 해당 라인으로 다시 할당하기 전에 SongCollection을 출시하는 것을 잊고 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top