iOS开发常用代码Word文档下载推荐.docx
《iOS开发常用代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《iOS开发常用代码Word文档下载推荐.docx(20页珍藏版)》请在冰豆网上搜索。
@"
];
4、用标准c创建字符串:
initWithCString方法
char*Cstring="
NSString*astring=[[NSStringalloc]initWithCString:
Cstring];
5、创建格式化字符串:
占位符(由一个%加一个字符组成)
inti=1;
intj=2;
NSString*astring=[[NSStringalloc]
initWithString:
[NSStringstringWithFormat:
%d.Thisis%istring!
i,j]];
6、创建临时字符串
NSString*astring;
astring=[NSStringstringWithCString:
Thisisatemporarystring"
/*----------------从文件读取字符串:
initWithContentsOfFile方法----------------*/
NSString*path=@"
astring.text"
NSString*astring=[[NSStringalloc]initWithContentsOfFile:
path];
/*----------------写字符串到文件:
writeToFile方法----------------*/
[astringwriteToFile:
pathatomically:
YES];
/*----------------比较两个字符串----------------*/
用C比较:
strcmp函数
charstring1[]="
string!
charstring2[]="
if(strcmp(string1,string2)==0)
{
1"
);
}
isEqualToString方法
NSString*astring01=@"
NSString*astring02=@"
BOOLresult=[astring01isEqualToString:
astring02];
result:
%d"
result);
compare方法(comparer返回的三种值)
BOOLresult=[astring01compare:
astring02]==NSOrderedSame;
NSOrderedSame判断两者内容是否相同
thisisaString!
astring02]==NSOrderedAscending;
//NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
astring02]==NSOrderedDescending;
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
不考虑大小写比较字符串1
BOOLresult=[astring01caseInsensitiveCompare:
//NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
不考虑大小写比较字符串2
astring02
options:
NSCaseInsensitiveSearch|NSNumericSearch]==NSOrderedSame;
//NSCaseInsensitiveSearch:
不区分大小写比较NSLiteralSearch:
进行完全比较,区分大小写NSNumericSearch:
比较字符串的字符个数,而不是字符值。
/*----------------改变字符串的大小写----------------*/
NSString*string1=@"
AString"
NSString*string2=@"
String"
string1:
[string1uppercaseString]);
//大写
string2:
[string2lowercaseString]);
//小写
[string2capitalizedString]);
//首字母大小
/*----------------在串中搜索子串----------------*/
Thisisastring"
string"
NSRangerange=[string1rangeOfString:
string2];
intlocation=range.location;
intleight=range.length;
Location:
%i,Leight:
%i"
location,leight]];
/*----------------抽取子串----------------*/
-substringToIndex:
从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString*string2=[string1substringToIndex:
3];
string2);
-substringFromIndex:
以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString*string2=[string1substringFromIndex:
-substringWithRange:
//按照所给出的位置,长度,任意地从字符串中截取子串
NSString*string2=[string1substringWithRange:
NSMakeRange(0,4)];
constchar*fieldValue=[valuecStringUsingEncoding:
NSUTF8StringEncoding];
constchar*fieldValue=[valueUTF8String];
NSString转NSData
NSString*str=@"
kilonet"
NSData*data=[strdataUsingEncoding:
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:
//根据时区名字获取当前时间,如果该时区不存在,默认获取系统当前时区的时间
//NSTimeZone*timeZone=[NSTimeZonetimeZoneWithName:
Europe/Andorra"
//[formattersetTimeZone:
timeZone];
//获取所有的时区名字
NSArray*array=[NSTimeZoneknownTimeZoneNames];
//NSLog(@"
array:
array);
//for循环
//for(inti=0;
i<
[arraycount];
i++)
//{
[arrayobjectAtIndex:
i]];
//NSString*locationTime=[formatterstringFromDate:
nowDate];
时区名字:
%@:
时区当前时间:
%@"
[arrayobjectAtIndex:
i],locationTime);
////NSLog(@"
timezonenameis:
i]);
//}
//快速枚举法
for(NSString*timeZoneNameinarray){
[formattersetTimeZone:
[NSTimeZonetimeZoneWithName:
timeZoneName]];
%@,%@"
timeZoneName,[formatterstringFromDate:
nowDate]);
[formatterrelease];
[nowDaterelease];
NSCalendar用法:
-(NSString*)getWeek:
(NSDate*)d{
NSCalendar*calendar=[[NSCalendaralloc]initWithCalendarIdentifier:
NSGregorianCalendar];
unsignedunits=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit;
NSDateComponents*components=[calendarcomponents:
unitsfromDate:
[calendarrelease];
switch([componentsweekday]){
case2:
return@"
Monday"
break;
case3:
Tuesday"
case4:
Wednesday"
case5:
Thursday"
case6:
Friday"
case7:
Saturday"
case1:
Sunday"
default:
NoWeek"
//用components,我们可以读取其他更多的数据。
4.用Get方式读取网络数据:
将网络数读取为字符串
-(NSString*)getDataByURL:
(NSString*)url{
return[[NSStringalloc]initWithData:
[NSDatadataWithContentsOfURL:
[NSURLURLWithString:
[urlstringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]]]encoding:
//读取网络图片
-(UIImage*)getImageByURL:
return[[UIImagealloc]initWithData:
NSUTF8StringEncoding]]]];
多线程
[NSThreaddetachNewThreadSelector:
@selector(scheduleTask)toTarget:
selfwithObject:
nil];
-(void)scheduleTask{
//createapool
NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
//releasethepool;
[poolrelease];
//如果有参数,则这么使用:
@selector(scheduleTask:
)toTarget:
[NSDatedate]];
-(void)scheduleTask:
(NSDate*)mdate{
//注意selector里有冒号。
//在线程里运行主线程里的方法
[selfperformSelectorOnMainThread:
@selector(moveToMain)withObject:
nilwaitUntilDone:
FALSE];
6.定时器NSTimer用法:
代码
//一个可以自动关闭的Alert窗口
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:
nil
message:
[@"
一个可以自动关闭的Alert窗口"
delegate:
cancelButtonTitle:
nil//NSLocalizedString(@"
OK"
@"
)//取消任何按钮
otherButtonTitles:
//[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];
releasealert"
[alertrelease];
-(void)dismissAlert:
(NSTimer*)timer{
releasetimer"
NSLog([[timeruserInfo]objectForKey:
]);
UIAlertView*alert=[[timeruserInfo]objectForKey:
[alertdismissWithClickedButtonIndex:
0animated:
YES];
定时器停止使用:
[timerinvalidate];
timer=nil;
7.用户缺省值NSUserDefaults读取:
//得到用户缺省值
NSUserDefaults*defs=[NSUserDefaultsstandardUserDefaults];
//在缺省值中找到AppleLanguages,返回值是一个数组
NSArray*languages=[defsobjectForKey:
AppleLanguages"
alllanguage语言is%@"
languages);
//在得到的数组中的第一个项就是用户的首选语言了