Objective-C中的数据类型、常量、变量、运算符与表达式

1、Objective-C中的数据类型:

  • Objective-C中的基本数据类型有:int、char(-128-127)、float、double、BOOL,Byte(0-255)
  • Id类型相当于(等价与)NSObject *类型

  (在iOS中,int类型可使用NSInteger / NSUInteger表示;float / double类型可使用CGFloat表示;后者可以根据系统位数长度[32位/64位]自动选择实际数据类型)

  OC中对象数据类型以指针形式表示(例如:NSString *str = @”Hello World!”;)

2、数据类型转换:

  • 自动类型转换:将范围小的数据类型值转换到范围大的类型值;

  Byte,short,char->int->long->float->double

  Byte short char之间不能互相转换,他们三者在计算时首先会转换成int类型

  • 强制类型转换:(类型)变量或数值或对象

  Byte b1 = 250(160); Byte b2 = 10; Byte b3 = b1 + b2; nslog(@”b3=%d”,b3);

  Byte=unsigned char    NSinteger CGfloat

  Int a = 38;int b = 100; float c = a /b(float c = (float)a/b);

  强制类型转换:

1 int a = 38;
2 int b = 100;
3 float f = (float)a/b;      //可以使用"(数据类型)变量 "这种语法强制转换类型,做强制类型转换时,要考虑一下 数据是否会溢出。      
4 NSLog(@"f=%f",f);
5 
6 Byte b = 3;//Byte 是oc重定义为Uint 范围是0 - 255 不能赋给负值
7 
8 BOOL b = YES;//布尔类型有非零既真的说法 所以YES=1 NO=0

3、常量、变量(注意:以及变量的命名规范):

  标识符由字母、下划线、$、数字组成:

1 int a;
2 int $a;
3 int _a;
4 //int 1_a;  错的
5 int A;
6 //int if;    错的
7 //int a*;   错的
8 NSString *str = @"abc";//""是c里的字符串 @""是oc里的字符串

4、运算符与表达式:

  • 算数运算符:+、-、*、/、%、++、--
  • 比较运算符:==、!=、>、>=、<、<=
  • 逻辑运算符:!、&&、||
  • 赋值运算符:=、+=、-=、*=、/=、%=
  • 运算优先级:

  () //运算优先级高

  !、*、/、%

  +、-

  ==、!=、>、>=、<、<=

  &&、||  

  +=、-=、*=、/=、%=

  = //运算优先级低

5、学习参考网站:

      http://developer.apple.com

      http://www.cocoachina.com

      http://www.macx.cn

      http://www.code4app.com

      http://ui4app.com

  IT综合类网站:

      http://www.36kr.com

      http://www.51cto.com

 

posted @ 2015-11-24 22:50  CheungSir  阅读(319)  评论(0编辑  收藏  举报