# Objective-C

**Objective-C** 是一种面向对象的编程语言，是 C 语言的严格超集，在 C 的语法、基本类型和控制流之上加入了 [Smalltalk](https://www.edgepedia.org/zh/smalltalk) 风格的消息传递、类和动态运行时。它由 Brad Cox 和 Tom Love 于 1980 年代初创造，1983 年由 Cox 的公司 Productivity Products International（后更名为 Stepstone）首次发布，在 Swift 问世之前，一直是 Apple 的 OS X 和 iOS 平台上软件的主要语言。<sup>[1](https://computerhistory.org/blog/the-deep-history-of-your-apps-steve-jobs-nextstep-and-early-object-oriented-programming/)</sup><sup> • </sup><sup>[2](https://archive.computerhistory.org/resources/access/text/2024/06/102739376-05-0001-acc.pdf)</sup><sup> • </sup><sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/index.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由 Brad Cox 和 Tom Love 于 1980 年代初创造；1983 年由 Productivity Products International 首次发布<sup>[1](https://computerhistory.org/blog/the-deep-history-of-your-apps-steve-jobs-nextstep-and-early-object-oriented-programming/)</sup><sup> • </sup><sup>[2](https://archive.computerhistory.org/resources/access/text/2024/06/102739376-05-0001-acc.pdf)</sup> |
| 语言模型 | C 的严格超集；将同样的扩展施加于 C++ 则产生 Objective-C++<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup> |
| 消息传递 | 消息表达式写在方括号中，`[receiver message]`，由 Objective-C 运行时系统在运行时解析<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup> |
| 内存管理 | 自动引用计数（ARC），由编译器插入 retain 和 release 调用；ARC 不提供循环收集器<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup> |
| 标准化 | Objective-C 和 Objective-C++ 均无正式成文标准；Apple 的文档和 Clang 规范充当参考<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc.pdf)</sup> |
| 运行时 | GCC 默认使用 GNU Objective-C 运行时，它与 Apple 系统上使用的 Apple/NeXT 运行时不同；通过编译器标志在两者之间选择<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc.pdf)</sup> |
| Swift 互操作 | 在应用目标中通过桥接头文件、在框架目标中通过伞头文件，将 Objective-C 代码暴露给 Swift<sup>[7](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift?changes=_4)</sup> |

## 历史

