ios开发经常用到的代码_第1页
ios开发经常用到的代码_第2页
ios开发经常用到的代码_第3页
ios开发经常用到的代码_第4页
ios开发经常用到的代码_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include  /* 说明 malloc, NULL, size_t */#include  /* 说明 va_ 相关类型和函数 */#include  /* 说明 strcat 等 */char *vstrcat(const char *first, .)size_t len;char *retbuf;va_list argp;char *p;if(first = NULL)return NULL;len = strlen(first);va_start(argp, first);while(p = va_arg(argp, c

2、har *) != NULL)len += strlen(p);va_end(argp);retbuf = malloc(len + 1); /* +1 包含终止符 0 */if(retbuf = NULL)return NULL; /* 出错 */(void)strcpy(retbuf, first);va_start(argp, first); /* 重新开始扫描 */while(p = va_arg(argp, char *) != NULL)(void)strcat(retbuf, p);va_end(argp);retbuf = malloc(len + 1); /* +1 包含终止

3、符 0 */if(retbuf = NULL)return NULL; /* 出错 */(void)strcpy(retbuf, first);va_start(argp, first); /* 重新开始扫描 */while(p = va_arg(argp, char *) != NULL)(void)strcat(retbuf, p);va_end(argp);return retbuf;%c 一个单一的字符%d 一个十进制整数%i 一个整数%e, %f, %g 一个浮点数%o 一个八进制数%s 一个字符串%x 一个十六进制数%p 一个指针%n 一个等于读取字符数量的整数%u 一个无符号整数

4、% 一个字符集% 一个精度符号 /一、NSString    /*-创建字符串的方法-*/    1、创建常量字符串。    NSString *astring = "This is a String!"    2、创建空字符串,给予赋值。    NSString *astring = init;    astring = "This is a String!"    NSLog("astring:%&qu

5、ot;,astring);    ;    3、在以上方法中,提升速度:initWithString方法    NSString *astring = initWithString:"This is a String!"    NSLog("astring:%",astring);    ;    4、用标准c创建字符串:initWithCString方法    char *Cstring = "This

6、is a String!"    NSString *astring = initWithCString:Cstring;    NSLog("astring:%",astring);    ;    5、创建格式化字符串:占位符(由一个%加一个字符组成)    int i = 1;    int j = 2;    NSString *astring =         &#

7、160;               initWithString:;    NSLog("astring:%",astring);    ;    6、创建临时字符串    NSString *astring;    astring = ;    NSLog("astring:%",astring);    /*-从

8、文件读取字符串:initWithContentsOfFile方法 -*/    NSString *path = "astring.text"    NSString *astring = initWithContentsOfFile:path;    NSLog("astring:%",astring);    ;    /*-写字符串到文件:writeToFile方法 -*/    NSString *astring = initW

9、ithString:"This is a String!"    NSLog("astring:%",astring);    NSString *path = "astring.text"    ;    ;        /*- 比较两个字符串-*/    用C比较:strcmp函数    char string1 = "string!"  &

10、#160; char string2 = "string!"    if(strcmp(string1, string2) = = 0)            NSLog("1");        isEqualToString方法    NSString *astring01 = "This is a String!"    NSString *astri

11、ng02 = "This is a String!"    BOOL result = ;    NSLog("result:%d",result);    compare方法(comparer返回的三种值)    NSString *astring01 = "This is a String!"    NSString *astring02 = "This is a String!"    BO

12、OL result = = = NSOrderedSame;    NSLog("result:%d",result);    NSOrderedSame 判断两者内容是否相同    NSString *astring01 = "This is a String!"    NSString *astring02 = "this is a String!"    BOOL result = = = NSOrderedAscending;&

13、#160;   NSLog("result:%d",result);    /NSOrderedAscending 判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)    NSString *astring01 = "this is a String!"    NSString *astring02 = "This is a String!"    BOOL result = = = NSOrderedDes

14、cending;    NSLog("result:%d",result);    /NSOrderedDescending 判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)    不考虑大 小写比较字符串1    NSString *astring01 = "this is a String!"    NSString *astring02 = "This is a String!" 

15、  BOOL result = = = NSOrderedSame;    NSLog("result:%d",result);    /NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为 真)    不考虑大小写比较字符串2    NSString *astring01 = "this is a String!"    NSString *astring02 = "

