Objective-C的基本数据类型:int fioat double char id五种数据类型。
在Objective-C中,任何数字、单个字符或字符串通常被称为常量。
int:
int类型的变量只能用于保存整型值。
整数常量是由一个或者多个数字的序列组成。序列钱的负号表示该值是一个负数。158、-1和0都是合法的整数常量。
float:
float类型的变量可以存储包含小位数的值。要显示浮点数可用NSLog转换字符%f或者%g。
double:
和float类型非常相似,只是可存储范围是float变量的两倍。
char:
char变量可以存储单个字符,将字符放入一对单引号中就能得到字符常量。'0'、'a'、';'都是合法的字符常量。
字符常量是放在单引号中的单个字符,而字符串是放在双引号中的任意个数的字符。
字符常量' '是换行符。
id类型:
可存储任何类型的对象。
整数运算和一元负号运算符:
04
05 int main (int argc, const char * argv[]) {
06 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
07
08 int a = 25;
09 int b = 2;
10 float c = 25.0;
11 float d = 2.0;
12
13 NSLog(@"6 + a / 5 * b = %i", 6 + a / 5 * b);
14 NSLog(@"a / b * b = %i", a / b * b);
15 NSLog(@"c / d * d = %f", c / d * d);
16 NSLog(@"-a = %i", -a);
17
18 [pool drain];
19 return 0;
20 }
6 + a / 5 * b = 16
a / b * b = 24
c / d * d = 25.000000
-a = -25
整型值和浮点值的相互转换:
04
05 int main (int argc, const char * argv[])
06 {
07 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
08 float f1 = 123.125, f2;
09 int i1, i2 = -150;
10
11 i1 = f1; // floating to integer conversion
12 NSLog(@"%f assigned to an int produces %i", f1, i1);
13
14 f1 = i2; // integer to floating conversion
15 NSLog(@"%i assigned to a float produces %f", i2, f1);
16
17 f1 = i2 / 100; //integer divided by integer
18 NSLog(@"%i divided by 100 produces %f", i2, f1);
19
20 f2 = i2 / 100.0; //integer divided by a float
21 NSLog(@"%i divided by 100.0 produces %f", i2, f2);
22
23 f2 = (float) i2 / 100; //type cast operator
24 NSLog(@"(float) %i divided by 100 produces %f", i2, f2);
25
26 [pool drain];
27 return 0;
28 }
123.125000 assigned to an int produces 123
-150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
-150 divided by 100.0 produces -1.500000
(float) -150 divided by 100 produces -1.500000
02
03 #import <Foundation/Foundation.h>
04
05 int main (int argc, const char * argv[]) {
06 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
07 int a = 25, b = 5, c = 10, d = 7;
08
09 NSLog(@"a %% b = %i", a % b);
10 NSLog(@"a %% c = %i", a % c);
11 NSLog(@"a %% d = %i", a % d);
12 NSLog(@"a / d * d + a %% d = %i", a / d * d + a % d);
13
14 [pool drain];
15 return 0;
16 }
a % b = 0
a % c = 5
a % d = 4
a / d * d + a % d = 25
02
03 #import <Foundation/Foundation.h>
04
05 int main (int argc, const char * argv[])
06 {
07 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
08 float f1 = 123.125, f2;
09 int i1, i2 = -150;
10
11 i1 = f1; // floating to integer conversion
12 NSLog(@"%f assigned to an int produces %i", f1, i1);
13
14 f1 = i2; // integer to floating conversion
15 NSLog(@"%i assigned to a float produces %f", i2, f1);
16
17 f1 = i2 / 100; //integer divided by integer
18 NSLog(@"%i divided by 100 produces %f", i2, f1);
19
20 f2 = i2 / 100.0; //integer divided by a float
21 NSLog(@"%i divided by 100.0 produces %f", i2, f2);
22
23 f2 = (float) i2 / 100; //type cast operator
24 NSLog(@"(float) %i divided by 100 produces %f", i2, f2);
25
26 [pool drain];
27 return 0;
28 }
123.125000 assigned to an int produces 123
-150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
-150 divided by 100.0 produces -1.500000
(float) -150 divided by 100 produces -1.500000