极客班 Objective-C 高级编程(一)

@段松 老师有7年的iOS研发经验,一线大牛我喜欢,奇幻之旅就正式开始吧。

iOS 开发和盖房子差不多,那么我们就先学习一些基本建筑材料:数据类型与操作符。后面还有一些数组、运算符、流程控制语句、枚举、结构体和宏定义等。

虽然这些之前都有学过,也算温故知新吧。和 Swift 相比确实繁琐古老的一些。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
NSInteger age = 18;
NSInteger testAge;
testAge = age >= 18 ? 1 : 0;


if (testAge) {
NSLog(@"你已经是个成年人了");
} else {
NSLog(@"你还是未成年人");
}

char name[] = "Will";
printf(name);

NSInteger price = 4;
switch (price) {
case 4: {
NSLog(@"four");
NSLog(@"more than one line.");
break;
}
case 3:
NSLog(@"three");
break;
case 2:
NSLog(@"two");
break;
default:
break;
}

bool isFemale = false;
BOOL isMale = YES;
if (isMale) {
if (!isFemale) {
NSLog(@"You are a ture man.");
}
}

// 循环
int sum = 0;
for (int i = 1; i <= 100; ++i) {
sum += i;
}
NSLog(@"%i", sum);

int sum1 = 0;
int j = 1;
for ( ; ; ) {
sum1 += j;
j++;
if (j > 100) {
break;
}
}
NSLog(@"%i", sum1);


// 枚举
enum Person {GoodGirl, GodBoy, BadGirl, BadBoy};
enum Person someone;
someone = GodBoy;
if (someone != BadBoy && someone != BadGirl) {
NSLog(@"Someone is a good person.");
}


// 结构体
typedef struct {CGFloat x; CGFloat y;} MyPoint;
MyPoint myPoint = {6, 8};
CGFloat result = myPoint.x + myPoint.y;
NSLog(@"result = %f", result);

// #define 宏:文本替换

#define PI 3.1415926
#define SQUARE(x) ((x)*(x))

NSLog(@"%f", PI * SQUARE(5 - 4)); // 3.141593
NSLog(@"%.7f", PI * SQUARE(5 - 4)); // 3.1415926
NSLog(@"%e", PI * SQUARE(5 - 4)); // 3.141593e+00

// #import 导入文件
// #import "example.h"

// 条件编译
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif