Ios App开发宝典.docx

上传人:b****5 文档编号:8004558 上传时间:2023-01-27 格式:DOCX 页数:13 大小:18.80KB
下载 相关 举报
Ios App开发宝典.docx_第1页
第1页 / 共13页
Ios App开发宝典.docx_第2页
第2页 / 共13页
Ios App开发宝典.docx_第3页
第3页 / 共13页
Ios App开发宝典.docx_第4页
第4页 / 共13页
Ios App开发宝典.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

Ios App开发宝典.docx

《Ios App开发宝典.docx》由会员分享,可在线阅读,更多相关《Ios App开发宝典.docx(13页珍藏版)》请在冰豆网上搜索。

Ios App开发宝典.docx

IosApp开发宝典

iosapp开发宝典:

String用法大全

一、NSString

创建字符串。

NSString*astring=@"ThisisaString!

";

创建空字符串,给予赋值。

NSString*astring=[[NSStringalloc]init];

astring=@"ThisisaString!

";

NSLog(@"astring:

%@",astring);

stringrelease];

使用变量初始化

NSString*name=@"Ivan!

";

NSString*astring=[[NSStringstringWithFormat:

@”Mynameis%@!

”,name]];

NSLog(@"astring:

%@",astring);

判断是否包含某字符串

检查字符串是否以另一个字符串开头

-(BOOL)hasPrefix:

(NSString*)aString;

NSString*String1=@"NSStringInformation.txt";

[String1hasPrefix:

@"NSString"]==1?

NSLog(@"YES"):

NSLog(@"NO");

[String1hasSuffix:

@".txt"]==1?

NSLog(@"YES"):

NSLog(@"NO");

是否包含其它字符

NSString*astring=[[NSStringalloc]initWithString:

@"ThisisaString!

"];

Booleancontains=[astringrangeOfString:

@”This”].length>0;

从文件读取字符串:

initWithContentsOfFile方法

NSString*path=@"astring.text";

NSString*astring=[[NSStringalloc]initWithContentsOfFile:

path];

NSLog(@"astring:

%@",astring);

[astringrelease];

写字符串到文件:

writeToFile方法

NSString*astring=[[NSStringalloc]initWithString:

@"ThisisaString!

"];

NSLog(@"astring:

%@",astring);

NSString*path=@"astring.text";

[astringwriteToFile:

pathatomically:

YES];

[astringrelease];

比较两个字符串

isEqualToString方法

NSString*astring01=@"ThisisaString!

";

NSString*astring02=@"ThisisaString!

";

BOOLresult=[astring01isEqualToString:

astring02];

NSLog(@"result:

%d",result);

compare方法(comparer返回的三种值)

NSString*astring01=@"ThisisaString!

";

NSString*astring02=@"ThisisaString!

";

BOOLresult=[astring01compare:

astring02]==NSOrderedSame;

NSLog(@"result:

%d",result);

NSOrderedSame判断两者内容是否相同

NSString*astring01=@"ThisisaString!

";

NSString*astring02=@"thisisaString!

";

BOOLresult=[astring01compare:

astring02]==NSOrderedAscending;

NSLog(@"result:

%d",result);

不考虑大小写比较字符串1

NSString*astring01=@"thisisaString!

";

NSString*astring02=@"ThisisaString!

";

BOOLresult=[astring01caseInsensitiveCompare:

astring02]==NSOrderedSame;

NSLog(@"result:

%d",result);

改变字符串的大小写

NSString*string1=@"AString";

NSString*string2=@"String";

NSLog(@"string1:

%@",[string1uppercaseString]);//大写

NSLog(@"string2:

%@",[string2lowercaseString]);//小写

NSLog(@"string2:

%@",[string2capitalizedString]);//首字母大小

在串中搜索子串

NSString*string1=@"Thisisastring";

NSString*string2=@"string";

NSRangerange=[string1rangeOfString:

string2];

intlocation=range.location;

intleight=range.length;

NSString*astring=[[NSStringalloc]initWithString:

[NSStringstringWithFormat:

@"Location:

%i,Leight:

%i",location,leight]];

NSLog(@"astring:

%@",astring);

[astringrelease];

替换字符串

NSString*astring01=@"hello中国";

NSString*new=[astring01stringByReplacingOccurrencesOfString:

@”中国”withString:

@"北京"];

NSLog(new);

分割字符串成数组

NSString*s=@"abdef";

NSArray*arr=[scomponentsSeparatedByString:

@""];

NSLog(@"count=%d",[arrcount]);

字符串数组拼接成字符串

NSArray*pathArray=[NSArrayarrayWithObjects:

@"here",

@"be",@"dragons",nil];

NSLog(@"%@",[pathArraycomponentsJoinedByString:

@""]);

抽取子串

-substringToIndex:

从字符串的开头一直截取到指定的位置,但不包括该位置的字符

NSString*string1=@"Thisisastring";

NSString*string2=[string1substringToIndex:

3];

NSLog(@"string2:

%@",string2);

-substringFromIndex:

以指定位置开始(包括指定位置的字符),并包括之后的全部字符

NSString*string1=@"Thisisastring";

NSString*string2=[string1substringFromIndex:

3];

NSLog(@"string2:

%@",string2);

-substringWithRange:

//按照所给出的位置,长度,任意地从字符串中截取子串

NSString*string1=@"Thisisastring";

NSString*string2=[string1substringWithRange:

NSMakeRange(0,4)];

NSLog(@"string2:

%@",string2);

二、NSMutableString

给字符串分配容量

//stringWithCapacity:

NSMutableString*String;

String=[NSMutableStringstringWithCapacity:

40];

在已有字符串后面添加字符

//appendString:

andappendFormat:

NSMutableString*String1=[[NSMutableStringalloc]initWithString:

@"ThisisaNSMutableString"];

//[String1appendString:

@",Iwillbeaddingsomecharacter"];

[String1appendFormat:

[NSStringstringWithFormat:

@",Iwillbeaddingsomecharacter"]];

NSLog(@"String1:

%@",String1);

*/

在已有字符串中按照所给出范围和长度删除字符

//deleteCharactersInRange:

NSMutableString*String1=[[NSMutableStringalloc]initWithString:

@"ThisisaNSMutableString"];

[String1deleteCharactersInRange:

NSMakeRange(0,5)];

NSLog(@"String1:

%@",String1);

在已有字符串后面在所指定的位置中插入给出的字符串

//-insertString:

atIndex:

NSMutableString*String1=[[NSMutableStringalloc]initWithString:

@"ThisisaNSMutableString"];

[String1insertString:

@"Hi!

"atIndex:

0];

NSLog(@"String1:

%@",String1);

将已有的换成其它的字符串

//-setString:

NSMutableString*String1=[[NSMutableStringalloc]initWithString:

@"ThisisaNSMutableString"];

[String1setString:

@"HelloWord!

"];

NSLog(@"String1:

%@",String1);

按照所给出的范围,和字符串替换的原有的字符

//-setString:

NSMutableString*String1=[[NSMutableStringalloc]initWithString:

@"ThisisaNSMutableString"];

[String1replaceCharactersInRange:

NSMakeRange(0,4)withString:

@"That"];

NSLog(@"String1:

%@",String1);

三、NSArray

创建数组

NSArray*array=[[NSArrayalloc]initWithObjects:

@"One",@"Two",@"Three",@"Four",nil];

self.dataArray=array;

[arrayrelease];

//-(unsigned)Count;数组所包含对象个数;

NSLog(@"self.dataArraycound:

%d",[self.dataArraycount]);

//获取指定索引处的对象

NSLog(@"self.dataArraycound2:

%@",[self.dataArrayobjectAtIndex:

2]);

从一个数组拷贝数据到另一数组

//arrayWithArray:

//NSArray*array1=[[NSArrayalloc]init];

NSMutableArray*MutableArray=[[NSMutableArrayalloc]init];

NSArray*array=[NSArrayarrayWithObjects:

@"a",@"b",@"c",nil];

NSLog(@"array:

%@",array);

MutableArray=[NSMutableArrayarrayWithArray:

array];

NSLog(@"MutableArray:

%@",MutableArray);

array1=[NSArrayarrayWithArray:

array];

NSLog(@"array1:

%@",array1);

//Copy

//idobj;

NSMutableArray*newArray=[[NSMutableArrayalloc]init];

NSArray*oldArray=[NSArrayarrayWithObjects:

@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

NSLog(@"oldArray:

%@",oldArray);

for(inti=0;i<[oldArraycount];i++)

{

obj=[[oldArrayobjectAtIndex:

i]copy];

[newArrayaddObject:

obj];

}

//

NSLog(@"newArray:

%@",newArray);

[newArrayrelease];

快速遍历数组

//NSMutableArray*newArray=[[NSMutableArrayalloc]init];

NSArray*oldArray=[NSArrayarrayWithObjects:

@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

NSLog(@"oldArray:

%@",oldArray);

for(idobjinoldArray)

{

[newArrayaddObject:

obj];

}

//

NSLog(@"newArray:

%@",newArray);

[newArrayrelease];

Copyandsort

//NSMutableArray*newArray=[[NSMutableArrayalloc]init];

NSArray*oldArray=[NSArrayarrayWithObjects:

@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];

NSLog(@"oldArray:

%@",oldArray);

NSEnumerator*enumerator;

enumerator=[oldArrayobjectEnumerator];

idobj;

while(obj=[enumeratornextObject])

{

[newArrayaddObject:

obj];

}

[newArraysortUsingSelector:

@selector(compare:

)];

NSLog(@"newArray:

%@",newArray);

[newArrayrelease];

NSMutableArray

给数组分配容量

//NSArray*array;

array=[NSMutableArrayarrayWithCapacity:

20];

在数组末尾添加对象

//-(void)addObject:

(id)anObject;

//NSMutableArray*array=[NSMutableArrayarrayWithObjects:

@"One",@"Two",@"Three",nil];

[arrayaddObject:

@"Four"];

NSLog(@"array:

%@",array);

删除数组中指定索引处对象

//-(void)removeObjectAtIndex:

(unsigned)index;

//NSMutableArray*array=[NSMutableArrayarrayWithObjects:

@"One",@"Two",@"Three",nil];

[arrayremoveObjectAtIndex:

1];

NSLog(@"array:

%@",array);

数组枚举

从前向后

//NSMutableArray*array=[NSMutableArrayarrayWithObjects:

@"One",@"Two",@"Three",nil];

NSEnumerator*enumerator;

enumerator=[arrayobjectEnumerator];

idthingie;

while(thingie=[enumeratornextObject]){

NSLog(@"thingie:

%@",thingie);

}

从后向前

NSMutableArray*array=[NSMutableArrayarrayWithObjects:

@"One",@"Two",@"Three",nil];

NSEnumerator*enumerator;

enumerator=[arrayreverseObjectEnumerator];

idobject;

while(object=[enumeratornextObject]){

NSLog(@"object:

%@",object);

}

快速枚举

//NSMutableArray*array=[NSMutableArrayarrayWithObjects:

@"One",@"Two",@"Three",nil];

for(NSString*stringinarray)

{

NSLog(@"string:

%@",string);

}

NSDictionary

创建字典

//-(id)initWithObjectsAndKeys;

ctionary*dictionary=[[NSDictionaryalloc]initWithObjectsAndKeys:

@"One",@"1",@"Two",@"2",@"Three",@"3",nil];

NSString*string=[dictionaryobjectForKey:

@"One"];

NSLog(@"string:

%@",string);

NSLog(@"dictionary:

%@",dictionary);

[dictionaryrelease];

练习题:

将文本“成绩单.text”内容加载到内存中并按规定的格式输出出来

NSMutableDictionary

创建

NSMutableDictionary*dictionary=[NSMutableDictionarydictionary];

添加字典

[dictionarysetObject:

@"One"forKey:

@"1"];

[dictionarysetObject:

@"Two"forKey:

@"2"];

[dictionarysetObject:

@"Three"forKey:

@"3"];

[dictionarysetObject:

@"Four"forKey:

@"4"];

NSLog(@"dictionary:

%@",dictionary);

删除指定的字典

[dictionaryremoveObjectForKey:

@"3"];

NSLog(@"dictionary:

%@",dictionary);

NSValue(对任何对象进行包装)

将NSRect放入NSArray中

NSMutableArray*array=[[NSMutableArrayalloc]init];

NSValue*value;

CGRectrect=CGRectMake(0,0,320,480);

value=[NSValuevalueWithBytes:

&rectobjCType:

@encode(CGRect)];

[arrayaddObject:

value];

NSLog(@"array:

%@",array);

从Array中提取

value=[arrayobjectAtIndex:

0];

[valuegetValue:

&rect];

NSLog(@"value:

%@",value);

定义结构体并添加到NSArray里面

typedefstruct{

floatreal;

floatimaginary;

}ImaginaryNumber;

ImaginaryNumbermiNumber;

miNumber.real=1.1;

miNumber.imaginary=1.41;

NSValue*miValue=[NSValuevalue:

miNumber

withObjCType:

@encode(ImaginaryNumber)];//encodeusingthetypename

ImaginaryNumbermiNumber2;

[miValuegetValue:

&miNumber2];

四、宏定义#define讲解

NSNumber

+(NSNumber*)numberWithInt:

(int)value;

+(NSNumber*)numberWithDouble:

(double)value;

-(int)intVal

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1