版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
ObjectiveC基礎(chǔ)庫(kù)介紹NSString創(chuàng)建字符串常見(jiàn)方法3NSString*astring=@"ThisisaString!";NSString*astring=[[NSStringalloc]initWithString:@"ThisisaString!"];
NSString
*astring
=
[NSString
stringWithFormat:@"我的名字:%@
我的年齡:%d
我的郵箱:%s",@“XiaoMing",
25,“"];
NSString
*astring
=
[NSString
stringWithUTF8String:"字符串轉(zhuǎn)換"];
4合并兩個(gè)字符串int
i
=
100;
char*c
=
“chinasofti";
NSString
*temp
=
@"我是臨時(shí)字符串";
NSString
*str1
=
[temp
stringByAppendingFormat:@"整型:
%d
字符型
:%s",i,c];
NSString
*str2
=
[temp
stringByAppendingString:temp];
得到字符串長(zhǎng)度5NSString*astring=@”HelloWorld”;NSIntegernum=[astringlength]NSLog(@”%d”,num);num=[astringlengthOfBytesUsingEncoding:NSUnicodeStringEncoding];NSLog(@”%d”,num);字符串比較6NSString*str1=@"ThisisaString!";NSString*str2=@"ThisisaString!";BOOLret=[str1isEqualToString:str2];if(ret){ NSLog(@”str1和str2字符串內(nèi)容相同”);}else{ NSLog(@”str1和str2字符串內(nèi)容不相同”);}注意,判斷兩個(gè)字符串內(nèi)容是否相同,一定不可以這樣判斷:str1==str27NSString*str1=@"ThisisaString!";NSString*str2=@"Thisisastring!";NSComparisonResultret=[str1compare:str2]if(NSOrderedAscending==ret){ NSLog(@”str1小于str2”);}elseif(NSOrderedSame==ret){ NSLog(@”str1等于str2”);}elseif(NSOrderedDescending==ret){ NSLog(@”str1大于str2”);}8NSString*str0=@“icon_background.jpg“;
//字符串以開(kāi)頭比較
if([str0hasPrefix:@“icon"]){NSLog(@"str0字符串以icon開(kāi)頭");}
//字符串以結(jié)尾比較
if([str1hasSuffix:@“jpg]){NSLog(@"str0字符串以jpg結(jié)尾");}搜索字符串與替換字符串9NSString*str0=@"mynameisXiaoMing";NSString*temp=@"is";NSRangerang=[str0rangeOfString:temp];if(rang.location!=NSNotFound){ NSLog(@“{%d,%d}",rang.location,rang.length);}else{ NSLog(@”沒(méi)有找到%@”,temp);}
NSString*str=[str0stringByReplacingCharactersInRange:rangwithString:@"啦啦啦……"];NSLog(@"替換后字符串為%@",str);
字符串的截取與大小寫改變10NSString*str0=@"mynameisXiaoMing";
//截取字符串起始點(diǎn)到index為4之間的內(nèi)容
NSString*to=[str0substringToIndex:4];
NSLog(@"to=%@",to);
//截取字符index為2到結(jié)尾之間的內(nèi)容
NSString*from=[str0substringFromIndex:2];
NSLog(@"from=%@",from);
//設(shè)置截取字符串的范圍,從第二位到第十位
NSRangerang=NSMakeRange(2,10);NSString*strRang=[str0substringWithRange:rang];NSLog(@"rang=%@",strRang);
//設(shè)置字符串首字母大寫
NSLog(@"str0首字母大寫:%@",[str0capitalizedString]);//設(shè)置字符串全部?jī)?nèi)容為大寫
NSLog(@"str0大寫:%@",[str0uppercaseString]);//設(shè)置字符全部?jī)?nèi)容為小寫
NSLog(@"str0小寫:%@",[str0lowercaseString]);數(shù)值轉(zhuǎn)換doubleValuefloatValueintValueintegerValuelongLongValueboolValue11NSMutableString添加字符串13NSMutableString*str=[[NSMutableStringalloc]initWithString:@"Hello"];[strappendString:@"World!"];NSLog(@"str:%@",str);[strappendFormat:@"1+1=%d",1+1];NSLog(@"str:%@",str);[strrelease];NSMutableString是NSString的子類,它和NSString的主要區(qū)別在于可修改刪除、插入、替換14NSMutableString*str=[[NSMutableStringalloc]initWithString:@"ThisisaNSMutableString"];[strdeleteCharactersInRange:NSMakeRange(0,5)];NSLog(@"str:%@",str);[strrelease];NSMutableString*str=[[NSMutableStringalloc]initWithString:@"ThisisaNSMutableString"];[strinsertString:@"Hi!"atIndex:0];NSLog(@"str:%@",str);[strrelease];NSMutableString*str=[[NSMutableStringalloc]initWithString:@"ThisisaNSMutableString"];[strreplaceCharactersInRange:NSMakeRange(0,4)withString:@"That"];NSLog(@"str:%@",str);[strrelease];NSArray和NSMutableArrayNSArray數(shù)組只可以管理OC對(duì)象,不可以管理非OC對(duì)象1617[arraycount]:數(shù)組的長(zhǎng)度。
[arrayobjectAtIndex0]:傳入數(shù)組腳標(biāo)的id得到數(shù)據(jù)對(duì)象。
[arrayWithObjects;...]:向數(shù)組對(duì)象初始化賦值。這里可以寫任意對(duì)象的指針,結(jié)尾必須使用nil。#import<Foundation/Foundation.h>intmain(intargc,constchar*argv[]){@autoreleasepool{NSArray*array=[NSArrayarrayWithObjects:@"a",@”b”,@"c",nil];NSLog(@"arrayCount:%lu",[arraycount]);//遍歷數(shù)組
for(NSObject*objectinarray){NSLog(@"數(shù)組對(duì)象:%@",object);}}return0;}NSArray基本用法切割和合并18NSString*string=[[NSStringalloc]initWithString:@"One,Two,Three,Four"];NSLog(@"string:%@",string);NSArray*array=[stringcomponentsSeparatedByString:@","];NSLog(@"array:%@",array);[stringrelease];NSArray*array=[[NSArrayalloc]initWithObjects:@"One",@"Two",@"Three",@"Four",nil];NSString*string=[arraycomponentsJoinedByString:@","];NSLog(@"string:%@",string);NSMutableArray基本用法[NSMutableArrayarrayWithCapacity:0]:初始化可變數(shù)組對(duì)象的長(zhǎng)度,如果后面代碼繼續(xù)添加數(shù)組超過(guò)長(zhǎng)度0以后NSMutableArray的長(zhǎng)度會(huì)自動(dòng)擴(kuò)充,6是自己可以設(shè)置的顆粒度。
[arrayaddObject:...]:向可變數(shù)組尾部添加數(shù)據(jù)對(duì)象。1920#import<Foundation/Foundation.h>intmain(intargc,constchar*argv[]){@autoreleasepool{
NSMutableArray*muArray=[NSMutableArrayarrayWithCapacity:6];[muArrayaddObject:@"對(duì)象1"];[muArrayaddObject:@"對(duì)象2"];[muArrayaddObject:@"對(duì)象3"];[muArrayaddObject:@"對(duì)象4"];[muArrayinsertObject:@"攪局的"atIndex:2];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}}return0;}刪除對(duì)象[arrayremoveObject:(id)]:刪除數(shù)組中指定元素,根據(jù)對(duì)象isEqual消息判斷。[arrayremoveObjectIdenticalTo:(id)]:刪除數(shù)組中指定元素,根據(jù)對(duì)象的地址判斷[arrayremoveObjectIdenticalTo:(id)inRange:(NSRange)]:在指定范圍內(nèi)刪除指定的元素。[arrayremoveObjectAtIndex:(NSUInteger)]:刪除數(shù)組中指定腳標(biāo)索引的數(shù)據(jù)。[arrayremoveObjectsInArray:(NSArray*)]:刪除一個(gè)數(shù)組的元素。2122intmain(intargc,constchar*argv[]){@autoreleasepool{NSString*str1=[[NSStringalloc]init];NSString*str2=[[NSStringalloc]init];NSString*str3=[str1stringByAppendingFormat:@"字符串"];NSString*str4=[str2stringByAppendingFormat:@"字符串"];NSMutableArray*muArray=[NSMutableArrayarrayWithCapacity:6];[muArrayaddObject:@"對(duì)象"];[muArrayaddObject:str3];[muArrayaddObject:str4];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}if([str3isEqual:str4]){NSLog(@"str1isEqualstr2");}if(str3==str4){NSLog(@"str1==str2");}[muArrayremoveObject:str3];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}[str1release];[str2release];}return0;}23intmain(intargc,constchar*argv[]){@autoreleasepool{NSString*str1=[[NSStringalloc]init];NSString*str2=[[NSStringalloc]init];NSString*str3=[str1stringByAppendingFormat:@"字符串"];NSString*str4=[str2stringByAppendingFormat:@"字符串"];NSMutableArray*muArray=[NSMutableArrayarrayWithCapacity:6];[muArrayaddObject:@"對(duì)象"];[muArrayaddObject:str3];[muArrayaddObject:str4];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}if([str3isEqual:str4]){NSLog(@"str1isEqualstr2");}if(str3==str4){NSLog(@"str1==str2");}[muArrayremoveObjectIdenticalTo:str3];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}[str1release];[str2release];}return0;}替換24intmain(intargc,constchar*argv[]){@autoreleasepool{NSMutableArray*muArray=[NSMutableArrayarrayWithCapacity:6];[muArrayaddObject:@"對(duì)象1"];[muArrayaddObject:@"對(duì)象2"];[muArrayaddObject:@"對(duì)象3"];[muArrayaddObject:@"對(duì)象4"];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}[muArrayreplaceObjectAtIndex:2withObject:@"字符串替換回來(lái)"];for(NSObject*objectinmuArray){NSLog(@"數(shù)組對(duì)象:%@",object);}}return0;}NSDictionary和NSMutableDictionaryNSDictionary基本用法NSDictionary把數(shù)據(jù)以鍵值對(duì)的形式儲(chǔ)存起來(lái),取值的時(shí)候通過(guò)key就可以直接拿到對(duì)應(yīng)的值,方便快捷。和NSArray一樣,一個(gè)詞典對(duì)象也能保存不同類型的值,詞典也分別有不可變?cè)~典和可變的詞典(NSDictionary與NSMutableDictionary),前者是線程安全的,后者不是。26[NSDictionarydictionaryWithObjectsAndKeys:..]:使用鍵值對(duì)直接創(chuàng)建詞典對(duì)象,結(jié)尾必需使用nil標(biāo)志結(jié)束。[dictionarycount]:得到詞典的鍵值對(duì)數(shù)量。[dictionarykeyEnumerator]:將詞典的所有key儲(chǔ)存在NSEnumerator中,類似于Java語(yǔ)言中的迭代器[dictionaryobjectEnumerator]:將詞典的所有value儲(chǔ)存在NSEnumerator中[dictionaryobjectForKey:key]:通過(guò)傳入key對(duì)象可以拿到當(dāng)前key對(duì)應(yīng)儲(chǔ)存的值。2728intmain(intargc,constchar*argv[]){@autoreleasepool{NSDictionary*dictionary=[NSDictionarydictionaryWithObjectsAndKeys:@"25",@"age",@"張三",@"name",@"男",@"性別",nil];NSLog(@"%lu",[dictionarycount]);NSEnumerator*enumeratorKey=[dictionarykeyEnumerator];for(NSObject*objectinenumeratorKey){NSLog(@"key:%@",object);}
NSEnumerator*enumeratorObject=[dictionaryobjectEnumerator];for(NSObject*objectinenumeratorObject){NSLog(@"value:%@",object);}NSLog(@"keyname的值是:%@",[dictionaryobjectForKey:@"name"]);
}return0;}NSMutableDictionary基本用法[dictionarysetObject:forKey:]:向可變的詞典動(dòng)態(tài)的添加數(shù)據(jù)
[dictionaryremoveAllObjects..]:刪除掉詞典中的所有數(shù)據(jù)。
[dictionaryremoveObjectForKey..]:刪除掉詞典中指定key的數(shù)據(jù)2930intmain(intargc,constchar*argv[]){@autoreleasepool{NSMutableDictionary*dictionary=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"25",@"age",@"張三",@"name",@"男",@"性別",nil];[dictionarysetObject:@"30名"forKey:@"名次"];
NSLog(@"%lu",[dictionarycount]);NSEnumerator*enumeratorKey=[dictionarykeyEnumerator];for(NSObject*objectinenumeratorKey){NSLog(@"key:%@",object);}
NSEnumerator*enumeratorObject=[dictionaryobjectEnumerator];for(NSObject*objectinenumeratorObject){NSLog(@"value:%@",object);}NSLog(@"key名次的值是:%@",[dictionaryobjectForKey:@"名次"]);[dictionaryremoveObjectForKey:@"名次"];NSLog(@"%lu",[dictionarycount]);}return0;}NSSet和NSMutableSetNSSet基本用法NSSet和NSMutableSet是無(wú)序的,但是它保證數(shù)據(jù)的唯一性。當(dāng)插入相同的數(shù)據(jù)時(shí),不會(huì)有任何效果。從內(nèi)部實(shí)現(xiàn)來(lái)說(shuō)是hash表,所以可以常數(shù)時(shí)間內(nèi)查找一個(gè)數(shù)據(jù)。32[NSSetsetWithSet:(NSSet*)set];用另外一個(gè)set對(duì)象構(gòu)造[NSSetsetWithArray:(NSArray*)array];用數(shù)組構(gòu)造[NSSetsetWithObjects:...]:創(chuàng)建集合對(duì)象,并且初始化集合中的數(shù)值,結(jié)尾必需使用nil標(biāo)志。[setcount];得到這個(gè)結(jié)合對(duì)象的長(zhǎng)度。[setcontainsObject:...]:判斷這個(gè)集合中是否存在傳入的對(duì)象,返回BOOL值。[setobjectEnumerator]:將集合放入迭代器。[setisEqualToSet:objset]:判斷兩個(gè)集合是否完全相等,返回BOOL值。[setisSubsetOfSet:objset]:判斷集合中的所有數(shù)據(jù)是否都相等與objeset集合中,返回BOOL值。3334intmain(intargc,constchar*argv[]){@autoreleasepool{NSSet*set=[NSSetsetWithObjects:@"25",@"age",@"張三",@"name",@"男",nil];NSSet*set1=[NSSetsetWithObjects:@"25",@"age",@"張三",@"name",@"男",@"性別",nil];
NSLog(@"setcount:%lu",[setcount]);if([setcontainsObject:@"age"]){NSLog(@"set包含age");}if([setisEqualToSet:set1]){NSLog(@"set等于set1");}if([setisSubsetOfSet:set1]){NSLog(@"setisSubsetOfSetset1");}NSArray*array=[setallObjects];NSLog(@"array:%@",array);
NSEnumerator*enumerator=[setobjectEnumerator];for(NSObject*objectinenumerator){NSLog(@"set1里的對(duì)象:%@",object);}}return0;}NSMutableSet的基本使用[NSMutableSetsetWithCapacity:6]:創(chuàng)建可變集合對(duì)象,并且初始化長(zhǎng)度為6。[setaddObject:obj]:向集合中動(dòng)態(tài)的添加對(duì)象。[setremoveObject:obj]:刪除集合中的一個(gè)對(duì)象。[setremoveAllObjects]:刪除集合中的所有對(duì)象。[setunionSet:obj]:向集合中添加一個(gè)obj集合的所有數(shù)據(jù)。[setminusSet:obj]:向集合中刪除一個(gè)obj集合的所有數(shù)據(jù)。[setintersectSet:obj]:向集合中刪除一個(gè)不包含obj集合的所有數(shù)據(jù)。35intmain(intargc,constchar*argv[]){@autoreleasepool{NSMutableSet*muSet=[NSMutableSetsetWithCapacity:6];[muSetaddObject:@"對(duì)象1"];NSSet*set=[NSSetsetWithObjects:@"對(duì)象2",@"對(duì)象3",@"被企鵝咬了一口",nil];//添加set數(shù)據(jù)
[muSetunionSet:set];for(NSObject*objectinmuSet){NSLog(@"allnuSet:%@",object);}NSSet*set1=[NSSetsetWit
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025陜西省建筑安全員知識(shí)題庫(kù)及答案
- 2025海南省建筑安全員-A證考試題庫(kù)附答案
- 2025河南建筑安全員知識(shí)題庫(kù)附答案
- 《A期中沖刺復(fù)習(xí)》課件
- 下肢深靜脈血栓的形成
- 物質(zhì)的量完整課件
- 《醫(yī)院火災(zāi)培訓(xùn)課件》課件
- 房地產(chǎn)行業(yè)定期報(bào)告:鄭州出臺(tái)容積率新規(guī)一線新房成交環(huán)比與9.6
- 《技術(shù)必修》課件
- 單位管理制度展示合集職員管理篇十篇
- 脊柱四肢及肛門直腸檢查
- 頸椎病診治與康復(fù)指南2023年版
- 高中政治期末綜合檢測(cè)部編版選修1
- 鑄造基礎(chǔ)知識(shí)及常見(jiàn)鑄造缺陷簡(jiǎn)介課件
- 歷史(中職)PPT全套教學(xué)課件
- 藥物分離技術(shù)教材吳昊課后參考答案
- 我和外公的戰(zhàn)爭(zhēng)
- 浙人美2011版二年級(jí)美術(shù)上冊(cè)《淘氣堡》教案及教學(xué)反思
- 提高屋面防水合格率QC成果演示文稿
- 【招標(biāo)控制價(jià)編制研究文獻(xiàn)綜述(論文)4800字】
- 肝硬化護(hù)理教學(xué)查房
評(píng)論
0/150
提交評(píng)論