OC動態字典和數組演示demo

前言及題目:

最近在學OC框架,學到了字典和數組,於是寫了一道練習題,這道題從語法角度來說並不難,但是邏輯和類的設計上麻煩一些。可能因爲我是小白的原因吧!在這裏把這個代碼記錄下來,因爲我寫了兩個小時吧,從拿到題目開始構思到完成。

題目: 定義三個新類,分別名爲Song、PlayList和MusicCollection。Song對象包含着關於特定歌曲的信息,比如歌曲名、藝術家、專輯、歌曲長度等;PlayList對象包含播放列表名稱和一個歌曲的集合;MusicConllection對象包含播放列表集合,它包括一個名爲Library的主播放列表,這個列表包含該集合中的所有歌曲。定義上述的三個類,並編寫方法實現下列任務:
//• 創建一個Song對象,並設置其信息。
//• 創建一個PlayList對象,並對播放列表添加和刪除歌曲。如果一首新歌不在主列表中,那麼將其添加進去。確保從主播放列表中刪除一首歌時,也要從音樂集合中的其他播放列表刪除此歌曲。
//• 創建一個MusicCollection對象,並對該集合添加和刪除播放列表。
//• 搜索並顯示關於所有歌曲、播放列表或整個音樂集合的信息。

代碼及程序結構:

main.m:

#import <Foundation/Foundation.h>
#import "Song.h"
#import "PlayList.h"
#import "MusicConllection.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
//        MusicCollection
        MusicConllection *musicCollection=[[MusicConllection alloc] init];
//        歌曲數組
        NSArray *songArray=[NSArray array];
//        添加歌曲
        for (int i=0; i<5; i++) {
//            輸入歌名
            char *name=malloc(30*sizeof(char));
            NSLog(@"輸入song%i的歌名:",i);
            gets(name);
            NSString *nameStr=[NSString stringWithUTF8String:name];
//            輸入藝術家
            char *artist=malloc(20*sizeof(char));
            NSLog(@"輸入song%i的藝術家:",i);
            gets(artist);
            NSString *artistStr=[NSString stringWithUTF8String:artist];
//            輸入專輯
            char *album=malloc(30*sizeof(char));
            NSLog(@"輸入song%i的專輯:",i);
            gets(album);
            NSString *albumStr=[NSString stringWithUTF8String:album];
//            輸入歌曲長度
            int length;
            NSLog(@"輸入song%i的長度:",i);
            scanf("%d",&length);
            getchar();
//            song
            Song *song=[[Song alloc] initWithName:nameStr andArtist:artistStr andAlbum:albumStr andLength:length];
//            songArray
            songArray=[songArray arrayByAddingObject:song];
        }
//        PlayList1
        PlayList *playList1=[[PlayList alloc] initWithListName:@"playList1"];
//        PlayList2
        PlayList *playList2=[[PlayList alloc] initWithListName:@"playList2"];
//       把0,1,2這三首歌曲放到歌曲列表1,把3,4兩首歌放到歌曲列表2
        if ([musicCollection addList:playList1]&&[musicCollection addList:playList2]) {
            for (int i=0; i<3; i++) {
//                添加歌曲到列表1
                [musicCollection addSongToList:songArray[i] andListName:playList1.listName];
            }
            for (int i=3; i<5; i++) {
//                添加歌曲到列表2
                [musicCollection addSongToList:songArray[i] andListName:playList2.listName];
            }
//            打印列表1
            NSLog(@"%@",[[[musicCollection collection] objectForKey:playList1.listName] songList]);
//            打印列表2
            NSLog(@"%@",[[[musicCollection collection] objectForKey:playList2.listName] songList]);
//            打印主列表
            NSLog(@"%@",[[musicCollection collection] objectForKey:@"mainPlayList"]);
            for (int i=0; i<3; i++) {
                //                從列表1中刪除歌曲
                [musicCollection removeSongFromList:[songArray[i] name] andListName:playList1.listName];
            }
            for (int i=3; i<5; i++) {
                //                從主列表中刪除歌曲
                [musicCollection removeSongFromMainList:[songArray[i]name]];
            }
//            刪除列表1
            [musicCollection removeList:playList1.listName];
//            刪除列表2
            [musicCollection removeList:playList2.listName];
        }
    }
    return 0;
}

Song.h

//Song對象包含着關於特定歌曲的信息,比如歌曲名、藝術家、專輯、歌曲長度等;
#import <Foundation/Foundation.h>
@interface Song : NSObject
@property NSString* name;//歌名
@property NSString* artist;//藝術家
@property NSString* album;//專輯
@property float length;//歌曲長度
//在構造函數中完成歌曲信息設置
-(id)initWithName:(NSString *)name andArtist:(NSString*) artist andAlbum:(NSString *)album andLength:(float) length;
@end

