天天看點

iOS Foundation架構常用類結構的常用用法

慕課網 https://www.imooc.com/learn/420 學習筆記

iOS的Foundation架構常用類結構的常用用法

1.NSString(不可變字元串)

1.oc和C語言字元串之間的類型轉換

char *s=“This is a string of c”;//c語言子串以字元數組的方式存儲

NSString *str=@“This is a string of oc”;//oc字元串為NSString類型,雙引号前有@号表示這是一個NSString類型

//字元串 c轉到oc

NSString *str1=[NSString stringWithUTF8String: s].

//字元串 oc轉c

char * s2=[str UTF8String];

2.建立字元串

//下面建立的字元串,不需要自己釋放記憶體

NSString *str3=@“IOS”

//下面建立的字元串,需要自己釋放記憶體

NSString * str4=[[NSString alloc] init];

str4=@“IOS”

3.格式化字元串

int a=10;

int b=20;

//以下str5為 @“a10 b=10”

NSString *str5=[NSString stringWithFormat:@“a=%d b=%d”,a,b];

4.拼接字元串

//将str3拼接在str6d的後面

NSString *str6=[str stringByAppendString:str3];

5.大小寫轉換

NSString *str7=@“aBCDef”

//轉換為小寫,str8為@“abcdef”:

NSString *str8=[str7 lowercaseString];

//轉換為大寫,str9為@“ABCDEF”:

NSString *str8=[str7 uppercaseString];

6.字首字尾的判斷

NSString *str10=@“www.imooc.com”;

//判斷字首,以下hasPrefix為YES

BOOL hasPrefix=[str10 hasPrefix:@“www.”];

//判斷字尾,以下hasSuffix為YES

BOOL hasSuffix=[str10 hasPrefix:@".com"];

7.兩字元串是否相同

NSString * str11=@“hello”;

NSString * str12=@“hello”;

//判斷str11是否與str12一緻,isEqual為YES

BOOL isEqual=[str11 isEqualToString:str12];

8.比較字元串

使用NSComparisionResult,它有三個結果NSOrderedAscending,NSOrderedSame,NSOrderedDescending,分别是大于等于小于。

9.分割字元串

//按照指定字元分割字元串 ,以下以逗号分割

NSString * str13=@“a,b,c,d,e,f,g”;

NSArray *strArray=[str13 componentsSeparatedByString:@","];

10.截取字元串

//按照範圍截取字元串

NSRange range=NSMakeRange(1,5);//設定一個範圍

NSString *str14=[str13 substringWithRange:range];//截取範圍内的字元串,str14為@",b,c,"

//從某一位開始截取後面的字元串

NSString *str15=[str13 substringFromIndex:2];//從下标2開始将其後所有字元都截取。str15為@“b,c,d,e,f,g”

//從某位向前截取所有字元

NSString *str16=[str13 substringToIndex:7];//截取下标7之前(不包括7)的字元。str16為@“a,b,c,d”

//字元串取指定位置字元

char c=[str13 characterAtIndex:0];取下标為0的字元,c為’a’

11.字元串長度

int len=[str13 length];//len為str13的長度,為13

12.字元串查找

NSString *str17=@“ab cd ef gh ij ab”

//查找指定字元串的位置,range1.location為0,range1.length為2

NSRange range1=[str17 rangeOfString:@“ab”];//隻查出第一個字元串出現的位置

13.字元串替換

NSString *str18=@“Hello iOS,Hello imooc”;

//替換某一個範圍的内容

NSString *str19=[str18 stringByReplacingCharactersInRange:NSMakeRange(0,5) withString:@“你好”];

//str19為@“你好 iOS,Hello imooc”

//用指定字元串替換源字元串的子串

str20=[str18 stringByReplacingOccurrencesOfString:@"Hello"withString: @“你好”];

//str20為@“你好 iOS.你好 imooc”

14.讀取檔案

檔案來源1.本地 檔案 2.網絡檔案

NSString *str21=“www.baidu.com”;

//網絡路徑

NSURL *httpURL=[NSURL URLWithString:str21];

//本地路徑

NSURL *fileURL=[NSURL fileURLWithPath:str21];

//讀取網絡檔案

NSString *httpStr=[NSString stringWithContentsOfURL:httpURL encoding: NSUTF8StringEncoding error:nil];

//讀本地檔案

NSString *fileStr=[NSString stringWithContentsOfFile: @"這裡是檔案路徑"encoding: NSUTF8StringEncoding error:nil];

//寫入檔案

NSString * str22=@“Hello Visitor”;

BOOL isOK=[str22 writeToFile:@“這裡寫檔案位址” atomically:YES encoding:NSUTF8Encoding error:nil];

//寫入檔案成功則isOK為YES

2.NSMutableString (可變字元串)

它是NSString的子類,可以使用所有NSString的方法,并且擁有一些自己特有的方法:

1.執行個體化