16、This is a String!"    BOOL result = astring01 compare:astring02                            options:NSCaseInsensitiveSearch | NSNumericSearch = = NSOrderedSame;    NSLog("result:%d&

17、quot;,result);         /NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。    /*-改变字符串的大小写-*/    NSString *string1 = "A String"    NSString *string2 = "String"    N

18、SLog("string1:%",);/大写    NSLog("string2:%",);/小写    NSLog("string2:%",);/首字母大小    /*-在串中搜索子串 -*/    NSString *string1 = "This is a string"    NSString *string2 = "string"    NSRange range =

19、 ;    int location = range.location;    int leight = range.length;    NSString *astring =                         initWithString:NSString stringWithFormat:"Location:%i,Leight:%i"&#

20、160;                       ,location,leight;    NSLog("astring:%",astring);    ;    /*-抽取子串 -*/    -substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符    NSString *strin

21、g1 = "This is a string"    NSString *string2 = ;    NSLog("string2:%",string2);    -substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符    NSString *string1 = "This is a string"    NSString *string2 = ;    NSLog(

22、"string2:%",string2);    -substringWithRange: /按照所给出的位置,长度,任意地从字符串中截取子串    NSString *string1 = "This is a string"    NSString *string2 = ;    NSLog("string2:%",string2); const char *fieldValue = ;const char *fieldValue = ;NSString

23、 转 NSDataNSString* str= "kilonet"NSData* data=;    Date format用法:  -(NSString *) getDay:(NSDate *) d    NSString *s ;    NSDateFormatter *format = init;    ;    s = ;    ;    return s;各地时区获取:NSDate *nowDate = ;

24、    NSDateFormatter *formatter    =   init;    ;    /    根据时区名字获取当前时间,如果该时区不存在,默认获取系统当前时区的时间    /    NSTimeZone* timeZone = ;    /    ;    /获取所有的时区名字    NSArray *array = ;  &#

25、160; /    NSLog("array:%",array);    /for循环    /    for(int i=0;i<i+)    /        /        NSTimeZone* timeZone = ;    /            /

26、0;       NSString *locationTime = ;    /        NSLog("时区名字:%   : 时区当前时间: %",locationTime);    /        /NSLog("timezone name is:%",);    /    &#

27、160;   /快速枚举法    for(NSString *timeZoneName in array)                NSLog("%,%",timeZoneName,);        ;    ;NSCalendar用法:-(NSString *) getWeek:(NSDate *) d     NSCalendar *c

28、alendar = initWithCalendarIdentifier:NSGregorianCalendar;    unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekdayCalendarUnit;    NSDateComponents *components = ;    ;    switch ()        

29、; case 2:            return "Monday"            break;        case 3:            return "Tuesday"      &#

30、160;     break;        case 4:            return "Wednesday"            break;        case 5:         

31、60; return "Thursday"            break;        case 6:            return  "Friday"            break;   &#

32、160;    case 7:            return  "Saturday"            break;        case 1:            return "Sunday"

33、            break;        default:            return "No Week"            break;        / 用components,我们可以读取其他更多的数据。4. 用

34、Get方式读取网络数据:将网络数读取为字符串- (NSString *) getDataByURL:(NSString *) url     return initWithData: encoding:NSUTF8StringEncoding;/读取网络图片- (UIImage *) getImageByURL:(NSString *) url     return initWithData:;多线程;-(void) scheduleTask     /create a pool    NSAutoreleaseP

35、ool *pool = init;    /release the pool;    ;/如果有参数,则这么使用:;-(void) scheduleTask:(NSDate *) mdate     /create a pool    NSAutoreleasePool *pool = init;    /release the pool;    ;/注意selector里有冒号。    /在线程里运行主线程里的方法     ;6. 定

36、时器NSTimer用法:代码  / 一个可以自动关闭的Alert窗口    UIAlertView *alert = initWithTitle:nil                                               &#

37、160;    message:"一个可以自动关闭的Alert窗口"                                                   delegate:nil   

38、                                       cancelButtonTitle:nil /NSLocalizedString("OK", "OK")   /取消任何按钮         

39、                                 otherButtonTitles:nil;    /alert setBounds:CGRectMake      (alert.bounds.origin.x, alert.bounds.origin.y,     &#

40、160;alert.bounds.size.width, alert.bounds.size.height+30.0);    ;    UIActivityIndicatorView *indicator = initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge;    / Adjust the indicator so it is up a few pixels from the bottom of the alert   

