oc基礎類型之NSDictionary
阿新 • • 發佈:2019-02-20
char fork ons tty 重復 初始化 alloc rem property
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { //字典用大括號賦值 //字典是無序的 系統會自動對其key排序 //字典的創建 NSDictionary *dic3 =@ { @"key1":@"value1", @"key2":@"value2", @"key3":@"value3" }; NSLog(@"%@",dic3); // NSDictionary *dics = [NSDictionary new]; // NSDictionary *dics = [[NSDictionary alloc] init]; // NSDictionary *dics = [NSDictionary dictionary]; //字典的初始化:- (instancetype)initWithObjectsAndKeys:(id)firstObject, ... NSDictionary *dic1= [[NSDictionary alloc]initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil];// 先寫值再寫鍵; NSLog(@"%@",dic1); // + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ... 類方法創建字典並賦值 NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key" ,nil]; NSLog(@"%@",dic2); //鍵值對的值是任意類型的Person *p1 = [[Person alloc] initWithname:@"肖明瑛" andage:17]; Person *p2 = [[Person alloc] initWithname:@"呂莎" andage:19]; Person *p3 =[[Person alloc] initWithname:@"巫婷" andage:18]; Person *p4 = [[Person alloc] initWithname:@"胡琳" andage:18]; NSDictionary *dic4 = @{ @"person1":p1, @"person2":p2, @"person3":p3, @"person4":p4 }; NSLog(@"%@",dic4); //@property (readonly) NSUInteger count;獲取字典中鍵值對的個數 NSLog(@"%lu",[dic4 count]); //- (nullable ObjectType)objectForKey:(KeyType)aKey;根據key取出對應的value值 NSLog(@"%@",[dic4 objectForKey:@"person1"]); //或者 NSLog(@"%@",dic4[@"person1"]); //@property (readonly, copy) NSArray<KeyType> *allKeys;獲取字典中所有的key值 NSArray *array2 = [dic4 allKeys]; NSLog(@"%@",array2); //獲取所有的value值 NSArray *array3 = [dic4 allValues]; NSLog(@"%@",array3); //- (NSArray<KeyType> *)allKeysForObject:(ObjectType)anObject;根據value值取出key值 可能不止一個 NSArray *array = [dic4 allKeysForObject:p1]; NSLog(@"%@",array); //可變的字典 //初始化方法:+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems; 開辟內存空間 // NSMutableDictionary *mutable=[NSMutableDictionary dictionaryWithCapacity:10 ];//每次開辟10個內存空間 用完再開辟 //NSMutableDictionary *mutable = [[NSMutableDictionary alloc] initWithCapacity:10]; //或者:+ (instancetype)dictionary; // NSMutableDictionary *mutable2 = [NSMutableDictionary dictionary]; //NSMutableDictionary *mutable2 = [NSMutableDictionary new]; // NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init]; //常用方法 // - (void)setValue:(nullable id)value forKey:(NSString *)key; 添加元素 在可變的數組中 重復的key值會覆蓋前面的(相當於更改) NSMutableDictionary *dic5 = [NSMutableDictionary dictionary]; [dic5 setValue:@"xmy" forKey:@"person5"]; NSLog(@"%@",dic5); [dic5 setValue:@"hyt" forKey:@"person5"]; NSLog(@"%@",dic5); //- (void)setObject:(ObjectType)anObject forKey:(KeyType <NSCopying>)aKey;添加鍵值對 [dic5 setObject:@"" forKey:@"person5"]; NSLog(@"%@",dic5); //- (void)removeObjectForKey:(KeyType)aKey; 根據key值刪除元素 [dic5 removeObjectForKey:@"person5"]; NSLog(@"%@",dic5); [dic5 removeObjectsForKeys:@[@"person3",@"person2"]];//刪除多個值 //- (void)removeAllObjects;刪除全部元素 [dic5 removeAllObjects]; } return 0; }
oc基礎類型之NSDictionary