Objective-C部分語(yǔ)法_第1頁(yè)
Objective-C部分語(yǔ)法_第2頁(yè)
Objective-C部分語(yǔ)法_第3頁(yè)
Objective-C部分語(yǔ)法_第4頁(yè)
Objective-C部分語(yǔ)法_第5頁(yè)
已閱讀5頁(yè),還剩18頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、查找Subviews(Detecting Subviews我們可以通過(guò)循環(huán)來(lái)查找一個(gè)已經(jīng)存在的View。當(dāng)我們使用view的tag屬性的話(huà),就很方便實(shí)現(xiàn)Detect Subviews。for (UIImageView *anImage in self.view subviews if (anImage.tag = 1 / do somethingNSString NSarray 枚舉/* *NSString* */一、NSString/*-創(chuàng)建字符串的方法-*/1、創(chuàng)建常量字符串。NSString *astring = "This is a String!"/2、創(chuàng)建空字符

2、串,給予賦值。NSString *astring = NSString allocinit;astring = "This is a String!"astring release;NSLog("astring:%",astring;/3、在以上方法中,提升速度:initWithString方法NSString *astring = NSString alloc initWithString:"This is a String!"NSLog("astring:%",astring;astring release;/

3、4、用標(biāo)準(zhǔn)c創(chuàng)建字符串:initWithCString方法char *Cstring = "This is a String!"NSString *astring = NSString alloc initWithCString:Cstring;NSLog("astring:%",astring;astring release;/5、創(chuàng)建格式化字符串:占位符(由一個(gè)%加一個(gè)字符組成int i = 1;int j = 2;NSString *astring = NSString allocinitWithString:NSString stringWith

4、Format:"%d.This is %i string!",i,j;NSLog("astring:%",astring;astring release;/6、創(chuàng)建臨時(shí)字符串NSString *astring;astring = NSString stringWithCString:"This is a temporary string"NSLog("astring:%",astring;/*-從文件讀取字符串:initWithContentsOfFile方法-*/NSString *path = "as

5、tring.text"NSString *astring = NSString alloc initWithContentsOfFile:path;NSLog("astring:%",astring;astring release;/*-寫(xiě)字符串到文件:writeToFile方法-*/NSString *astring = NSString allocinitWithString:"This is a String!"NSLog("astring:%",astring;NSString *path = "astri

6、ng.text"astring writeToFile: path atomically: YES;astring release;/*-比較兩個(gè)字符串-*/用C比較:strcmp函數(shù)char string1 = "string!"char string2 = "string!"if(strcmp(string1, string2 = = 0NSLog("1"/isEqualToString方法NSString *astring01 = "This is a String!"NSString *astri

7、ng02 = "This is a String!"BOOL result = astring01 isEqualToString:astring02;NSLog("result:%d",result;/compare方法(comparer返回的三種值NSString *astring01 = "This is a String!"NSString *astring02 = "This is a String!"BOOL result = astring01 compare:astring02 = = NSOrde

8、redSame;NSLog("result:%d",result;/NSOrderedSame判斷兩者內(nèi)容是否相同NSString *astring01 = "This is a String!"NSString *astring02 = "this is a String!"BOOL result = astring01 compare:astring02 = = NSOrderedAscending;NSLog("result:%d",result;/NSOrderedAscending判斷兩對(duì)象值的大小(按字

9、母順序進(jìn)行比較,astring02大于astring01為真NSString *astring01 = "this is a String!"NSString *astring02 = "This is a String!"BOOL result = astring01 compare:astring02 = = NSOrderedDescending;NSLog("result:%d",result;/NSOrderedDescending判斷兩對(duì)象值的大小(按字母順序進(jìn)行比較,astr ing02小于astring01為真/不考慮

10、大小寫(xiě)比較字符串1NSString *astring01 = "this is a String!"NSString *astring02 = "This is a String!"BOOL result = astring01caseInsensitiveCompare:astring02 = = NSOrderedSame;NSLog("result:%d",result;/NSOrderedDescending判斷兩對(duì)象值的大小(按字母順序進(jìn)行比較,astr ing02小于astring01為真/不考慮大小寫(xiě)比較字符串2NSSt

11、ring *astring01 = "this is a String!"NSString *astring02 = "This is a String!"BOOL result = astring01 compare:astring02options:NSCaseInsensitiveSearch | NSNumericSearch = = NSOrderedSame;NSLog("result:%d",result;/NSCaseInsensitiveSearch:不區(qū)分大小寫(xiě)比較NSLiteralSearch:進(jìn)行完全比較,區(qū)分

12、大小寫(xiě)NSNumericSearch:比較字符串的字符個(gè)數(shù),而不是字符值。/*-改變字符串的大小寫(xiě)-*/NSString *string1 = "A String"NSString *string2 = "String"NSLog("string1:%",string1 uppercaseString;/大寫(xiě)NSLog("string2:%",string2 lowercaseString;/小寫(xiě)NSLog("string2:%",string2capitalizedString;/首字母大小/

13、*-在串中搜索子串-*/NSString *string1 = "This is a string"NSString *string2 = "string"NSRange range = string1 rangeOfString:string2;int location = range.location;int leight = range.length;NSString *astring = NSString allocinitWithString:NSStringstringWithFormat:"Location:%i,Leight:%

14、i",location,leight ;NSLog("astring:%",astring;astring release;/*-抽取子串 -*/-substringToIndex:從字符串的開(kāi)頭一直截取到指定的位置,但不包括該位置的字符NSString *string1 = "This is a string"NSString *string2 = string1 substringToIndex:3;NSLog("string2:%",string2;/-substringFromIndex:以指定位置開(kāi)始(包括指定位置

15、的字符,并包括之后的全部字符NSString *string1 = "This is a string"NSString *string2 = string1 substringFromIndex:3;NSLog("string2:%",string2;/-substringWithRange:/按照所給出的位置,長(zhǎng)度,任意地從字符串中截取子串NSString *string1 = "This is a string"NSString *string2 = string1 substringWithRange:NSMakeRange(

16、0, 4;NSLog("string2:%",string2;/擴(kuò)展路徑NSString *Path = "/NSData.txt"NSString *absolutePath = Path stringByExpandingTildeInPath;NSLog("absolutePath:%",absolutePath;NSLog("Path:%",absolutePath stringByAbbreviatingWithTildeInPath;/文件擴(kuò)展名NSString *Path = "/NSDat

17、a.txt"NSLog("Extension:%",Path pathExtension;/* *NSMutableString* */*-給字符串分配容量-*/stringWithCapacity:NSMutableString *String;String = NSMutableString stringWithCapacity:40;/*-在已有字符串后面添加字符-*/ /appendString: and appendFormat:NSMutableString *String1 = NSMutableString alloc initWithString

18、:"This is a NSMutableString"/String1 appendString:", I will be adding some character"String1 appendFormat:NSString stringWithFormat:", I will be adding some character"NSLog("String1:%",String1;*/*-在已有字符串中按照所給出范圍和長(zhǎng)度刪除字符-*/ /*/deleteCharactersInRange:NSMutableSt

19、ring *String1 = NSMutableString alloc initWithString:"This is a NSMutableString"String1 deleteCharactersInRange:NSMakeRange(0, 5; NSLog("String1:%",String1;/*-在已有字符串后面在所指定的位置中插入給出的字符串-* /-insertString: atIndex:NSMutableString *String1 = NSMutableString alloc initWithString:"

20、This is a NSMutableString"String1 insertString:"Hi! "atIndex:0;NSLog("String1:%",String1;/*-將已有的空符串換成其它的字符串-*/-setString:NSMutableString *String1 = NSMutableString alloc initWithString:"This is a NSMutableString"String1 setString:"Hello Word!"NSLog("

21、String1:%",String1;/*-按照所給出的范圍,和字符串替換的原有的字符-*/-setString:NSMutableString *String1 = NSMutableString alloc initWithString:"This is a NSMutableString"String1 replaceCharactersInRange:NSMakeRange(0, 4 withString:"That"NSLog("String1:%",String1;/*-判斷字符串內(nèi)是否還包含別的字符串(前綴,后

22、綴-*/01:檢查字符串是否以另一個(gè)字符串開(kāi)頭- (BOOL hasPrefix: (NSString * aString;NSString *String1 = "NSStringInformation.txt"String1 hasPrefix:"NSString"= = 1? NSLog("YES": NSLog("NO"String1 hasSuffix:".txt" = = 1 ? NSLog("YES" :NSLog("NO"/02:查找字符串

23、某處是否包含其它字符串 - (NSRange rangeOfString: (NSString *aString,這一點(diǎn)前面在串中搜索子串用到過(guò);/* *NSArray* */*-創(chuàng)建數(shù)組-*/NSArray *array = NSArray alloc initWithObjects: "One","Two","Three","Four",nil;self.dataArray = array;array release;/- (unsigned Count;數(shù)組所包含對(duì)象個(gè)數(shù);NSLog("self.d

24、ataArray cound:%d",self.dataArraycount;/- (id objectAtIndex: (unsigned intindex;獲取指定索引處的對(duì)象;NSLog("self.dataArray cound 2:%",self.dataArray objectAtIndex:2;/*-從一個(gè)數(shù)組拷貝數(shù)據(jù)到另一數(shù)組(可變數(shù)級(jí)-*/arrayWithArray:/NSArray *array1 = NSArray alloc init;NSMutableArray *MutableArray = NSMutableArray alloc

25、init;NSArray *array = NSArray arrayWithObjects:"a","b","c",nil;NSLog("array:%",array;MutableArray = NSMutableArray arrayWithArray:array;NSLog("MutableArray:%",MutableArray;array1 = NSArray arrayWithArray:array;NSLog("array1:%",array1;/Copy/

26、id obj;NSMutableArray *newArray = NSMutableArray alloc init;NSArray *oldArray = NSArray arrayWithObjects:"a","b","c","d","e","f","g","h",nil;NSLog("oldArray:%",oldArray;for(int i = 0; i < oldArray count; i+o

27、bj = oldArray objectAtIndex:icopy;newArray addObject: obj;/NSLog("newArray:%", newArray;newArray release;/快速枚舉/NSMutableArray *newArray = NSMutableArray alloc init;NSArray *oldArray = NSArray arrayWithObjects:"a","b","c","d","e","f&quo

28、t;,"g","h",nil;NSLog("oldArray:%",oldArray;for(id obj in oldArraynewArray addObject: obj;/NSLog("newArray:%", newArray;newArray release;/Deep copy/NSMutableArray *newArray = NSMutableArray alloc init;NSArray *oldArray = NSArray arrayWithObjects:"a",&

29、quot;b","c","d","e","f","g","h",nil;NSLog("oldArray:%",oldArray;newArray =(NSMutableArray*CFPropertyListCreateDeepCopy(kCFAllocato rDefault, (CFPropertyListRefoldArray, kCFPropertyListMutableContainers;NSLog("newArray:%&

30、quot;, newArray;newArray release;/Copy and sort/NSMutableArray *newArray = NSMutableArray alloc init;NSArray *oldArray = NSArray arrayWithObjects:"b","a","e","d","c","f","h","g",nil;NSLog("oldArray:%",oldArray;

31、NSEnumerator *enumerator;enumerator = oldArray objectEnumerator;id obj;while(obj = enumerator nextObjectnewArray addObject: obj;newArray sortUsingSelector:selector(compare:;NSLog("newArray:%", newArray;newArray release;/*-切分?jǐn)?shù)組-*/從字符串分割到數(shù)組- componentsSeparatedByString:NSString *string = NSS

32、tring allocinitWithString:"One,Two,Three,Four"NSLog("string:%",string;NSArray *array = stringcomponentsSeparatedByString:","NSLog("array:%",array;string release;/從數(shù)組合并元素到字符串- componentsJoinedByString:NSArray *array = NSArray allocinitWithObjects:"One"

33、;,"Two","Three","Four",nil;NSString *string = arraycomponentsJoinedByString:","NSLog("string:%",string;/* *NSMutableArray* */*-給數(shù)組分配容量-*/NSArray *array;array = NSMutableArray arrayWithCapacity:20;/*-在數(shù)組末尾添加對(duì)象-*/- (void addObject: (id anObject;/NSMuta

34、bleArray *array = NSMutableArray arrayWithObjects:"One","Two","Three",nil;array addObject:"Four"NSLog("array:%",array;/*-刪除數(shù)組中指定索引處對(duì)象-*/ /-(void removeObjectAtIndex: (unsigned index;/NSMutableArray *array = NSMutableArray arrayWithObjects:"One&q

35、uot;,"Two","Three",nil;array removeObjectAtIndex:1;NSLog("array:%",array;/*-數(shù)組枚舉-*/- (NSEnumerator *objectEnumerator;從前向后/NSMutableArray *array = NSMutableArray arrayWithObjects:"One","Two","Three",nil;NSEnumerator *enumerator;enumerator =

36、array objectEnumerator;id thingie;while(thingie = enumerator nextObjectNSLog("thingie:%",thingie;/- (NSEnumerator *reverseObjectEnumerator;從后向前/NSMutableArray *array = NSMutableArray arrayWithObjects:"One","Two","Three",nil;NSEnumerator *enumerator;enumerator

37、= array reverseObjectEnumerator;id object;while(object = enumerator nextObjectNSLog("object:%",object;/快速枚舉/NSMutableArray *array = NSMutableArray arrayWithObjects:"One","Two","Three",nil;for(NSString *string in arrayNSLog("string:%",string;/* *NSDic

38、tionary* */*-創(chuàng)建字典-*/- (id initWithObjectsAndKeys;/NSDictionary *dictionary = NSDictionary alloc initWithObjectsAndKeys:"One","1","Two","2","Three", "3",nil;NSString *string = dictionary objectForKey:"One"NSLog("string:%"

39、,string;NSLog("dictionary:%",dictionary;dictionary release;/* *NSMutableDictionary* */*-創(chuàng)建可變字典-*/創(chuàng)建NSMutableDictionary *dictionary = NSMutableDictionary dictionary;/添加字典dictionary setObject:"One"forKey:"1"dictionary setObject:"Two"forKey:"2"dictionar

40、y setObject:"Three"forKey:"3"dictionary setObject:"Four"forKey:"4"NSLog("dictionary:%",dictionary;/刪除指定的字典dictionary removeObjectForKey:"3"NSLog("dictionary:%",dictionary;/* *NSValue(對(duì)任何對(duì)象進(jìn)行包裝* */*-將NSRect放入NSArray中-*/將NSRect放入NSArray中NSMutableArray *array = NSMutableArray allocinit;NSValue *value;CGRect rect = CGRectMake(0, 0, 320, 480;value = NSValue valueWithBytes:&rectobjCType:encode(CGRect;array addObject:value;NSLog("array:%&

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論