1

わかりました、私は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);

ここで誰かが私を助けてくれることを願っています。私は頭を壁にぶつけています!

4

3 に答える 3

8

songCollectionはもともと NSMutableArray でしたが、から返されたもので上書きしました[GeneralFunctions getJSONAsArray:@"library"]。それが何であれ、それはおそらく配列ではありません。

ちなみに、ここで配列をリークしています。

于 2009-06-17T02:28:34.803 に答える
7

コードを段階的に分解してみましょう。

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(可変またはその他)でもないことは明らかです。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"];
于 2009-06-17T05:20:42.673 に答える
1

[GeneralFunctions getJSONAsArray:@"library"] は実際に NSArray を返しますか?

その行で再割り当てする前に、songCollection を解放することも忘れています。

于 2009-06-17T02:27:37.710 に答える