41、indicator.center = CGPointMake(alert.bounds.size.width/2,  alert.bounds.size.height-40.0);    ;    ;    ;    NSTimer scheduledTimerWithTimeInterval:3.0f                      

42、              target:self                                   selector:selector(dismissAlert:)         

43、0;                         userInfo:NSDictionary dictionaryWithObjectsAndKeys:alert,                   "alert", "testing ", "key&q

44、uot; ,nil  /如果不用传递参数,那么可以将此项设置为nil.                                    repeats:NO;    NSLog("release alert");    ;-(void) dismissAlert:(NSTi

45、mer *)timer    NSLog("release timer");    NSLog(  objectForKey:"key");    UIAlertView *alert =   objectForKey:"alert"    ;定时器停止使用:;timer = nil;     7. 用户缺省值NSUserDefaults读取:    /得到

46、用户缺省值    NSUserDefaults *defs = ;    /在缺省值中找到AppleLanguages, 返回值是一个数组    NSArray* languages = ;    NSLog("all language语言 is %", languages);    /在得到的数组中的第一个项就是用户的首选语言了    NSLog("首选语言 is %",);      /get

47、 the language & country code    NSLocale *currentLocale = ;    NSLog("Language Code is %", );    NSLog("Country Code is %", currentLocale objectForKey:NSLocaleCountryCode8. View之间切换的动态效果设置:    SettingsController *settings = initWithNibN

48、ame:"SettingsView" bundle:nil;    settings.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;  /水平翻转    ;    ;9.NSScrollView 滑动用法:-(void) scrollViewDidScroll:(UIScrollView *)scrollView    NSLog("正在滑动中.");/用户直接滑动NSScrollV

49、iew,可以看到滑动条-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView / 通过其他控件触发NSScrollView滑动,看不到滑动条- (void) scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView     11.键盘处理系列/set the UIKeyboard to switch to a different text field when you press return/switch textField to th

50、e name of your textfield;srandom(time(NULL); /随机数种子id d = random(); / 随机数   4. iPhone的系统目录:/得到Document目录:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = ;/得到temp临时目录:NSString *tempPath = NSTemporaryDirectory();

51、/得到目录上的文件地址:NSString *文件地址 = 目录地址 stringByAppendingPathComponent:"文件名.扩展名"5. 状态栏显示Indicator:.networkActivityIndicatorVisible = YES;   6.app Icon显示数字:- (void)applicationDidEnterBackground:(UIApplication *)application    setApplicationIconBadgeNumber:5;   7.sqli

52、te保存地址: 代码    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *thePath = ;    NSString *filePath = ;    NSString *dbPath = resourcePath              

53、0;         stringByAppendingPathComponent:"kilonet2.sqlite"    8.Application退出:exit(0);      9. AlertView,ActionSheet的cancelButton点击事件:代码-(void) actionSheet :(UIActionSheet *) actionSheet didDismissWithButtonIndex:(NSInteger) butt

54、onIndex     NSLog("cancel actionSheet.");    /当用户按下cancel按钮    if( buttonIndex = )         exit(0);    /    /当用户按下destructive按钮/    if( buttonIndex = ) /        / DoSomet

55、hing here./    - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex      NSLog("cancel alertView.");    if (buttonIndex = )         exit(0);      10.给Window设置全局

56、的背景图片:window.backgroundColor = ;    11. UITextField文本框显示及对键盘的控制:代码#pragma mark -#pragma mark UITextFieldDelegate/控制键盘跳转- (BOOL)textFieldShouldReturn:(UITextField *)textField     if (textField = _txtAccount)         if (=0)       &

57、#160;     return NO;                    else if (textField = _txtPassword)                 return YES;/输入框背景更换-(BOOL) textFieldShouldBeginEditing:(UITextField *)text

58、Field    ;    return YES;-(void) textFieldDidEndEditing:(UITextField *)textField    ;12.UITextField文本框前面空白宽度设置以及后面组合按钮设置:代码    /给文本输入框后面加入空白    _txtAccount.rightView = _btnDropDown;    _txtAccount.rightViewMode =  UITextFieldViewModeAlways;    /给文本输入框前面加入空白    CGRect frame = ;    frame.size.width =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论