這裏是我的項目:https://github.com/a8b/musicManager/tree/master/musicManager空NSMutableArray的添加元素後
我有問題與addSongToLibrary方法MusicCollection.m - 它不添加歌曲[庫addSong:歌]
這裏是我的項目:https://github.com/a8b/musicManager/tree/master/musicManager空NSMutableArray的添加元素後
我有問題與addSongToLibrary方法MusicCollection.m - 它不添加歌曲[庫addSong:歌]
那麼你註釋掉下面的代碼
/*-(instancetype) init {
self = [super init];
if (self) {
library.songs = [NSMutableArray array];
[library addSong:[Song new]];
}
return self;
}*/
所以庫從未instaniated,或者您取消註釋,或者在viewDidLoad
中您需要添加library.songs = [NSMutableArray array];
,否則它將爲零。
爲了簡化爲清楚起見代碼,它這樣做:
-(void) addSongToLibrary:(Song *)song {
for (Song *song in library.songs) {
NSLog(@"%@", song);
}
[library addSong:song];
}
}
您聲明具有相同名稱作爲方法參數的變量,所以它是隱藏在傳遞的值。
它有什麼用? [library.songs count]返回零。 – 2014-09-02 17:24:22
您應該在您的問題中發佈相關代碼,如果可能,請描述您嘗試解決問題的方法。 – 2014-09-02 17:03:17
在訪問數組之前是否初始化數組?你有'self.library = [[NSMutableArray alloc] init];'某處? – Logan 2014-09-02 18:16:12
是的,問題是關於分配內存。 – 2014-09-02 18:52:16