iOS学习笔记-01基本语法

1.基本语法

  • 与其它编程语言一样有基本类型,比如int,bool,double,float等等。为了64位的问题需要使用OC中定义的类型,例如NSInteger
1
2
3
4
5
6
7
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
  • 在类的定义上分为Interface和Implement两部分,这个与C的头文件和实现文件是一个概念,只不过写法上还是有区别的。Interface作为头文件的基本写法就是这样的,注意到在@Interface上有一句变量的定义,这个是全局静态变量其用法和C中定义静态变量是一样的,在@Interface的花括号中还定义了两变量,其实@private可有可无,写法与C++是一样的。而property则写在中间,在@Interface花括号区域定义的变量目前发现不能使用类似retain这样的写法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static NSInteger staticIntInFirstViewController;
@interface FirstViewController : UIViewController {
@private
NSInteger privateInt;
@public
NSInteger publicInt;
}
- (void)foo:(NSInteger)arg1;
- (void)foo:(BOOL)arg1 withArg2:(NSInteger)arg2;
+ (void)foo;

- (void)printRetain;
@property (nonatomic, retain) NSArray* arr;
@end

字段和属性的使用方法如下:

1
2
3
4
FirstViewController* f = [[FirstViewController alloc] init];
staticIntInFirstViewController = 1; // 只要引用了头文件,直接使用即可
f->publicInt = 2; // C++的用法
f.arr;
  • 而Implement则是实现部分,Implement中可以添加一些只用于.m文件中的变量。
1
2
3
@implementation FirstViewController {
NSInteger I;
}
  • 类文件的生成:

    UI类:右键→New File→选定平台(iOS/OSX等)→Cocoa Class
    分类(category):右键→New File→选定平台(iOS/OSX等)→Objective-C File→输入名称、选择类型为category,选择基类。(在这里还可以创建协议(protocol)和类扩展(extension))

  • 类的扩展,类的扩展分为分类(category)和使用()的形式来扩展类(class-continuation)。其区别在于category类似一个完整的类文件,有.h和.m。它的文件的名称通常为原类名+分类名,比如
    FirstViewController+Test,其Interface和Implement分别为

1
2
3
4
5
#import "FirstViewController.h"

@interface FirstViewController (Test)

@end
1
2
3
4
5
#import "FirstViewController+Test.h"

@implementation FirstViewController (Test)

@end
注意在分类中可以添加属性,但是会提示 ![](http://images2015.cnblogs.com/blog/23250/201610/23250-20161003182039301-389964144.jpg) 这是因为除了class-continuation其它分类不支持生成实例变量,而且也不建议在分类中定义属性。

而使用()来做的扩展类只是个头文件,形式如下,注意到类名后面有个(),这个里面定义的方法还是需要在主类的.m文件中实现。在class-continuation中可以定义属性、实例变量、方法,其无法被外部类访问,这就实现了细节的隐藏。

1
2
3
4
5
#import "FirstViewController.h"

@interface FirstViewController ()

@end
  • 函数的定义,”-“为实例方法,”+”类方法(静态方法),语法上基本没有什么特别注意的,只是在函数和变量的明面上应该遵循oc命名的习惯
1
2
3
4
5
@interface FirstViewController : UIViewController
- (void)foo:(NSInteger)arg1;
- (void)foo:(BOOL)arg1 withArg2:(NSInteger)arg2;
+ (void)foo;
@end
  • 函数的调用[xxxInstance foo]或者实例函数[xxxClass foo]
1
2
3
4
5
FirstViewController *f = [[FirstViewController alloc] init];
[f foo:1];
[f foo:YES withArg2:1];
[FirstViewController foo];
[f release];
  • 协议(protocol)和代理(delegate),两者是合起来用,代理本质上是一个实现了协议的对象的引用。协议的形式为
1
2
3
@protocol FirstViewDelegate <NSObject>
- (void)didClickSomeUI;
@end

代理的形式为 @property (nonatomic, assign) id<FirstViewDelegate> delegate;

需要注意的是:因为是个对象的引用,因此在使用的时候可能出现互相引用的问题,这样在定义property的时候需要定义为弱引用,防止对象因为互相调用而无法被释放。

  • 在流程控制上一样使用if else, switch, for, do while等
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
- (void)foo:(NSInteger)arg1 {
if (arg1 < 1) {
return;
}
NSLog(@"for start");
for (NSInteger i = 0; i < arg1; i++) {
if (i % 2 == 0) {
NSLog(@"%ld", i);
}
}

NSLog(@"while start");
NSInteger m = 10;
while (m > 0) {
NSLog(@"%ld", m);
m--;
}

NSLog(@"do while start");
do {
NSLog(@"%ld", m);
m++;
} while (m < 10);

switch (arg1) {
case 1:
NSLog(@"");
break;
case 2: {
// do some
} break;
default:
break;
}

}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×