iOS複習筆記14:常用資料結構之類,ios複習14資料結構
一 NSString/NSMutableString字串
1 NSString
<pre name="code" class="objc">NSString* s1 = @"string";//NSString* s2 = [[NSString alloc] initWithFormat(@"%d is one",1)];NSString* s2 = [NSString stringWithFormat(@"%d is one",1)];// C語言字串和OC字串相互轉換// NSString* os = [[NSString alloc] initWithUTF8String:"C String"];NSString* os = [NSString stringWithUTF8String:"C String"];const char* cs = [os UTF8String]; // 使用檔案內容初始化字串// NSString* s3 = [[NSString alloc] initWithContentsOfFile:@"檔案絕對路徑"// encoding:NSUTF8StringEncoding // 中文編碼// error:nil];NSString* s3 = [NSString stringWithContentsOfFile:@"檔案絕對路徑" encoding:NSUTF8StringEncoding // 中文編碼 error:nil];// URL:///資源路徑// 協議頭:///資源路徑// file:///資源路徑// ftp:///資源路徑// NSURL* url = [[NSURL alloc] initWithString:@"file://路徑"];NSURL* url = [NSURL urlWithString:@"file://路徑"];// NSString* s6 = [[NSString alloc] initWithContentsOfURL:url// encoding:NSUTF8StringEncoding // 中文編碼// error:nil];NSString* s6 = [NSString stringWithContentsOfURL:urlencoding:NSUTF8StringEncoding // 中文編碼 error:nil];// 字串分割NSArray* arr = [@"one|two|three|four|five" componentsSeparatedByString:@"|" ]// 寫入檔案[s1 writeToFile:@"檔案路徑"atomiccally:YES encoding:NSUTF8StringEncoding error:nil];[s1 writeToURL:urlatomiccally:YES encoding:NSUTF8StringEncoding error:nil];NSString* strNum = @"10";int n = [strNum intValue];
2 NSMutableStringNSString的子類
NSMutableString* ms1 = [NSMutableString stringWithFormat:@"my age is 10"];// 添加字串[ms1 appendString:@" or 13"];// 無傳回值,修改ms1本身的值// 刪除字串NSRange rg = [s1 rangOfString:@"age"];[ms1 deleteCharactersInRange:rg];NSString* ns1 = @"my age ";NSString* ns2 = [ns1 stringByAppendingString:@"is 10"];// 返回新的字串,不修改ns1的值
二 集合類
1 NSArray/NSMutableArray數組
OC數組只能存放OC對象,不能存放基礎資料類型和nil
NSArray:不可變數組NSMutableArray:可變數組// 數組建立NSArray* arr1 = [NSArray array];NSArray* arr2 = [arrayWithObject:@"name"];// nil 數組元素結束標誌NSArray* arr3 = [arrayWithObjects:@"name", @"id", nil];NSArray* arr4 = @[@"one",@"two",@"three",@"four"];// 數組元素個數unsigned long count1 = [arr3 count];unsigned long count2 = arr3.count;// 數組元素訪問NSString* e1 = [arr3 objectAtIndex:1];NSString* e2 = arr3[0];// 數組遍曆for (int i = 0; i < arr4.count; ++i){arr4[i];}for (id obj in arr4){obj; [arr4 indexOfObject:obj];// 擷取改數組的下標}[arr4 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){if (idx == 1){*stop = YES;// 跳出遍曆}}];// 可變數組NSMutableArray* marr = [[NSMutableArray alloc] init];;[marr addObject:@"one"];[marr addObject:@"two"];[marr addObject:@"three"];// 可變數組-刪除[marr removeObject:@"one"];[marr removeObjectAtIndex:0];[marr removeAllObjects];
2 NSSet/NSMutableSet集合
只能存放OC對象,不能存放基礎資料類型和存放nil
NSSet* st1 = [NSSet set];NSSet* st2 = [NSSet setWithObject:@"one"];NSSet* st3 = [NSSet setWithObjects:@"one", @"two", nil];// 隨機拿出一個對象NSString* ssobj = [st3 anyObject];NSMutableSet* mst = [NSMutableSet set];[mst addObject:@"three"];// 刪除[mst removeObject:@"one"];[mst removeAllObjects];3 NSDictionary/NSMutableDictionary字典// 建立NSArray* keys = @[@"name", @"address"];NSArray* objs = @[@"jun", @"china"];NSDictionary* dict1 = [NSDictionary dictionaryWithObjests:objs forKeys:keys];NSDictionary* dict2 = [NSDictionary dictionaryWithObjestsAndKeys:@"name",@"jun",@"address",@"china", nil];NSDictionary* dict3 = @{@"name":@"jun",@"address":@"china"};// 可變字典NSMutableDictionary* msdict = [NSDictionary dictionary];// 添加元素[dict1 setObject:@"jun" forKey:@"name"];[dict1 setObject:@"hanfeng" forKey:@"name"];// 覆蓋之前的值// 移除[dict1 removeObjectForKey:@"name"];// 訪問id obj1 = [dict1 objectForKey:@"name"];id obj2 = dict1{@"name"}// 擷取索引值對的個數int count = dict1.count// 擷取所有的key值,然後遍曆這個數組NSArray* ks = [dict1 allKeys]for (int i = 0; i < dict1.count; ++i){id k = keys[i];id v = dict1[k];//[dict1 objectForKey:k];}[dict1 enumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop){// key ,obj}];
上面的三個類(NSArray, NSSet, NSDictionary)稱之為集合類
NSArray/NSMutableArray
* 有序
* 快速建立:@[obj1,obj2,...]
* 快速存取:數組名[i]
NSSet/NSMutableSet
* 無序
NSDictionary/NSMutableDictionary
* 無序
* 快速建立:@{key1:value1,key2:value2,...}
* 快速存取:字典名{key}
三 NSValue/NSNumber數字
1 NSValue
將其他資料類型(常見結構體)封裝成OC對象, 這樣就可以放入OC數組和字典中了。
CGPoint p = CGPointMake(10, 10);NSValue* v = [NSValue valueWithPoint:p]CGPoint pt = [v pointValue];
2 NSNumber
將基礎資料類型(數字)裝成對象,這樣就可以放入OC數組和字典中了。
NSNumber num1 = [NSNumber numberWithInt:10];NSNumber num2 = [NSNumber numberWithLong:10];NSNumber num3 = [NSNumber numberWithBool:true];NSNumber num4 = [NSNumber numberWithFloat:10.0];NSNumber num4 = @10;// 快速轉換int count = 5;NSNumber num5 = @(count);...int i = [num1 intValue];int l = [num1 longValue];int b = [num1 boolValue];int f = [num1 floatValue];...
四 NSDate日期
<pre name="code" class="objc">NSDate* d1 = [NSDate date];// 返回目前時間NSLog(@"%@", d1);// 格林尼治時間時間(0時區),跟北京(東八區)時間差8個小時NSDate* d2 = [NSDate dateWithTimeInterval:5 sinceDate:d1];// 時間間隔NSTimeInterval sec1 = [d2 timeIntervalSince1970];NSTimeInterval sec2 = [d2 timeIntervalSinceNow];// 日期格式化NSDateFormatter* f1 = [[NSDateFormatter alloc]init];// y年M月d日h(12小時制)時H(24小時制)時m分s秒f1.dateFormat = @"yyyy-MM-dd HH:mm:ss";NSString* dateStr = [f1 stringFromDate:d1];NSString* timeStr = @"2015/02/04 16:57";NSDateFormatter* f2 = [[NSDateFormatter alloc]init];f2.dateFormat = @"yyyy/MM/dd HH:mm";NSDate* d3 = [f2 dateFromString:timeStr];