Brad Cox 和 Tom Love 在 ITT 公司编程技术中心任职期间接触到了 Smalltalk，他们创造这门语言是对 C 的回应，而非对 C++ 的回应——他们起步时 C++ 尚不存在；他们的目标是克服 C 在构建可复用软件组件方面的局限。<sup>[8](https://www.oreilly.com/library/view/objective-c/9780470479223/ch01.html)</sup><sup> • </sup><sup>[9](https://cse.sc.edu/~mgv/csce330f22/pres/az_10.pdf)</sup> Cox 的早期工作是面向对象预编译器（Object-Oriented Precompiler，OOPC），发表于 1983 年的一篇论文，它将 Smalltalk 的面向对象思想与 C 结合起来。<sup>[10](https://s3.computerhistory.org/core/core-2017.pdf)</sup> 这门语言于 1983 年由 Cox 的 Productivity Products International 首次发布，作为 C 的机器无关的面向对象扩展。<sup>[2](https://archive.computerhistory.org/resources/access/text/2024/06/102739376-05-0001-acc.pdf)</sup>

设计上有意保持 Smalltalk 层轻薄。Cox 称早期版本完全极简，OOPC 的大部分最初是一组 UNIX shell 脚本，目标是提供在 C 之上创建类 Smalltalk 环境所需的最小必要量。<sup>[11](https://archive.computerhistory.org/resources/access/text/2017/07/102717175-05-01-acc.pdf)</sup> 方括号消息语法即来自这一时期；两级设计的想法——C 过程层之上一个面向对象的 Smalltalk 层——被归功于 Tom Love，而 Cox 回忆说，选择方括号等字符是通过测试哪些字符不会在 Lex 和 Yacc 解析器工具中触发解析冲突而决定的。<sup>[11](https://archive.computerhistory.org/resources/access/text/2017/07/102717175-05-01-acc.pdf)</sup>

Objective-C 最初由 Stepstone 授权给 NeXT，用于该公司的 NEXTSTEP 操作系统，后来支撑了 Apple 的 OS X 和 iOS 操作系统。<sup>[10](https://s3.computerhistory.org/core/core-2017.pdf)</sup> 与 Smalltalk 相比，这门语言具有实际优势：Smalltalk 程序无法独立运行，需要完整的运行时环境——一个虚拟机——因而资源消耗大；而 Objective-C 程序运行速度与传统的 C 程序一样快，内存占用相当，并且可以使用已经为 Unix 和其他平台编写的数百个 C 库。<sup>[1](https://computerhistory.org/blog/the-deep-history-of-your-apps-steve-jobs-nextstep-and-early-object-oriented-programming/)</sup>

## 语言设计

作为 C 的超集，Objective-C 继承了该语言的语法、基本类型和控制流语句，然后为其扩展出声明类和方法的语法，提供对象图管理和对象字面量的语言级支持，并加入将许多职责推回运行时的动态类型和动态绑定。<sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/index.html)</sup> 这些扩展足够显著，以至于 Clang 文档将 Objective-C 视为不同于 C 的语言，而将同样的扩展施加于 C++ 则产生 Objective-C++。<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup>

**消息传递。** 该语言的核心操作是消息发送，写在方括号中：`[someObject doSomething]` 向对象 `someObject` 发送 `doSomething` 消息，运行时系统从接收者的方法集合中选择合适的方法并调用它。<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup> 由于消息中的方法名选择了一个方法实现，消息中的方法名被称为选择器。<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup>

**选择器。** 选择器有两层含义：源代码中使用的方法名，以及编译代码时取代该名称的类型为 `SEL` 的唯一标识符。为提高效率，编译后的代码中不使用完整的 ASCII 名称；编译器将每个方法名写入一张表，并为其配对一个唯一的运行时标识符，所有同名方法共享同一选择器。`@selector()` 指令在编译时引用编译后的选择器，`NSSelectorFromString` 在运行时将字符串转换为选择器。<sup>[12](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html)</sup>

**动态行为。** 该语言尽可能多地将决策从编译时和链接时推迟到运行时，动态执行创建对象、确定调用哪个方法等操作；运行时系统充当该语言的一种操作系统。<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup> 动态绑定延伸到消息本身：`performSelector:` 系列方法接受一个 `SEL` 标识符作为参数，因此接收者和消息都可以是运行时确定的变量。这一能力是 AppKit 用户界面控件所使用的 target-action 设计模式的基础。<sup>[12](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html)</sup> 不响应某消息的对象也可以安排将其转发给另一个对象，在调用者看来就好像它直接处理了该消息。<sup>[12](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html)</sup>

**协议与分类。** 协议声明独立于任何特定类的方法和属性，定义一种消息传递契约，例如视图对其数据源调用的方法；方法默认是必需的，可以用 `@optional` 指令标记为可选，此时调用者在运行时用 `respondsToSelector:` 检查是否符合。<sup>[13](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html)</sup> 分类可以向任何类添加方法，包括无法获得原始实现源代码的类，例如 `NSString` 等框架类。<sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/index.html)</sup>

## 内存管理

Objective-C 通过对象所有权的概念，使用引用计数来管理其对象的内存。Apple 的文档描述了三种机制：自动引用计数（ARC），由编译器推断对象的生命周期；手动引用计数（MRC，有时称为手动 retain/release），由程序员决定对象生命周期；以及垃圾回收，iOS 上不可用。<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup>

**ARC。** 自动引用计数为 Objective-C 对象和 block 实现自动内存管理，使程序员无需显式插入 retain 和 release。它不提供循环收集器，因此用户必须手动或借助弱引用、不安全引用来打破引用循环。ARC 通过编译器标志 `-fobjc-arc` 启用，通过 `-fno-objc-arc` 禁用，启用时预处理器宏 `__has_feature(objc_arc)` 展开为 1。<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup> 使用 ARC 编译时，编译器接收程序员创建的引用，并自动插入对底层内存管理机制的调用。<sup>[14](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectOwnership.html)</sup>

**强引用与弱引用。** 强引用使对象在内存中保持存活；当指向某对象的强引用全部消失时，该对象被释放。弱引用对所指对象的生命周期没有影响：对象被释放时，指向它的所有弱引用被置为 `nil`，因此弱引用有时被称为归零弱引用。弱引用用 `__weak` 限定符创建。强引用与弱引用的选择遵循所有权：父视图持有对其子视图的强引用，而子视图持有对其父视图的弱引用，视图持有对其委托和数据源的弱引用。<sup>[14](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectOwnership.html)</sup> OS X v10.6 和 iOS 4 不支持弱属性，那里改用 `assign`。<sup>[15](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html)</sup>

**运行时支持。** ARC 运行时与编译器生成代码之间的交互，在 Clang 规范中被描述为语言特定的 ABI 补充，类似于 C++ 的 Itanium 通用 ABI，而非 ARC 语言规范的一部分。运行时跟踪持有非空值的 `__weak` 对象，编译器发出对 `objc_storeStrong` 和 `objc_storeWeak` 等运行时入口点的调用；其中若干函数在语义上等价于一次消息发送，但发出的是 C 函数调用，因为机器码显著更小，函数更容易被 ARC 优化器识别，并且成熟的运行时可以在常见情况下避免消息发送。<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup>

## Block

Block 是 C 语言的一种扩展，用于创建匿名函数，通过 block 指针与之交互。block 可以捕获局部变量的值，这需要动态分配：初始分配在栈上进行，运行时的 `Block_copy` 函数将 block 复制到堆上，引用计数为 1，若已在堆上则递增计数；配对的 `Block_release` 递减计数，并在计数归零时销毁对象。无论 Objective-C 运行时是否运作，所有 block 都被构造为 Objective-C 对象，block 引用类型被扩展为可在任何能使用 `id` 的地方使用。<sup>[4](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)</sup><sup> • </sup><sup>[16](https://clang.llvm.org/docs/BlockLanguageSpec.html)</sup>

## 实现与运行时

该语言不仅需要编译器，还需要运行时系统来执行编译后的代码，不同的运行时以不同方式实现消息传递。<sup>[5](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)</sup><sup> • </sup><sup>[6](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc.pdf)</sup> GCC 同时处理经典的 Objective-C（Objective-C 1.0）和 Objective-C 2.0 的新增特性——属性、快速枚举以及 `@optional` 和 `@required` 协议关键字——除非另有指示，它链接到 GNU Objective-C 运行时，这是一个随 GCC 捆绑的库，与 Apple 机器上的 Apple/NeXT 运行时不同；`-fgnu-runtime` 和 `-fnext-runtime` 选项在两种运行时之间选择。<sup>[6](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc.pdf)</sup> Clang 还支持在 Objective-C 中使用 ARC 风格的弱引用和不安全引用，即使在 ARC 模式之外，弱引用通过 `-fobjc-weak` 选项启用。<sup>[17](https://clang.llvm.org/docs/LanguageExtensions.html)</sup>

## 与 Swift 的互操作

无论项目最初使用哪种语言，Objective-C 和 Swift 文件都可以在同一项目中共用。要在应用目标中从 Swift 使用 Objective-C 声明，需在 Objective-C 桥接头文件中列出 Objective-C 头文件，这使这些声明自动可从目标中的任何 Swift 文件使用，无需 import 语句。在框架目标中，框架的伞头文件导入每个要暴露的 Objective-C 头文件，并启用 Defines Module 构建设置。<sup>[7](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift?changes=_4)</sup> 并发检查在另一方向增加了约束：用 `@objc` 暴露给 Objective-C 的 actor 隔离的 `async` 方法导入时带有 completion handler 参数，并且由于 Objective-C 不了解 actor 隔离，跨越该边界的类型必须是 `Sendable`，否则在 Swift 6 语言模式下会报错。<sup>[18](https://docs.swift.org/latest/documentation/diagnostics/non-sendable-types-and-objective-c-interoperability/)</sup>

## References

1. [The Deep History of Your Apps: Steve Jobs, NeXTSTEP, and Early Object-Oriented Programming, Computer History Museum](https://computerhistory.org/blog/the-deep-history-of-your-apps-steve-jobs-nextstep-and-early-object-oriented-programming/)
2. [Objective-C, Computer History Museum archive (2022)](https://archive.computerhistory.org/resources/access/text/2024/06/102739376-05-0001-acc.pdf)
3. [About Objective-C, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/index.html)
4. [Objective-C Automatic Reference Counting (ARC), Clang documentation](https://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=objective)
5. [Objects, Classes, and Messaging, The Objective-C Programming Language (archive), Apple](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html)
6. [Using the GNU Compiler Collection (GCC), section 2.3 Objective-C and Objective-C++ Languages](https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc.pdf)
7. [Importing Objective-C into Swift, Apple Developer Documentation](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift?changes=_4)
8. [Objective-C, O'Reilly chapter 1: Introducing Objective-C](https://www.oreilly.com/library/view/objective-c/9780470479223/ch01.html)
9. [Objective-C: Brad Cox interview (presentation slides)](https://cse.sc.edu/~mgv/csce330f22/pres/az_10.pdf)
10. [History of Objective-C essay, CHM Core magazine (2017)](https://s3.computerhistory.org/core/core-2017.pdf)
11. [Oral History of Brad Cox, Computer History Museum (2016)](https://archive.computerhistory.org/resources/access/text/2017/07/102717175-05-01-acc.pdf)
12. [Selectors, The Objective-C Programming Language (archive), Apple](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocSelectors.html)
13. [Working with Protocols, Programming with Objective-C (archive), Apple](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html)
14. [Object ownership, Apple Developer Documentation Archive](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectOwnership.html)
15. [Declared Properties, The Objective-C Programming Language (archive), Apple](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html)
16. [Language Specification for Blocks, Clang documentation](https://clang.llvm.org/docs/BlockLanguageSpec.html)
17. [Clang Language Extensions, Clang documentation](https://clang.llvm.org/docs/LanguageExtensions.html)
18. [Non-Sendable types and Objective-C interoperability, Swift documentation](https://docs.swift.org/latest/documentation/diagnostics/non-sendable-types-and-objective-c-interoperability/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/objective-c

English original: https://www.edgepedia.org/objective-c
