我試圖用快速枚舉來打印播放列表中的所有歌曲,但似乎我做錯了。有人可以幫我嗎? 我已經定義了一個宋級是這樣的:Objective C快速枚舉問題
@interface Song : NSObject
@property (nonatomic,strong) NSString *title;
@property (nonatomic,strong) NSString *artist;
@property (nonatomic,strong) NSString *album;
@property (nonatomic,strong) NSString *playtime;
-(void) displaySong;
@end
displaySong功能如下:
-(void) displaySong {
NSLog(@"%@ - %@ [%@] - %@\n", artist, title, album, playtime);
}
,現在,到了播放列表類:
@interface PlayList : NSObject{
NSString *name;
NSMutableArray *playlist;
}
-(void) addSongToPlaylist: (Song *) song;
-(void) displayPlaylist;
@end
@implementation PlayList
-(void) addSongToPlaylist: (Song *) nameOfSong{
[playlist addObject:nameOfSong];
}
-(void) displayPlaylist{
NSLog(@"Playlist called %@:\n", name);
for(id obj in playlist)
{
if([obj isKindOfClass:[Song class]])
[obj displaySong];
}
}
// I also tried the displayPlaylist method this way, but it wasn't working either
-(void) displayPlaylist{
NSLog(@"Playlist called %@:\n", name);
for(Song *song in playlist)
{
[song displaySong];
}
@end
可能有人請解釋我爲什麼我的代碼不工作?
什麼displayPlaylist方法更好?
「不工作」是什麼意思?請更加明確您的預期和實際結果。 – 2012-03-04 22:19:40