我正在读一本书来学习Objective-C。我被困在其中一个练习中。您必须制作一个包含班级歌曲、播放列表和音乐收藏的程序。如果你创建一首歌曲,它必须自动添加到音乐收藏中,谁有一个 NSMutableArray 用于收集歌曲。如果您从音乐收藏中删除一个对象,则该歌曲必须从包含该歌曲的每个播放列表中删除。
歌曲界面
#import <Foundation/Foundation.h>
@interface Song : NSObject{
NSString *title;
NSString *artist;
NSString *album;
}
@property (copy) NSString *title;
@property (copy) NSString *artist;
@property (copy) NSString *album;
-(Song *) initWithNames:(NSString*) title1 and: (NSString*) artist1 and: (NSString*) album1;
@end
播放列表界面
#import <Foundation/Foundation.h>
@interface Playlist : NSObject{
NSString * title;
NSMutableArray *collecsongs;
}
@property (strong) NSString *title;
@property (strong) NSMutableArray *collecsongs;
-(Playlist *) initWithName: (NSString *) name;
@end
音乐收藏界面
#import <Foundation/Foundation.h>
#import "Playlist.h"
@interface MusicCollection : NSObject{
NSMutableArray *collecplist;
Playlist *library;
}
@property (strong) NSMutableArray *collecplist;
@property (strong) Playlist *library;
@end
因此,如果我创建一首歌曲,例如歌曲 1,是否有办法将其添加到播放列表中,自动将其添加到 mastercoleection 变量“库”中,而不是这样做
Song *song1 = [[Song alloc] initWithNames:@"Somebody That I Used To Know" and: @"Gotye" and: @"First Album"];
Playlist *Misrolas = [[Playlist alloc] initWithName: @"Misrolas"];
MusicCollection *music = [[MusicCollection alloc] init];
[Misrolas.collecsongs addObject: song1];//adds song1 to the playlist named "Misrolas"
[music.library.collecsongs addObject: song1];//adds song1 to the music collection
所以我不知道该怎么做,我在想可能覆盖 addObject:,但这似乎并不正确和容易,感谢您的帮助 =)
我这样做是这样的,有没有更有效或更好的方法来添加它???
-(void) addsong: (Song *)song addtocollection: (Playlist *) library{
NSAssert([song isKindOfClass: [Song class]], @"Not the same class");
[self.collecsongs addObject:song];
[library.collecsongs addObject: song];
}