Song.m

#import "Song.h"
@implementation Song
//構造,完成歌曲初始化
-(id)initWithName:(NSString *)name andArtist:(NSString *)artist andAlbum:(NSString *)album andLength:(float)length{
    if (self=[super init]) {
        _name=name;
        _artist=artist;
        _album=album;
        _length=length;
    }
    return self;
}
//查詢
-(NSString *)description{
    NSMutableString *str=[[NSMutableString alloc] init];
    [str appendString:@"{\n"];
    [str appendFormat:@"\t歌名:%@,藝術家:%@,專輯:%@,長度:%g\n",_name,_artist,_album,_length];
    [str appendFormat:@"}\n"];
    return str;
}
@end

PlayList.h

//PlayList對象包含播放列表名稱和一個歌曲的集合;
#import <Foundation/Foundation.h>
#import "Song.h"
@interface PlayList : NSObject
@property NSString* listName;//播放列表名稱
@property NSMutableDictionary *songList;//播放列表內容,是一個動態字典,裏面存放song對象
-(id)initWithListName:(NSString*)listName;//創建歌曲列表,初始化列表名
-(BOOL)addSong:(Song*)song;//添加歌曲,返回操作是否成功
-(BOOL)removeSong:(NSString*)songName;//刪除歌曲,返回操作是否成功
@end

PlayList.m

#import "PlayList.h"
@implementation PlayList
//構造,完成列表初始化
-(id)initWithListName:(NSString *)listName{
    if (self=[super init]) {
        _listName=listName;
//        創建歌曲列表內容對象,之後添加刪除操作都針對該內容對象
        NSMutableDictionary *songList=[NSMutableDictionary dictionaryWithCapacity:100];
        _songList=songList;
    }
    return self;
}
//添加歌曲
-(BOOL)addSong:(Song *)song{
//    先檢查本列表中是否已經包含該歌曲,如果包含則返回NO,表示操作失敗
//    否則添加並返回YES
    if ([[_songList allKeys] containsObject:song.name]) {//如果包含
        return NO;
    }else{
        [_songList setObject:song forKey:song.name];//向本列表添加
        return YES;
    }
}
//刪除歌曲
-(BOOL)removeSong:(NSString *)songName{
//    先檢查本列表中是否包含該歌曲,如果包含則返回NO,表示操作失敗
//    否則添加並返回YES
    if ([[_songList allKeys] containsObject:songName]) {//如果包含
        [_songList removeObjectForKey:songName];
        return YES;
    }else{
        return NO;
    }
}
//查詢
-(NSString *)description{
    NSMutableString *str=[[NSMutableString alloc] init];
    [str appendFormat:@"%@:{\n",_listName];
    for (id key in _songList) {
        [str appendFormat:@"\t%@,",[[_songList objectForKey:key] name]];
    }
    [str appendFormat:@"}\n"];
    return str;
}
@end

MusicConllection.h

//MusicConllection對象包含播放列表集合,它包括一個名爲Library的主播放列表,這個列表包含該集合中的所有歌曲。
#import <Foundation/Foundation.h>
#import "PlayList.h"
#import "Song.h"
@interface MusicConllection : NSObject
@property NSMutableDictionary *collection;//播放列表集合內容,裏面要包括一個主播放列表還有其他播放列表
-(id)init;
-(BOOL)addSongToList:(Song*)song andListName:(NSString *)listName;//添加歌曲到指定列表
-(BOOL)removeSongFromList:(NSString*)songName andListName:(NSString*)listName;//從指定列表刪除歌曲
-(BOOL)removeSongFromMainList:(NSString*)songName;//從主播放列表中刪除歌曲
-(BOOL)addList:(PlayList*)newPlayList;//向音樂集合中添加播放列表
-(BOOL)removeList:(NSString*)playListName;//從音樂集合中刪除播放列表
@end

MusicConllection.m

#import "MusicConllection.h"

