首先申明下,本文为笔者学习《Objective-C 基础教程》的笔记,并加入笔者自己的理解和归纳总结。
1. import语句
#import
可保证头文件只被包含一次。
#import <Foundation/Foundation.h>
2. NSLog和@“字符串”
NSLog
方法与printf
相似,向控制台输出。@
符号表示NSString
类型。
3. 布尔类型(BOOL)
YES
和NO
4. 类
@interface
表示这是类的接口,以@end
结束。@implementation
实现类定义的接口,以@end
结束。- 每个类都是
NSObject
。 - 在
{}
内定义类需要的各种数据成员。 - 短线
-
定义一个方法,后面紧跟返回类型,void
表示无返回值。+
定义一个类方法。 - 方法参数中
()
定义参数类型,紧跟参数名。
示例
@interface ShapeBounds : NSObject {
int width, height;
}
- (void)setWidth:(int)width height:(int)height;
@end
@implementation ShapeBounds
- (void)setWidth:(int)w height:(int)h {
width = w;
height = h;
}
- (NSString *)description {
return [NSString stringWithFormat:@"(%d, %d)", width, height];
}
@end
int main(int argc, const char* argv[]) {
ShapeBounds *bounds = [ShapeBounds new];
[bounds setWidth:100 height:60];
NSLog(@"%@", bounds); // (100, 60)
}
输出
(100, 60)
5. 继承
类声明时,冒号:
后面的标识符是需要继承的类,Objective-C只支持单继承。super
关键字能调用父类的方法。
@interface Shape : NSObject
- (void)draw;
@end
@implementation Shape
- (void)draw {
NSLog(@"draw Shape.");
}
@end
@interface Circle : Shape
@end
@implementation Circle
- (void)draw {
[super draw];
NSLog(@"draw Circle.");
}
@end
int main(int argc, const char* argv[]) {
Shape *circle = [Circle new];
[circle draw];
}
输出
draw Shape.
draw Circle.
6. 复合
Shape
拥有自己的位置,可以用ShapeBounds
表示。
@interface Shape : NSObject {
ShapeBounds *bounds;
}
- (void)setBounds:(ShapeBounds *)bounds;
- (void)draw;
@end
@implementation Shape
- (void)setBounds:(ShapeBounds *)b {
bounds = b;
}
- (void)draw {
NSLog(@"draw Shape at %@.", bounds);
}
@end
int main(int argc, const char* argv[]) {
ShapeBounds *bounds = [ShapeBounds new];
[bounds setWidth:100 height:60];
Shape *circle = [Circle new];
[circle setBounds:bounds];
[circle draw];
}
输出
draw Shape at (100, 60).
draw Circle.
7. 跨文件依赖
ShapeBounds
可以拆分为 “ShapeBounds.h” 和 “ShapeBounds.m”
“ShapeBounds.h” 文件
#import <Foundation/Foundation.h>
@interface ShapeBounds : NSObject {
int width, height;
}
- (void)setWidth:(int)width height:(int)height;
@end
“ShapeBounds.m” 文件
#import "ShapeBounds.h"
@implementation ShapeBounds
- (void)setWidth:(int)w height:(int)h {
width = w;
height = h;
}
- (NSString *)description {
return [NSString stringWithFormat:@"(%d, %d)", width, height];
}
@end
Shape
类分为 “Shape.h” 和 “Shape.m”,在Shape
中引用了ShapeBounds
类,必须在文件中声明依赖关系。@class
可以声明引用关系,在编译时知道这是一个类,但ShapeBounds
被修改时不用重新编译 “Shape.h” 文件。
"Shape.h"文件
#import <Foundation/Foundation.h>
@class ShapeBounds;
@interface Shape : NSObject {
ShapeBounds *bounds;
}
- (void)setBounds:(ShapeBounds *)bounds;
- (void)draw;
@end
“Shape.m” 文件
#import "Shape.h"
#import "ShapeBounds.h"
@implementation Shape
- (void)setBounds:(ShapeBounds *)b {
bounds = b;
}
- (void)draw {
NSLog(@"draw Shape at %@.", bounds);
}
@end
Circle
类分为 “Circle.h” 和 “Circle.m”, “Circle.h” 文件中Shape
类不能用@class
声明。
“Circle.h” 文件
#import "Shape.h"
@interface Circle : Shape
@end
“Circle.m” 文件
#import "Circle.h"
@implementation Circle
- (void)draw {
[super draw];
NSLog(@"draw Circle.");
}
@end
“main.m” 文件
#import "Circle.h"
#import "ShapeBounds.h"
int main(int argc, const char* argv[]) {
ShapeBounds *bounds = [ShapeBounds new];
[bounds setWidth:100 height:60];
Shape *circle = [Circle new];
[circle setBounds:bounds];
[circle draw];
}