IOS基础之NSFounation框架的NSDictionary,NSMutableDictionary的使用
生活随笔
收集整理的這篇文章主要介紹了
IOS基础之NSFounation框架的NSDictionary,NSMutableDictionary的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS基礎(chǔ)之NSFounation框架的NSDictionary,NSMutableDictionary的使用
// // main.m // Day11-NSFoudation框架 // // Created by 魯軍 on 2021/5/8. // /**1) NSString 的本質(zhì)是1個(gè)類2) @"jack" 是NSString 對(duì)象3) 字符串的恒定性4) 最常用的5個(gè)方法5) 其他方法2。NSMutableString1) 是NSString 的子類可變3) 大批量的字符串的拼接的時(shí)候3 NSArray1)數(shù)組2) 特點(diǎn)3) 創(chuàng)建 取出 遍歷 其他的常用1) 是NSArray的子類2) 元素可以新增和刪除,5 。NSNumber 包裝基本數(shù)據(jù)類型的---------------------------------------1 將數(shù)組的值保存在磁盤中數(shù)據(jù)的持久化2.plist 屬性列表文件這個(gè)文件 可以保存數(shù)組,把數(shù)組中的元素保存在這個(gè)文件之中3.原理,將 數(shù)組的信息 存儲(chǔ)在plist文件之中,就會(huì)將數(shù)組的所有元素存儲(chǔ)到這個(gè)文件中- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;2.將plist文件當(dāng)中的數(shù)據(jù)還原成一個(gè)數(shù)組。+ (NSArray<ObjectType> *)arrayWithContentsOfFile:(NSString *)path;*/ #import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/lujun/Desktop/abc.plist"];if(arr!=nil){//讀取成功for (NSString *str in arr) {NSLog(@"%@",str);}}// NSLog(@"%@",arr);// [arr writeToFile:@"/Users/lujun/Desktop/abc.plist" atomically:NO];// NSString *str = @"jack"; // // NSArray *arr = @[@"jack",@"rose",@"lili",@"lucy"]; // // [arr writeToFile:@"/Users/lujun/Desktop/abc.plist" atomically:NO]; // // // for (NSString *str in arr) { // NSLog(@"%@",str); // }return 0; } // // main.m // 02-NSDictionary // // Created by 魯軍 on 2021/5/8. ///**數(shù)組的下標(biāo)不固定,無(wú)法通過(guò)下標(biāo)來(lái)確定數(shù)組中的元素所以 NSDictionary的引入。希望: 有一種存儲(chǔ)數(shù)據(jù)的方式,可以快速唯一的確定數(shù)組的元素。存儲(chǔ)數(shù)據(jù)的時(shí)候,必須要為存儲(chǔ)的數(shù)據(jù)鍵值對(duì)這個(gè)別名的作用,就是用來(lái)確定別名對(duì)應(yīng)的數(shù)據(jù)的Key-ValueKey就是鍵Value就是值2.NSDictionary NSMutableDictionary1) 他們是數(shù)組,要找到存儲(chǔ)這個(gè)數(shù)組中的數(shù)據(jù),通過(guò)別名來(lái)找,而不是下標(biāo)3 NSDictionary 字典數(shù)組1) 存儲(chǔ)數(shù)據(jù)的原理a.以鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù)b. 字典數(shù)組一旦創(chuàng)建,其中的元素就無(wú)法動(dòng)態(tài)的新增和刪除c. 鍵 只能是遵守了NSCoping 協(xié)議的對(duì)象。 而NSString就是遵守了這個(gè)協(xié)議值: 只能是OC對(duì)象2)創(chuàng)建字典數(shù)組NSDictionary *dict = [NSDictionary new];NSDictionary *dict2 = [[NSDictionary alloc] init];NSDictionary *dict3 =[NSDictionary dictionary];這種創(chuàng)建沒(méi)有意義,因?yàn)?沒(méi)有任何元素3) 一般的創(chuàng)建的方式+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"jack",@"name",@"北京市XX街道",@"address", nil];值鍵挨個(gè)寫在后面4) 簡(jiǎn)要的創(chuàng)建方式NSDictionary *dict2 = @{鍵1:值1,鍵2:值2,鍵3:值3,鍵4:值4};NSDictionary *dict2 = @{@"name":@"rose",@"age":@"18",@"address":@"北京"};NSString *str = dict2[@"name"];NSLog(@"%@",str);NSString *str2 =[dict2 objectForKey:@"age"];沒(méi)有這個(gè)key返回的是nil不會(huì)報(bào)錯(cuò)。如果鍵的不存在 ,返回的是nil計(jì)算有幾個(gè)鍵值對(duì) NSUInteger count = dict2.count;5.遍歷字典數(shù)組1)字典數(shù)組中的數(shù)組無(wú)法使用下標(biāo)去取2)使用for in 循環(huán),遍歷出來(lái)的都是key 鍵再通過(guò)鍵取到值NSDictionary *dict2 = @{@"name":@"rose",@"age":@"18",@"address":@"北京"};for (id item in dict2) {NSLog(@"%@ = %@",item,dict2[item]);}3)使用block遍歷[dict2 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {NSLog(@"%@-",obj);}];6. 打印的順序,使用NSLog(@"%@",dict); 按照鍵的ASCII碼打印的字典數(shù)組存儲(chǔ)數(shù)據(jù)的原理1)當(dāng)往字典數(shù)組中存儲(chǔ)1個(gè)鍵值對(duì)的時(shí)候,這個(gè)鍵值2)取值的時(shí)候,也是根據(jù)做1個(gè)哈希算法,就可以算出這個(gè)鍵值對(duì)存儲(chǔ)的下標(biāo),然后直接找到這個(gè)下標(biāo)的數(shù)據(jù)取出就可以了。與NSArray對(duì)比,1)NSArray 數(shù)組是挨個(gè)挨個(gè)的屁股后面,按照順序來(lái)存儲(chǔ)的,字典數(shù)組中不是挨個(gè)挨個(gè)的存儲(chǔ)的2) 取得時(shí)候, 如果取值的時(shí)候,是全部一股腦的取出來(lái),這個(gè)時(shí)候NSArray快一點(diǎn)如果取值的時(shí)候,只會(huì)取數(shù)組中指定的幾個(gè)元素,字典數(shù)組中的取值更快一點(diǎn)什么時(shí)候是有NSArray 什么時(shí)候使用字典數(shù)組存儲(chǔ)進(jìn)去之后,一旦要取值,就是全部取出,NSArray存儲(chǔ)進(jìn)去之后,取值只會(huì)取指定的幾個(gè)元素,字典數(shù)組。*/ #import <Foundation/Foundation.h> #import "CZPerson.h"int main(int argc, const char * argv[]) {NSDictionary *dict2 = @{@"name":@"rose",@"age":@"18",@"address":@"北京"};NSLog(@"%@",dict2);CZPerson *p1 = [[CZPerson alloc] initWithName:@"小明1"];CZPerson *p2 = [[CZPerson alloc] initWithName:@"小明2"];CZPerson *p3 = [[CZPerson alloc] initWithName:@"小明3"];CZPerson *p4 = [[CZPerson alloc] initWithName:@"小明4"];CZPerson *p5 = [[CZPerson alloc] initWithName:@"小明5"];NSArray *arr =@[p1,p2,p3,p4,p5];NSDictionary *dict = @{p1.name:p1,p2.name:p2,p3.name:p3,p4.name:p4,p5.name:p5};dict[@"小明3"];/*NSDictionary *dict2 = @{@"name":@"rose",@"age":@"18",@"address":@"北京"};for (id item in dict2) {NSLog(@"%@ = %@",item,dict2[item]);}[dict2 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {NSLog(@"%@-",obj);}];*//*NSDictionary *dict2 = @{@"name":@"rose",@"age":@"18",@"address":@"北京"};[dict2 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {NSLog(@"%@-",obj);}];for(NSString *item in dict2.allKeys){NSLog(@"%@",item);}NSUInteger count = dict2.count;NSLog(@"%lu",count); //3NSString *str = dict2[@"name"];NSLog(@"%@",str);NSString *str2 =[dict2 objectForKey:@"age"];NSLog(@"%@",str2);NSLog(@"%@",dict2);NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"jack",@"name",@"北京市XX街道",@"address", nil];NSLog(@"%@",dict);*/ // // NSDictionary *dict = [NSDictionary new]; // NSDictionary *dict2 = [[NSDictionary alloc] init]; // NSDictionary *dict3 =[NSDictionary dictionary];// // NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"jack",@"rose",@"lili", nil]; // // [arr indexOfObject:@"lili"]; // [arr addObject:@"lucy"]; // [arr removeObjectAtIndex:0]; // [arr removeObjectAtIndex:2]; //return 0; } // // CZPerson.h // 02-NSDictionary // // Created by 魯軍 on 2021/5/8. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface CZPerson : NSObject @property(nonatomic,strong)NSString * name; -(instancetype)initWithName:(NSString *)name; @end NS_ASSUME_NONNULL_END// // CZPerson.m // 02-NSDictionary // // Created by 魯軍 on 2021/5/8. //#import "CZPerson.h" @implementation CZPerson - (instancetype)initWithName:(NSString *)name{if(self=[super init]){self.name = name;}return self; } @end // // main.m // 03-NSMutableDictionary // // Created by 魯軍 on 2021/5/8. // /**NSMutableDictionary1) 是NSDictionary的子類,所以 NSMUItableDictionary 也是1個(gè)字典數(shù)組,也是以鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù)的2) 重點(diǎn): NSMutableDictionary 在父類的基礎(chǔ)之上做的擴(kuò)展,存儲(chǔ)在其中的元素可以動(dòng)態(tài)的新增和刪除3) 創(chuàng)建可變的字典數(shù)組NSMutableDictionary *dict = [NSMutableDictionary new];NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];這樣創(chuàng)建出來(lái)的可變數(shù)組的長(zhǎng)度為0 ,是有意義的,可以動(dòng)態(tài)的新增和刪除NSMutableDictionary *dict5 = @{@"name":@"jack"};這樣是不行的,報(bào)錯(cuò)的。2.如果新增鍵值對(duì),- (void)setObject:(ObjectType)anObject forKey:(id<NSCopying>)aKey;[dict setObject:@"廣州市xxx街道" forKey:@"address"];如果鍵重復(fù),后添加的就會(huì)替換原有的3,如何刪除呢- (void)removeAllObjects;刪除所有的數(shù)組字典刪除指定的鍵值對(duì)根據(jù)鍵去刪除指定的 鍵值對(duì)- (void)removeObjectForKey:(KeyType)aKey;[dict removeObjectForKey:@"name"];4.字典的數(shù)組的信息持久化起來(lái)也可以將字典的數(shù)組信息保存到plist文件中+ (NSDictionary<KeyType, ObjectType> *)dictionaryWithContentsOfFile:(NSString *)path;- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;從plist 文件中還原回字典*/ #import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lujun/Desktop/dict.plist"];NSLog(@"%@",dict);// NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"jack",@"name",@"18",@"age", nil]; // // [dict setObject:@"廣州市xxx街道" forKey:@"address"]; // [dict setObject:@"178.1" forKey:@"height"]; // // //數(shù)據(jù)的持久化[dict writeToFile:@"/Users/lujun/Desktop/dict.plist" atomically:NO]; // NSLog(@"YES");// NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"jack",@"name",@"18",@"age", nil]; // [dict setObject:@"廣州市xxx街道" forKey:@"address"]; // [dict setObject:@"183.3" forKey:@"height"]; // [dict setObject:@"Lujun" forKey:@"name"]; // [dict setObject:@"Lujunjun" forKey:@"name"]; // NSLog(@"%@",dict); // [dict removeObjectForKey:@"name"];// [dict removeAllObjects];// NSLog(@"%@",dict);/*NSMutableDictionary *dict = [NSMutableDictionary new];NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];NSMutableDictionary *dict4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"jack",@"name",@"18",@"age", nil];*/return 0; } // // main.m // 03-集合的內(nèi)存管理 // // Created by 魯軍 on 2021/5/9. ///**集合NSArray集合,NSDictionary字典集合,就叫做集合2.在MRC的模式下,將1個(gè)對(duì)象存儲(chǔ)到集合中,會(huì)不會(huì)影響到引用的計(jì)數(shù)器將對(duì)象存儲(chǔ)到集合之中,會(huì)為這個(gè)對(duì)象的引用計(jì)數(shù)加1當(dāng)集合銷毀的時(shí)候,就會(huì)向存儲(chǔ)在集合中的所有的對(duì)象發(fā)送1條release消息3 使用@[] 或者 @{} 創(chuàng)建的集合已經(jīng)是被autorelease過(guò)的了直接調(diào)用同名的類方法創(chuàng)建的對(duì)象,也是被autorelease過(guò)的了4. 在ARC的模式下, 集合的元素是1個(gè)強(qiáng)類型的指針。*/ #import <Foundation/Foundation.h> #import "CZPerson.h" int main(int argc, const char * argv[]) {CZPerson *p1 = [CZPerson new];NSArray *arr = @[p1];// // @autoreleasepool { // NSArray *arr1 = [NSArray arrayWithObjects:@"jack",@"rose",@"lili", nil]; // NSArray *arr2 = @[@"luxu",@"lujun",@"dajun"]; // // } // // // CZPerson *p1 = [CZPerson new]; // // NSLog(@"%lu",p1.retainCount); // // NSArray *arr = @[p1]; // // NSLog(@"%lu",p1.retainCount); // // // [arr release]; // NSLog(@"%lu",p1.retainCount); // // //有對(duì)象創(chuàng)建,就有對(duì)象釋放 // [p1 release]; //return 0; }源碼在我的主頁(yè)下面
總結(jié)
以上是生活随笔為你收集整理的IOS基础之NSFounation框架的NSDictionary,NSMutableDictionary的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 助你解决新手开车四大问题 为您支招
- 下一篇: 敏捷教练的工具箱