这个题目,之前就回答过。
- // Playlist.h
- #import <Foundation/Foundation.h>
- @class Song;
- @interface Playlist : NSObject
- @property (nonatomic,strong) NSString *listName; // 列表名称
- @property (nonatomic, strong) NSMutableDictionary *songSet; // 歌曲集合
- // 添加歌曲
- - (void)addSong:(Song *)song;
- // 删除歌曲
- - (void)deleteSong:(NSString *)songName;
- @end
复制代码
- // Playlist.m
- #import "Playlist.h"
- #import "Song.h"
- @implementation Playlist
- // 添加歌曲
- - (void)addSong:(Song *)song
- {
- [_songSet setObject:song forKey:song.songName];
- }
- // 删除歌曲
- - (void)deleteSong:(Song *)songName
- {
- [_songSet removeObjectForKey:songName];
- }
- @end
复制代码
- //Song.h
- #import <Foundation/Foundation.h>
- @interface Song : NSObject
- @property (nonatomic, strong) NSString *songName; // 歌名
- @property (nonatomic, strong) NSString *singer; // 歌手
- @property (nonatomic, strong) NSString *zhuanJi; // 专辑
- @property (nonatomic, assign) double length; // 歌曲长度 (秒)
- @end
复制代码
- // Song.m
- #import "Song.h"
- @implementation Song
- @end
复制代码
- // main.m
- #import <Foundation/Foundation.h>
- #import "Song.h"
- #import "Playlist.h"
- int main(int argc, const char * argv[])
- {
- Song *s = [[Song alloc] init];
- s.songName = @"asdf";
-
- Playlist *p = [[Playlist alloc] init];
-
- NSMutableDictionary *dic = [NSMutableDictionary dictionary];
-
- p.songSet = dic;
-
- [p addSong:s];
- NSLog(@"%@", p.songSet);
-
- [p deleteSong:@"天意"];
-
- NSLog(@"%@", p.songSet);
-
- return 0;
- }
复制代码 |