0%

Objective-C基础入门(对象)

文件扩展名

扩展 含义
.c C语言源文件
.cc, .cpp C++语言源文件
.h 头文件
.m Objective-C源文件
.mm Objective-C++源文件
.pl Perl源文件
.o Object (已编译的) 文件

基础实例

导入包/库

导入Foundation目录下的Foundation.h文件
import <Foundation/Foundation.h>

main函数

int main (int argc, const char *argv[]) {
@autoreleasepool {}
return 0;
}

打印

打印常量
打印 NSString对象 @”Hello, World!”
NSLog(@"Hello, World!");

打印变量
int sum;
sum = 50 +25;
NSLog(@'The sum of 50 and 25 is %i', sum);

类, 对象, 方法

对象:类的唯一表示
实例:类的具体表示
方法:在实例上执行的操作

示例代码

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
// Program to work with fractions – class version
#import <Foundation/Foundation.h>

//---- @interface section ----
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

//---- @implementation section ----
@implementation Fraction {
int numerator;
int denominator;

}

-(void) print {
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}
@end

//---- program section ----
int main (int argc, char * argv[]) {

@autoreleasepool {
Fraction *myFraction;
// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
}

return 0;

}

类和方法的申明和调用

⚠️ OC是大小写敏感的

1、@interface section(预申明类和方法)

@interface section 描述类和方法
一般形式:

1
2
3
@interface NewClassName: ParentClassName
propertyAndMethodDeclrations
@end

示例代码:

1
2
3
4
5
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

第一行:申明了一个类Fraction,其父类为NSObject,这个父类在NSObject.h中被定义,此文件被包含在Foundation.h中
第二行: - 表示申明了一个实例方法(对类的实例执行的方法),+ 表示申明了一个类方法(对类本身执行的方法)
第三行: (void) 表示返回值为空
第四行: 申明了一个返回值为空,名叫setDenominator的实例方法,它的参数是d,类型为int
第五行:以@end结尾

2、@implementation section (具体申明类和方法)

@implementation section 描述数据和实施interface申明类和方法的实际代码
一般形式:

1
2
3
4
5
@implementation NewClassName {
memberDeclarations;
}
methodDefinitions;
@end

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@implementation Fraction {
int numerator;
int denominator;

}

-(void) print {
NSLog (@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n {
numerator = n;
}

-(void) setDenominator: (int) d {
denominator = d;
}
@end

第一行:类后面的父类可跟,可不跟
第二、三行:申明实例变量(实例化一个类时被创建,和实例对应)
扩展的函数:在{}申明函数的具体内容
最后以@end结尾

3、program section 包含执行的程序代码 (实例化类并调用方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main (int argc, char * argv[]) {   

@autoreleasepool {
Fraction *myFraction;

// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];

// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
}

return 0;

}

实例化并初始化一个对象方法一

1、申明一个指针(即存储一个地址)myFraction,指针指向的地址,用于储存Fraction类的值
Fraction *myFraction;

2、发送消息给Fraction类,分配一个内存空间(将所有实例变量都置零),这里已经实例化完成myFraction
myFraction = [Fraction alloc];

3、发送消息给myFraction,初始化
myFraction = [myFraction init];

实例化并初始化一个对象方法二

合并以上代码
Fraction *myFraction = [[Fraction alloc] init];

实例化并初始化一个对象方法三

合并alloc和init
Fraction *myFraction = [Fraction new];

调用类和方法

一般形式:
[ ClassOrInstance method ];
如: [ yourCar new ];

[ ClassOrInstance method: argument ];
如: [ yourCar setSpeed: 55 ];

示例代码:

1
2
3
[myFraction setNumerator: 1];     
[myFraction setDenominator: 3];
[myFraction print];

第一行:向myFraction发送消息setNumerator,参数为1;然后向setNumerator发送消息,参数为1;之后会调用Fraction类的setNumerator方法,此时1存在变量n中,最后将此值存到实例变量numerator中

基本的访问器方法(setter和getter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@interface Fraction: NSObject
- (void) setNumerator: (int) n;
- (int) numerator;
@end

@implementation Fraction {
int numerator;
}

- (void) setNumerator: (int) n {
numerator = n;
}

- (int) numerator {
return numerator;
}

@end