@implementation MusicConllection
//構造函數完成初始化
-(id)init{
    if (self=[super init]) {
//        創建播放列表集合
        NSMutableDictionary *collection=[NSMutableDictionary dictionaryWithCapacity:100];
//        創建一個主播放列表
        PlayList *mainPlayList=[[PlayList alloc] initWithListName:@"mainPlayList"];
//        將主播放列表添加到播放列表集合當中
        [collection setObject:mainPlayList forKey:mainPlayList.listName];
//        將剛剛創建的collection賦給成員變量播放列表內容對象
        _collection=collection;
    }
     return self;
}
//添加歌曲到指定列表中,添加完成之後要查看主列表中是否有
-(BOOL)addSongToList:(Song *)song andListName:(NSString *)listName{
    if ([[_collection objectForKey:listName] addSong:song]) {//指定列表添加成功
        NSLog(@"向列表%@添加%@成功",listName,song.name);
        if (![[[[_collection objectForKey:@"mainPlayList"] songList]allKeys]containsObject:song.name]) {//主列表中不包含
            NSLog(@"主列表中不包含該歌曲");
//            往主列表中添加
            [[[_collection objectForKey:@"mainPlayList"] songList]setObject:song forKey:song.name];
            NSLog(@"主列表中添加%@成功",song.name);
        }
        return YES;
    }else{
        NSLog(@"向列表%@添加%@失敗",listName,song.name);
        return NO;
    }
}
//從指定列表中刪除歌曲,刪除完成後要查看其他列表中是否也含有該歌曲,如果沒有就要從主列表中刪除該首歌
-(BOOL)removeSongFromList:(NSString *)songName andListName:(NSString *)listName{
    int tag=0;
    if ([[_collection objectForKey:listName] removeSong:songName]) {//指定列表刪除成功
//        遍歷collection中的列表,看除了main是否還有列表中含有該歌曲
        NSEnumerator *enu=[_collection keyEnumerator];
        id key;
        while (key=[enu nextObject]) {
            if (![key isEqualToString:@"mainPlayList"]&&[[[[_collection objectForKey:key] songList]allKeys]containsObject:songName])
            {//非主列表包含song
                tag=1;
                break;
            }
        }
//     判斷
        if (tag==0) {//其他列表不包含這首歌,主列表刪除
            [[_collection objectForKey:@"mainPlayList"] removeSong:songName];
        }
        NSLog(@"從指定列表%@中刪除%@成功",listName,songName);
                return YES;
    }else{
        NSLog(@"指定列表%@中沒有%@,刪除失敗",listName,songName);
        return NO;
    }

}
//向集合中添加播放列表
-(BOOL)addList:(PlayList *)newPlayList{
    if ([[_collection allKeys] containsObject:newPlayList.listName]) {//已經有該列表
        NSLog(@"集合中已經有%@,添加失敗",newPlayList.listName);
        return NO;
    }else{//添加
        [_collection setObject:newPlayList forKey:newPlayList.listName];
        NSLog(@"向集合中添加%@成功",newPlayList.listName);
        return YES;
    }
}
//從集合中刪除播放列表
-(BOOL)removeList:(NSString *)playListName{
    if (![[_collection  allKeys] containsObject:playListName]||[playListName isEqualToString:@"mainPlayList"]) {//沒有該列表或者想要刪除主列表,失敗
        NSLog(@"集合中沒有%@,刪除失敗",playListName);
        return NO;
    }else{
        [_collection removeObjectForKey:playListName];
        NSLog(@"集合中刪除%@成功",playListName);
        return YES;
    }
}
//從主播放列表中刪除歌曲
-(BOOL)removeSongFromMainList:(NSString *)songName{
    if (![[[[_collection objectForKey:@"mainPlayList"] songList]allKeys]containsObject:songName]) {//主列表不包含
        NSLog(@"主列表中不包含%@,刪除失敗",songName);
        return NO;
    }else{
        [[_collection objectForKey:@"mainPlayList"]removeSong:songName];//刪除
//        遍歷,查找其他列表中沒有被刪除的對應歌曲
        NSEnumerator *enu=[_collection keyEnumerator];
        id key;
        while (key=[enu nextObject]) {
            if ([[[[_collection objectForKey:key] songList]allKeys]containsObject:songName])
            {
                [[_collection objectForKey:key] removeSong:songName];
            }
        }
        NSLog(@"從主列表中刪除%@成功",songName);
        return YES;
    }
}
//查詢
-(NSString *)description{
    NSMutableString *str=[[NSMutableString alloc] init];
    [str appendFormat:@"collection:{/n"];
    for (id key in _collection) {
        [str appendFormat:@"\t%@,",key];
    }
    [str appendFormat:@"}\n"];
    return str;
}
@end

NSMutableDictionary+show.h

//
//  NSMutableDictionary+show.h
//  OC-Foundation4
//
//  Created by mac on 15/10/14.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSMutableDictionary (show)

@end

NSMutableDictionary+show.m

//
//  NSMutableDictionary+show.m
//  OC-Foundation4
//
//  Created by mac on 15/10/14.
//  Copyright (c) 2015年 macb. All rights reserved.
//

#import "NSMutableDictionary+show.h"

@implementation NSMutableDictionary (show)
-(NSString *)descriptionWithLocale:(id)locale{
    NSMutableString *mulStr=[NSMutableString string];
    [mulStr appendString:@"{\n"];
    for (id key in self)//self指的是當前調用的字典
    {
        [mulStr appendFormat:@"\t%@ = %@;\n",key,[self objectForKey:key]];
    }
    [mulStr appendString:@"}"];
    return mulStr;
}
@end

運行:

這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章