iOS开发常用代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

iOS开发常用的代码%c一个单一的字符%d一个十进制整数%i一个整数%e,%f,%g一个浮点数%o一个八进制数%s一个字符串%x一个十六进制数%p一个指针%n一个等于读取字符数量的整数%u一个无符号整数%[]一个字符集%%一个精度符号//一、NSString/*----------------创建字符串的方法----------------*/1、创建常量字符串。NSString*astring=@ThisisaString!;2、创建空字符串,给予赋值。NSString*astring=[[NSStringalloc]init];astring=@ThisisaString!;NSLog(@astring:%@,astring);[astringrelease];3、在以上方法中,提升速度:initWithString方法NSString*astring=[[NSStringalloc]initWithString:@ThisisaString!];NSLog(@astring:%@,astring);[astringrelease];4、用标准c创建字符串:initWithCString方法char*Cstring=ThisisaString!;NSString*astring=[[NSStringalloc]initWithCString:Cstring];NSLog(@astring:%@,astring);[astringrelease];5、创建格式化字符串:占位符(由一个%加一个字符组成)inti=1;intj=2;NSString*astring=[[NSStringalloc]initWithString:[NSStringstringWithFormat:@%d.Thisis%istring!,i,j]];NSLog(@astring:%@,astring);[astringrelease];6、创建临时字符串NSString*astring;astring=[NSStringstringWithCString:Thisisatemporarystring];NSLog(@astring:%@,astring);/*----------------从文件读取字符串: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];/*----------------比较两个字符串----------------*/用C比较:strcmp函数charstring1[]=string!;charstring2[]=string!;if(strcmp(string1,string2)==0){NSLog(@1);}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);//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)NSString*astring01=@thisisaString!;NSString*astring02=@ThisisaString!;BOOLresult=[astring01compare:astring02]==NSOrderedDescending;NSLog(@result:%d,result);//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)不考虑大小写比较字符串1NSString*astring01=@thisisaString!;NSString*astring02=@ThisisaString!;BOOLresult=[astring01caseInsensitiveCompare:astring02]==NSOrderedSame;NSLog(@result:%d,result);//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)不考虑大小写比较字符串2NSString*astring01=@thisisaString!;NSString*astring02=@ThisisaString!;BOOLresult=[astring01compare:astring02options:NSCaseInsensitiveSearch|NSNumericSearch]==NSOrderedSame;NSLog(@result:%d,result);//NSCaseInsensitiveSearch:不区分大小写比较NSLiteralSearch:进行完全比较,区分大小写NSNumericSearch:比较字符串的字符个数,而不是字符值。/*----------------改变字符串的大小写----------------*/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];/*----------------抽取子串----------------*/-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);constchar*fieldValue=[valuecStringUsingEncoding:NSUTF8StringEncoding];constchar*fieldValue=[valueUTF8String];NSString转NSDataNSString*str=@kilonet;NSData*data=[strdataUsingEncoding:NSUTF8StringEncoding];Dateformat用法:-(NSString*)getDay:(NSDate*)d{NSString*s;NSDateFormatter*format=[[NSDateFormatteralloc]init];[formatsetDateFormat:@YYYY/MM/ddhh:mm:ss];s=[formatstringFromDate:d];[formatrelease];returns;}各地时区获取:NSDate*nowDate=[NSDatenew];NSDateFormatter*formatter=[[NSDateFormatteralloc]init];[formattersetDateFormat:@yyyy/MM/ddHH:mm:ss];//根据时区名字获取当前时间,如果该时区不存在,默认获取系统当前时区的时间//NSTimeZone*timeZone=[NSTimeZonetimeZoneWithName:@Europe/Andorra];//[formattersetTimeZone:timeZone];//获取所有的时区名字NSArray*array=[NSTimeZoneknownTimeZoneNames];//NSLog(@array:%@,array);//for循环//for(inti=0;i[arraycount];i++)//{//NSTimeZone*timeZone=[NSTimeZonetimeZoneWithName:[arrayobjectAtIndex:i]];//[formattersetTimeZone:timeZone];//NSString*locationTime=[formatterstringFromDate:nowDate];//NSLog(@时区名字:%@:时区当前时间:%@,[arrayobjectAtIndex:i],locationTime);////NSLog(

1 / 30
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功