我试图用快速枚举来打印播放列表中的所有歌曲,但似乎我做错了。有人可以帮我吗? 我已经定义了一个宋级是这样的: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