【Objective-C编程】Objective-C的基本数据类型

 Objective-C是扩充C的面向对象编程语言,是在C的基础上,加入面向对象特性扩充而成的编程语言。目前,Objective-C主要应用于Mac OS X和iOS这两个NeXTSTEP的衍生系统。相信刚开始学Oc的人应该跟我一样对oc跟Cocoa的关系打问号。或许可以这么说,Cocoa是框架环境,而Oc是语言一个工具。打个比喻可能直观一点,Cocoa是厂房,那么Oc就是厂房里的机器 ,程序员要做的就是用机器在厂房这个环境里进行制作,生产产品,我们要生产的就是app。Cocoa这个厂房环境下有两个框架分别为Foundation和Application Kit 框架。


1.Objective-C基本数据类型的所占长度:

NSLog(@"The size of an int is: %lu bytes.",sizeof(int));  
NSLog(@"The size of a short int is: %lu bytes.",sizeof(short int));  
NSLog(@"The size of a long int is: %lu bytes.",sizeof(long int));  
NSLog(@"The size of a char is: %lu bytes.",sizeof(char));  
NSLog(@"The size of a float is: %lu bytes.",sizeof(float));  
NSLog(@"The size of a double is: %lu bytes.",sizeof(double));
NSLog(@"The size of a bool is: %lu bytes.",sizeof(bool));

结果: 

BaseType[3032:f803] The size of an int is: 4 bytes.  
BaseType[3032:f803] The size of a short int is: 2 bytes.  
BaseType[3032:f803] The size of a long int is: 4 bytes.  
BaseType[3032:f803] The size of a char is: 1 bytes.  
BaseType[3032:f803] The size of a float is: 4 bytes.  
BaseType[3032:f803] The size of a double is: 8 bytes.  
BaseType[3032:f803] The size of a bool is: 1 bytes.  

2.格式化输出数据 

int integerType = 5;  
//浮点型  
float floatType = 3.1415;  
//双浮点型  
double doubleType = 2.2033;  
//短整型  
short int shortType = 200;  
//长整型  
long long int longlongType = 7758123456767L;  
//c语言字符串  
char * cstring = "this is a string!";  
		      
  
//整型  
NSLog(@"The value of integerType = %d",integerType);  
//浮点型  
NSLog(@"The value of floatType = %.2f",floatType);  
//双浮点型  
NSLog(@"The value of doubleType = %e",doubleType);  
//短整型  
NSLog(@"The value of shortType = %hi",shortType);  
//长整型  
NSLog(@"The value of longlongType = %lli",longlongType);  
//c语言字符串  
NSLog(@"The value of cstring = %s",cstring); </span>
结果: 
BaseType[3215:f803] The value of integerType = 5  
BaseType[3215:f803] The value of floatType = 3.14  
BaseType[3215:f803] The value of doubleType = 2.203300e+00  
BaseType[3215:f803] The value of shortType = 200  
BaseType[3215:f803] The value of longlongType = 7758123456767  
BaseType[3215:f803] The value of cstring = this is a string!  


3.int,NSInteger,NSUInteger,NSNumber

1.int与NSInteger:int为C语言本身的数据类型,以NS开头的都是来自于Cocoa而不是其他工具包,包括NSInteger。

2.NSInteger与NSUInteger:NSUInteger是无符号的,没有负数,NSInteger是有符号的。

3.NSNumber:有人说有了int和NSInteger之后还需要NSNumber吗。前者都是数据类型,而Oc常常会用到对象,没错 NSNumber就是一个类,创建出来的为数字对象


4.NSString与NSInteger的转换

 int num =12345;
 NSString* string =[NSString stringWithFormat:@"%d",num];
 NSLog(@"%@",string);
        
 NSString* a=@"56";
 num =[a integerValue];
 NSLog(@"%d",num);
结果: 

2015-08-02 21:52:42.658 HelloWorld[6083:4972667] 12345
2015-08-02 21:52:42.659 HelloWorld[6083:4972667] 56
同理其他基本类型char float 都可以这样转。


ok.关于Oc的基本数据类型就分享到此,有讲得不对的地方还望指出进行探讨互相学习!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值