资源描述:
《objective-c数据类型和运算符x》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基础课程MACAppleInc.Objective-C语言苹果技术开发课程Liujie2012Lession2Objective-C数据类型和运算符Liujie2012数据类型和运算符本章内容包括:简单数据类型Objective-C其它数据类型运算符和表达式简单数据类型一、整型:int(4bytes)如果是64位系统则(8bytes),使用%i格式输出。longint(long)shortint(short)(2bytes)unsignedintunsignedlongunsignedshort简单数据类型1、整型变量的定义:intmain(intargc
2、,char*argv[]){NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];intintegerVar=100;NSLog(@"integerVar=%i",integerVar);[pooldrain];return0;}简单数据类型2、混用整型变量:NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];inta,b,c,d;unsignedu;a=12;b=-24;u=10;c=a+u;d=b+u;NSLog(@"a+u=%i,b+u=%i
3、",c,d);[pooldrain];return0;简单数据类型简单数据类型1、float定义:floatfloatingVar=331.79;doubledoubleVar=8.44e+11;NSLog(@"floatingVar=%f",floatingVar);NSLog(@"doubleVar=%e",doubleVar);NSLog(@"doubleVar=%g",doubleVar);说明:%f十进制形式表示float%e科学计数法表示%g最短表示法,用指数形式。简单数据类型2、float误差:floata=123456.789e5;floa
4、tb=a+20;NSLog(@"%f",a);NSLog(@"%f",b);简单数据类型3、float有效位数:floata=33333.33333;doubleb=33333.33333333333333;NSLog(@"%f",a);NSLog(@"%f",b);请检查结果默认保留6位小数部分。其余四舍五入简单数据类型三、字符型:区分字符和字符串‘a’,’b’,’1’,’2’字符@”helloworld”字符串简单数据类型三、字符型:转义字符::换行t:下一制表位置b:退格r回车f换页\反斜杠’’’单引号”双引号ddd八进制xh
5、h16进制简单数据类型1、字符型的整数存储:NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];chara=120;charb=121;NSLog(@"%c,%c",a,b);NSLog(@"%i,%i",a,b);[pooldrain];//////////////////////////////////////////////////x,y120,121简单数据类型2、字符型的运算:NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];char
6、a='a';charb='b';a=a-32;b=b-32;NSLog(@"%c,%c",a,b);NSLog(@"%i,%i",a,b);[pooldrain];//////////////////////////////////////////////////A,B65,66简单数据类型四、字符串:NSString定义,MFC是CString,(苹果和微软两个公司从1998年就是这么竞争着)合法字符串:@”china”@”$12.5”简单数据类型五、id类型:Objective-C中id类型可以代表任何数据类型对象,这种类型被定义为指向对象的指针,在使
7、用时不需要加*。简单数据类型1、id设计类的运用(student类设计):@interfaceStudent:NSObject{intsid;NSString*name;}@propertyintsid;@property(nonatomic,retain)NSString*name;-(void)print;-(void)setSid:(int)sidandName:(NSString*)name;@end简单数据类型1、id设计类的运用(student类设计):#import"Student.h"@implementationStudent@synth
8、esizesid,name;-(void)print{NSLog(