//這裡預先給NSMutableString 設定容量為10,超過時會自行擴容。這樣可以提升性能

NSMutableString *str=[[NSMutableString alloc] initWithCapacity:10];

2.指派

[str setString:@“Hello”];

3.追加字元串(在原有字元串後追加)

[str appendString:@“world”] //現在str為@“Hello world”

//格式化追加

int a=10;

[str appendFormat:@" %d",] //現在str為@“Hello world 10”

4.替換字元串

NSRange range=[str rangeOfString:@“world”];

[str replaceCharactersInRange:range withString:@“iOS”];

//現在str為@“Hello iOS10”

5.插入字元串

[str insertString:@“A” atIndex:6];

//現在str為@“Hello AiOS10”

6.删除字元串

NSRange range1=[str rangeOfString:@“AiOS”];

[str deleteCharactersInRange range1]

//現在str為@“Hello 10”

3.NSArray (不可變數組)

oc數組可以存儲不同類型的對象,但隻能存儲對象。數組的記憶體空間是連續的,它實際存儲的是對象的指針,占用空間較小,數組最後以nil結尾

1.執行個體化

NSArray *array1=[[NSArray alloc]initWithObject:@“1”, @“2”, @“3”, @“4”,@“5”,nil];

2.數組的長度

int len=(int)array1.count;//len為為5

3.判斷數組是否包含對應對象

BOOL isHaveOne=[array1 containsObject:@“1”];//isHaveOne為YES

4.擷取數組中指定位置的元素

NSString *strLast=[array1 lastObject];//取得數組最後一個元素

NSString *strFirst=[array1 firstObject];//取得數組第一個元素

NSString *strThird=[array1 objectAtIndex:3];//取得數組下标為3的元素

5.查找對象下标(如果沒有該元素,傳回-1)

int index=[array1 indexOfObject:@“3”];//index為2

6.數組的周遊

//基本for循環通過下标逐一取出

for(int i=0;i<array1.count;i++){

NSString *str=[array1 objectAtIndex:i];

//對str進行操作

}

//快速枚舉(要求數組中的元素類型都是一緻的)

for(NSString *str2 in array1){

//對str2進行操作

}

4.NSMutableArray (可變數組)

擁有NSArray所有特性,并添加自己的可特性

1.執行個體化

NSMutableArray *array= [[NSMutableArray alloc]init];

2.添加元素

//添加單個元素

NSString *str1=@“p1”;

NSString *str2=@“p2”;

[array addObject:str1 ];//将str1添加進array

//添加一個數組裡的所有元素

[array addObjectsFromArray:array1];

3.删除元素

[array removeAllObjects];//删除數組中的所有元素

[array removeLastObject];//删除最後一個元素

[array removeObject:str1];//删除指定元素

[array removeObjectAtIndex:0];//删除指定下标元素

4.交換元素位置

[array exchangeObjectAtIndex:0 withObjectAtIndex:1];//交換夏标為0和1 元素

5.NSDictionary 字典

字典的存儲記憶體不是連續的,用key和value進行對應(鍵值對的方式)

1.初始化

NSDictionary *dict1=[NSDictionary dictionaryWithObject:@“1” forKey:@“a” ];

//上面的方法不常用,一般用下面的方式建立,參數分别是是鍵和值的數組

NSDictionary *dict2=[NSDictionary dictionaryWithObjects:

[NSArray arrayWithObjects:@“1”,@“2”,@“3”,nil]

forKeys:[NSArray arrayWithObjects:@“a”,@“b”,@“c”,nil] ];

//簡便寫法

NSDictionary *dict3=@{@"1":@"a",@"2":@"b",@"3":@"c"};

2.鍵值對數量

int count=(int)[dict2 count];

3.根據鍵取得值

NSString *value=[dict2 valueForKey:@“b”];//value為@“2”

NSString *value2=[dict2 objectForKey:@“b”];//value2為@“2”

NSArray * allValues=[dict allValues];//所有字典中的值組成的數組

NSArray * allKeys=[dict allKeys];//所有字典中的鍵組成的數組

4.周遊字典

所有周遊都是根據鍵周遊

for(NSString * key in dict2){

NSString *value=[dict2 objectForKey:key];

}

//疊代器,枚舉器

NSEnumerator *en =[dict2 keyEnumerator];

id key=nil;

while(key=[en nextObject]){

NSString *value=[key objectForKey:key];

6.NSMutableDictionary 可變字典

1.初始化

NSMutableDictionary * dict=[[NSMutableDictionary alloc] init];

2.添加(修改)鍵值對

[dict setObject:@“1” forKey:@“a”];

[dict setObject:@“2” forKey:@“b”];

3.删除鍵值對

[dict removeObjectForKey:@“b”];//删除對應鍵的鍵值對

[dict removeObjectsForKeys:array];//删除數組array中元素的對應鍵

[dict removeAllObject];//全部删除