跳转至

Objective-C笔记

https://zh.wikipedia.org/wiki/Objective-C

HelloWorld

Objective-C是C语言的超集,完全兼容标准C语言并在其基础上添加了面向对象,消息机制以及反射等。 IDE 不用说了自然是Xcode,先敲个简单的"hello world":

Objective-C
#import <Foundation/Foundation.h>

int main(int argc, char *argv[]){
    @autoreleasepool{
        NSLog(@"Hello World!");
    }
    return 0;
}

简单分析下代码:

  • #import <Foundation/Foundation.h> 和C语言include相似,不同的是import会自动防止重复包含。Foundation.h 头文件中包含的是macOS系统提供的基础框架的接口声明。
  • @autoreleasepool 是Objective-C中用于管理内存的机制,在其包含的代码块中申请内存位于本线程的一个自动释放的内存池上,会在内存池销毁的时候自动回收。
  • 字符串字面量在 Objective-C 是这么表示的@"strings" 不像C中是个char数组而是一个 NSString 类。 编译命令:
    Bash
    # objc 编译时需要指定framework
    clang -framework Foundation hello.m -o hello.o
    

Objective-C的面向对象语法源自 SmallTalk,消息传递(Message Passing)风格。在源码风格方面,这是它与C Family语言(包括C/C++、Java、PHP)差别最大的地方。

类的定义与实现

在 objc 中,强制要求将类分为声明interface和实现implementation两个部分。声明放在 .h 文件,实现放在 .m 文件中。

在 objc 中大部分类都是直接或间接的继承 NSObject 。这个类遵循 NSObject 协议,提供了一些通用的方法(初始化、垃圾回收等),通过继承 NSObject,可以从其中继承访问运行时的接口,并让对象具备 objc 对象的基本能力。

Objective-C
#import <Foundation/Foundation.h>

// SampleClass 类的定义
@interface SampleClass:NSObject
- (void)sampleMethod;
@end

// SampleClass 类的实现
@implementation SampleClass
- (void)sampleMethod {
   NSLog(@"Hello, World! \n");
}
@end

int main() {
   // 实例 SampleClass
   SampleClass *sampleClass = [[SampleClass alloc]init];
   // 传递 sampleMethod 消息
   [sampleClass sampleMethod];
   return 0;
}

interface

Objective-C
@interface Hello: NSObject {
    int memberVar; // 实体变量
}

+ (return_type) class_method; // 类方法
- (return_type) instance_method1; // 一般的实例方法
- (return_type) instance_method2: (int) p1; // 单个参数
- (return_type) instance_method3: (int) p1 andPar: (int) p2; // 多个参数
@end

implementation

Objective-C
@implementation MyObject {
  int memberVar3; //私有实体变量
}

+(return_type) class_method {
    .... //method implementation
}
-(return_type) instance_method1 {
     ....
}
-(return_type) instance_method2: (int) p1 {
    ....
}
-(return_type) instance_method3: (int) p1 andPar: (int) p2 {
    ....
}
@end

Interface, Implementation 定义的实体变量两者区别在于访问权限的不同; - Interface: 默认权限为 protected - Implementation: 默认权限为 private

在 Implementation 区段定义私有成员更符合面向对象之封装原则,因为如此类别之私有信息就不需曝露于公开 interface(.h文件)中。

创建对象

Objective-C
MyObject *my = [[MyObject alloc]init];

自定义初始化

Objective-C
- (id) init {
    if ( self=[super init] ) {   // 必须调用父类的init
        // do something here ...
    }
    return self;
}

协议

协议是一组没有实现的方法列表,任何的类均可采纳协议并具体实现这组方法。

在Objective-C中,有两种定义协议的方式:由编译器保证的“正式协议”,以及为特定目的设定的“非正式协议”。

非正式协议 先忽略

正式协议

任何类都可以申明实现某个协议

Objective-C
@protocol Locking
- (void)lock;
- (void)unlock;
@end
Objective-C
@interface SomeClass: SomeSuperClass <Locking> // SomeClass宣称他采纳了Locking协议
@end

// 义务实现 Locking 协议的两个方法
@implementation SomeClass
- (void)lock{

}
- (void)unlock{

}

动态类型

下面三个方法,差异仅在于参数的形态:

- setMyValue:(id) foo; id形态表示参数“foo”可以是任何类的实例。

- setMyValue:(id <aProtocol>) foo; id表示“foo”可以是任何类的实例,但必须采纳“aProtocol”协议。

- setMyValue:(NSNumber*) foo; 该声明表示“foo”必须是“NSNumber”的实例。


最后更新: 2023-02-07