Day.01.19 可變‘字典’

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        /*_______可變數組 NSMutableDictionary________________________________________________________*/
        
        //1??創建
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        
        NSLog(@"%@",dic1);
        
        NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]initWithCapacity:5];
        
        NSLog(@"%@",dic2);
        
        NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithCapacity:5];
        
        NSLog(@"%@",dic3);
        
        //2??方法
        
        NSArray *array = @[@"arr1",@"arr2"];
        
        //1.增&改:添加[鍵值對]
        
        /**
         *  (1)判斷字典中是否有該鍵,如果沒有則添加新的鍵值對
                                如果有則替換鍵原來所對應的對象
         */
        [dic setObject:array forKey:@"0"];
        [dic setObject:array forKey:@"1"];
        [dic setObject:array forKey:@"2"];
        [dic setObject:array forKey:@"3"];
        
        NSLog(@"%@",dic);
        
        //2.刪:移除[鍵值對]
        
            //移除全部
//        [dic removeAllObjects];
        
            //移除一個
        [dic removeObjectForKey:@"0"];
        
            //移除多個
        [dic removeObjectsForKeys:@[@"1",@"2"]];
        
        NSLog(@"%@",dic);
        
    }
    return 0;
}

2016-01-19 19:29:36.877 07NSMutableDictionary[1385:201302] {
}
2016-01-19 19:29:36.879 07NSMutableDictionary[1385:201302] {
}
2016-01-19 19:29:36.879 07NSMutableDictionary[1385:201302] {
}
2016-01-19 19:29:36.879 07NSMutableDictionary[1385:201302] {
    0 =     (
        arr1,
        arr2
    );
    1 =     (
        arr1,
        arr2
    );
    2 =     (
        arr1,
        arr2
    );
    3 =     (
        arr1,
        arr2
    );
}
2016-01-19 19:29:36.879 07NSMutableDictionary[1385:201302] {
    3 =     (
        arr1,
        arr2
    );
}
Program ended with exit code: 0

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容