質問

OK

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"];

NSString @" test"を追加しますNSMutableArray songCollectionへ

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(mutableまたはそれ以外)でもありません。 GeneralFunctions getJSONAsArrayのドキュメントまたは署名を確認し、実際に返される内容を確認してください。

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

この出力の内容-実際に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