iOS开发常用代码.docx
- 文档编号:5427649
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:20
- 大小:21.74KB
iOS开发常用代码.docx
《iOS开发常用代码.docx》由会员分享,可在线阅读,更多相关《iOS开发常用代码.docx(20页珍藏版)》请在冰豆网上搜索。
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为真)
不考虑大小写比较字符串1
NSString*astring01=@"thisisaString!
";
NSString*astring02=@"ThisisaString!
";
BOOLresult=[astring01caseInsensitiveCompare:
astring02]==NSOrderedSame;
NSLog(@"result:
%d",result);
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
不考虑大小写比较字符串2
NSString*astring01=@"thisisaString!
";
NSString*astring02=@"ThisisaString!
";
BOOLresult=[astring01compare:
astring02
options:
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转NSData
NSString*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(@"timezonenameis:
%@",[arrayobjectAtIndex:
i]);
//}
//快速枚举法
for(NSString*timeZoneNameinarray){
[formattersetTimeZone:
[NSTimeZonetimeZoneWithName:
timeZoneName]];
NSLog(@"%@,%@",timeZoneName,[formatterstringFromDate:
nowDate]);
}
[formatterrelease];
[nowDaterelease];
NSCalendar用法:
-(NSString*)getWeek:
(NSDate*)d{
NSCalendar*calendar=[[NSCalendaralloc]initWithCalendarIdentifier:
NSGregorianCalendar];
unsignedunits=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit;
NSDateComponents*components=[calendarcomponents:
unitsfromDate:
d];
[calendarrelease];
switch([componentsweekday]){
case2:
return@"Monday";
break;
case3:
return@"Tuesday";
break;
case4:
return@"Wednesday";
break;
case5:
return@"Thursday";
break;
case6:
return@"Friday";
break;
case7:
return@"Saturday";
break;
case1:
return@"Sunday";
break;
default:
return@"NoWeek";
break;
}
//用components,我们可以读取其他更多的数据。
}
4.用Get方式读取网络数据:
将网络数读取为字符串
-(NSString*)getDataByURL:
(NSString*)url{
return[[NSStringalloc]initWithData:
[NSDatadataWithContentsOfURL:
[NSURLURLWithString:
[urlstringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]]]encoding:
NSUTF8StringEncoding];
}
//读取网络图片
-(UIImage*)getImageByURL:
(NSString*)url{
return[[UIImagealloc]initWithData:
[NSDatadataWithContentsOfURL:
[NSURLURLWithString:
[urlstringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]]]];
}
多线程
[NSThreaddetachNewThreadSelector:
@selector(scheduleTask)toTarget:
selfwithObject:
nil];
-(void)scheduleTask{
//createapool
NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
//releasethepool;
[poolrelease];
}
//如果有参数,则这么使用:
[NSThreaddetachNewThreadSelector:
@selector(scheduleTask:
)toTarget:
selfwithObject:
[NSDatedate]];
-(void)scheduleTask:
(NSDate*)mdate{
//createapool
NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
//releasethepool;
[poolrelease];
}
//注意selector里有冒号。
//在线程里运行主线程里的方法
[selfperformSelectorOnMainThread:
@selector(moveToMain)withObject:
nilwaitUntilDone:
FALSE];
6.定时器NSTimer用法:
代码
//一个可以自动关闭的Alert窗口
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:
nil
message:
[@"一个可以自动关闭的Alert窗口"
delegate:
nil
cancelButtonTitle:
nil//NSLocalizedString(@"OK",@"OK")//取消任何按钮
otherButtonTitles:
nil];
//[alertsetBounds:
CGRectMake
(alert.bounds.origin.x,alert.bounds.origin.y,
alert.bounds.size.width,alert.bounds.size.height+30.0)];
[alertshow];
UIActivityIndicatorView*indicator=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:
UIActivityIndicatorViewStyleWhiteLarge];
//Adjusttheindicatorsoitisupafewpixelsfromthebottomofthealert
indicator.center=CGPointMake(alert.bounds.size.width/2,alert.bounds.size.height-40.0);
[indicatorstartAnimating];
[alertinsertSubview:
indicatoratIndex:
0];
[indicatorrelease];
[NSTimerscheduledTimerWithTimeInterval:
3.0f
target:
self
selector:
@selector(dismissAlert:
)
userInfo:
[NSDictionarydictionaryWithObjectsAndKeys:
alert,
@"alert",@"testing",@"key",nil]//如果不用传递参数,那么可以将此项设置为nil.
repeats:
NO];
NSLog(@"releasealert");
[alertrelease];
-(void)dismissAlert:
(NSTimer*)timer{
NSLog(@"releasetimer");
NSLog([[timeruserInfo]objectForKey:
@"key"]);
UIAlertView*alert=[[timeruserInfo]objectForKey:
@"alert"];
[alertdismissWithClickedButtonIndex:
0animated:
YES];
}
定时器停止使用:
[timerinvalidate];
timer=nil;
7.用户缺省值NSUserDefaults读取:
//得到用户缺省值
NSUserDefaults*defs=[NSUserDefaultsstandardUserDefaults];
//在缺省值中找到AppleLanguages,返回值是一个数组
NSArray*languages=[defsobjectForKey:
@"AppleLanguages"];
NSLog(@"alllanguage语言is%@",languages);
//在得到的数组中的第一个项就是用户的首选语言了
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- iOS 开发 常用 代码