基础框架Fundation Framework

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

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

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

资源描述

基础课程MACAppleInc.Objective-C语言苹果技术开发课程Liujie2012Lession7基础框架FundationFrameworkLiujie2012基础框架•本章内容包括:基础框架就是一组类的包,提供给开发人员需要的类。常用的基础框架类包括在Foundation/Foundation.h文件中。可参考P161页图结构。本章任务:学会常用基础框架类提供的各种方法,用于应用程序的开发。基础框架•1、数字对象:NSNumber,数字类,是int,float,double的类表现形式。•NSNumber*myNumber,*floatNumber,*intNumber;•//创建integer类型对象•intNumber=[NSNumbernumberWithInteger:123];•NSLog(@%i,[intNumberintegerValue]);•//创建long类型对象•myNumber=[NSNumbernumberWithLong:0xababab];•NSLog(@%lx,[myNumberlongValue]);•//创建char类型对象•myNumber=[NSNumbernumberWithChar:'K'];•NSLog(@%c,[myNumbercharValue]);•//创建float类型对象•floatNumber=[NSNumbernumberWithFloat:123.00];•NSLog(@%f,[floatNumberfloatValue]);基础框架•1、数字对象:NSNumber,数字类,是int,float,double的类表现形式。•//创建double类型对象•myNumber=[NSNumbernumberWithDouble:112233e+15];•NSLog(@%lg,[myNumberdoubleValue]);•//判断两个对象的值是否相等•if([intNumberisEqualToNumber:floatNumber]==YES){•NSLog(@值相等);•}else{•NSLog(@值不相等);•}•//比较两个对象的值大小•if([intNumbercompare:myNumber]==NSOrderedAscending){•NSLog(@左边的数字小);•}else{•NSLog(@左边的数字大);•}基础框架•2、字符串类:NSString,注意一个字符和包括一个字符的字符串的区别。•‘a’:占1字节•“a”:占2字节,多了一个’\0’.•例题:•NSString*str1=@Thankyouverymuch,Sam;•NSString*str2=@Thankyouverymuch,Lee;•NSString*str3;•NSRangerange;•NSLog(@字符串1的长度为:%lu,[str1length]);•str3=[NSStringstringWithString:str1];•NSLog(@通过字符串1初始化的字符串3为:%@,str3);•str3=[str1stringByAppendingString:str2];•NSLog(@将两个字符串连接起来得到的字符串为:%@,str3);基础框架•2、字符串类:NSString•if([str1isEqualToString:str3]==YES){•NSLog(@这两个字符串相等);•}else{•NSLog(@这两个字符串不相等);•}•if([str1compare:str2]==NSOrderedAscending){•NSLog(@字符串1小于字符串2);•}elseif([str1compare:str2]==NSOrderedSame){•NSLog(@字符串1等于字符串2);•}else{•NSLog(@字符串1大于字符串2);•}•str3=[str1uppercaseString];•NSLog(@转换为大写的字符串为:%@,str3);基础框架•2、字符串类:NSString•str3=[str1lowercaseString];•NSLog(@转换为小写的字符串为:%@,str3);•str3=[str1substringToIndex:5];•NSLog(@截取前5个字符成为新的字符串为:%@,str3);•str3=[str1substringFromIndex:5];•NSLog(@去除前5个字符成为新的字符串为:%@,str3);•str3=[[str1substringFromIndex:20]substringToIndex:3];•NSLog(@第20个字符到第23个字符之间形成的字符串为:%@,str3);基础框架•2、字符串类:NSString•range=[str1rangeOfString:@Sam];•NSLog(@包含字符串开始的位置是%lu,长度是%lu,range.location,range.length);•if([str1rangeOfString:@Lee].location==NSNotFound){•NSLog(@没有找到包含字符串);•}else{•NSLog(@包含字符串开始的位置是%lu,长度是%lu,range.location,range.length);•}基础框架•3、可修改的字符串:NSMutableString•NSString本身不能修改,修改就是重新生成一个指针指向一个新的字符串首地址,为了扩展字符串功能派生了NSMutableString,该类是NSString类的派生类,所有的NSString类方法几乎都可以再NSMutableString类中使用,同时可以支持修改字符串功能。•例题:•NSString*str1=@Welocome,Sam!;•NSString*str2,*str3;•NSMutableString*mstr;•NSRangerange;•mstr=[NSMutableStringstringWithString:str1];•NSLog(@%@,mstr);•[mstrinsertString:@backatIndex:9];•NSLog(@%@,mstr);基础框架•3、可修改的字符串:NSMutableString•[mstrinsertString:@HowareyouatIndex:[mstrlength]];•NSLog(@%@,mstr);•[mstrappendString:@inthere?];•NSLog(@%@,mstr);•[mstrdeleteCharactersInRange:NSMakeRange(29,9)];•NSLog(@%@,mstr);•range=[mstrrangeOfString:@Howareyou?];•if(range.location!=NSNotFound){•[mstrdeleteCharactersInRange:range];•NSLog(@%@,mstr);•}基础框架•3、可修改的字符串:NSMutableString•[mstrsetString:@Welcome,Sam!];•NSLog(@%@,mstr);•[mstrreplaceCharactersInRange:NSMakeRange(8,3)withString:@Alex];•NSLog(@%@,mstr);•str2=@Welcome;•str3=@Hello;•range=[mstrrangeOfString:str2];•if(range.location!=NSNotFound){•[mstrreplaceCharactersInRange:rangewithString:str3];•NSLog(@%@,mstr);•}基础框架•3、可修改的字符串:NSMutableString•str2=@l;•str3=@L;•range=[mstrrangeOfString:str2];•while(range.location!=NSNotFound){•[mstrreplaceCharactersInRange:rangewithString:str3];•range=[mstrrangeOfString:str2];•}•NSLog(@%@,mstr);•str2=@L;•str3=@l;•[mstrreplaceOccurrencesOfString:str2•withString:str3•options:nil•range:NSMakeRange(0,[mstrlength])];•NSLog(@%@,mstr);基础框架•4、数组类:NSArray•NSArray*city=[NSArrayarrayWithObjects:•@上海,@广州,@宁波,@杭州,@重庆,@武汉,nil];•for(inti=0;i[citycount];i++){•NSLog(@%@,[cityobjectAtIndex:i]);•}基础框架•5、可变数组类:NSMutableArray•NSMutableArray*nsma=[NSMutableArrayarrayWithCapacity:5];•for(intp=1;p=50;p++){•if(p%3==0){•[nsmaaddObject:[NSNumbernumberWithInteger:p]];•}•}•for(inti=0;i[nsmacount];++i){•NSLog(@%li,(long)[[nsmaobjectAtIndex:i]integerValue]);•}基础框架•6、数组排序:使用selector可以设置排序student.h•@interfaceStudent:NSObject{•NSString*name;•intage;•}•@property(copy,nonatomic)NSString*name;•@propertyintage;•-(void)print;•-(NSComparisonResult)compareName:(id)element;•@end基础框架•6、数组排序:使用selector可以设置排序。•Student.m•@implementationStudent•@synthesizename,age;•-(void)print{•NSLog(@nameis%@,ageis%i,name,age);•}•-(NSComparisonResult)compareName:(id)element{•return[namecompare:[elementname]];•}•@end基础框架•6、数组排序:使用selector可以设置排序。•填充对象:•Student*stu1=[[Studentalloc]init];•Student*stu2=[[Studentalloc]init];•Student*stu3=[[Studentalloc]init];•[stu1setName:@Sam];•[stu1setAge:30];•[stu2setName:@Lee];•[stu2setAge:23];•[stu3setName:@Alex];•[stu3setAge:26];•NSMutableArray*students=[[NSMutableArrayalloc]init];•[studentsaddObject:stu1];•[studentsaddObject:stu2];•[studentsaddObject:stu3];基础框架•6、数组排序:使用selector可以设置排序。•排序:•NSLog(@排序前);•for(inti=0;i[studentscount];i++){•Student*stu4=[studentsobjectAtIndex:i];•NSLog(@Name:%@,Ag

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

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

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

×
保存成功