# Cocoa（API）

Cocoa 是苹果（Apple）用于在 macOS 上构建软件的面向对象应用框架，主要由 Foundation 和 AppKit 框架组成，主要用 [Objective-C](https://www.edgepedia.org/zh/objective-c) 和 Swift 编写。它在 iOS 上的对应物 Cocoa Touch 将 Foundation 与 UIKit 框架配对，用于 iPhone 和 iPad 应用。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup><sup> • </sup><sup>[2](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Cocoa.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心框架 | macOS 上为 Foundation 和 AppKit；iOS 上的 Cocoa Touch 中为 Foundation 和 UIKit。没有 Foundation 就无法构建任何 Cocoa 应用；macOS 开发需要 AppKit，而 iOS 开发需要 UIKit。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> |
| 命名前缀 | Foundation 和 AppKit 中的类、函数、数据类型和常量带有“NS”前缀；UIKit 中的则带有“UI”前缀。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> |
| 起源 | 由 NeXT 和 Sun 于 1994 年 9 月发布的官方 OpenStep API，首次将 API 划分为 Foundation 和 Application Kit，并首次使用“NS”前缀。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> |
| 更名 | 苹果于 1997 年收购 NeXT Software 后，OpenStep 成为 Yellow Box，随 OS X Server（Rhapsody）和 Windows 一同发布，并最终更名为 Cocoa。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> |
| 64 位过渡 | 在 OS X 10.5 版中，Cocoa 框架被移植到 64 位寻址，引入了 NSInteger、NSUInteger 和 CGFloat 类型。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> |
| 内存管理 | Objective-C 提供手动保留-释放，其基于 NSObject 提供的引用计数；还提供自动引用计数（Automatic Reference Counting，ARC），后者在编译时插入内存管理调用。<sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html?language=objc)</sup> |
| SwiftUI 互操作 | AppKit 视图和视图控制器可以嵌入 SwiftUI 界面，而 SwiftUI 视图可以通过 NSHostingView 和 NSHostingController 托管在 AppKit 应用中。<sup>[4](https://developer.apple.com/documentation/swiftui/appkit-integration?changes=l_7_6)</sup> |

## 框架结构

Cocoa 围绕两个基本框架之间的分工来组织。Foundation 定义了可在任何 Cocoa 程序中使用的类的基础层：它确立基本对象行为以及内存管理、可变性和通知的约定，为数字、字符串和集合等原始类型提供对象包装器，并支持持久化、对象分发、使用 Unicode 字符串和资源包的国际化，以及为可移植性提供一定程度的操作系统独立性。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> 区分 Foundation 与应用级框架的标准是用户界面：如果一个对象不出现在用户界面中，或并不专门支持用户界面，那么它的类就属于 Foundation。程序可以仅用 Foundation 构建，例如命令行工具和 Internet 服务器。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup>

AppKit 提供为 macOS 应用构建图形化、事件驱动用户界面所需的对象。除了绘制窗口、按钮、面板和文本字段之外，它还处理事件管理以及应用、用户和 macOS 之间的交互，并负责打印、动画，以及高效创建包含大量数据的文档。它包含对本地化和辅助功能的内建支持。<sup>[5](https://developer.apple.com/documentation/appkit?language=objc)</sup>

Cocoa 还公开或依赖更低层的技术。许多 Foundation 类基于等价的 Core Foundation 不透明类型，这种紧密关系使得“免费桥接”（toll-free bridging）成为可能，即在兼容的 Core Foundation 与 Foundation 类型之间进行强制转换。AppKit 和 Foundation 借助 Carbon Core 提供某些系统服务，例如用于在文件系统表示之间转换的 File Manager。Cocoa 的绘图和成像类紧密基于 Core Graphics，后者实现 Quartz 和窗口服务器；NSWorkspace 类则公开 Launch Services 的能力，包括为与应用程序和文档相关联的图标进行应用程序注册。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> 当某项能力未通过 Cocoa 的接口公开时，开发者可以直接调用底层框架；这些依赖框架大多具有用标准 [ANSI C](https://www.edgepedia.org/zh/ansi-c) 编写的编程接口，而 Objective-C 是它的超集。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup>

## Cocoa 与 Cocoa Touch

iOS 的应用框架层称为 Cocoa Touch。尽管其下的 iOS 基础设施与 OS X 中的 Cocoa 基础设施相似，但两者之间存在显著差异。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup> AppKit 和 UIKit 为应用提供类似支持，但它们的实现非常不同；将 iOS 应用迁移到 macOS 意味着用 AppKit 功能替换大量 UIKit 类。<sup>[6](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/MigratingFromCocoaTouch/MigratingFromCocoaTouch.html)</sup>

事件模型说明了这种分野。iOS 定义触摸事件和运动事件，而 OS X 定义鼠标事件、键盘事件、触控板事件、数位板事件和跟踪区域事件，其中大多数包含按键抬起和鼠标按下等阶段，或 Control 键被按下等修饰状态。AppKit 确实定义了触摸（NSTouch）对象和手势事件，但这些仅适用于配有受支持触控板的笔记本电脑和台式机。<sup>[6](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/MigratingFromCocoaTouch/MigratingFromCocoaTouch.html)</sup>

## 内存管理

Objective-C 提供两种应用内存管理方法。在手动保留-释放（MRR）中，开发者通过跟踪拥有的对象来显式管理内存，使用 Foundation 类 NSObject 与运行时环境共同提供的引用计数模型。在自动引用计数（ARC）中，系统使用相同的引用计数系统，但在编译时插入适当的内存管理方法调用；苹果的文档强烈鼓励新项目使用 ARC。<sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html?language=objc)</sup> Cocoa 还使用一种命名约定，用以表明调用方何时拥有方法返回的对象；自动释放池块提供了一种向对象发送延迟释放消息的机制，在放弃所有权而不冒立即释放风险时很有用，例如从方法返回对象时。<sup>[3](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html?language=objc)</sup>

Cocoa 还曾提供可选的垃圾回收器，它是分代的：将已分配内存划分为“代”，并优先回收最新代的内存；可达性由初始根集合决定，该集合包括全局变量（含 NSApp）、栈变量和具有外部引用的对象。<sup>[7](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcArchitecture.html)</sup>

## 历史

Cocoa 直接源自 NeXT 的应用框架。许多年前它被称为 [NeXTSTEP](https://www.edgepedia.org/zh/nextstep)，当时还没有 Foundation 框架；其 Application Kit 伴随有 Sound Kit 和 Music Kit，这两个 Objective-C 类库为音频和音乐合成提供对 Display PostScript 层的高层访问。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup>

1993 年初，NeXTSTEP 3.1 被移植到并发布于英特尔（Intel）、Sparc 和[惠普](https://www.edgepedia.org/zh/hewlett-packard)（Hewlett-Packard）计算机上，NeXTSTEP 3.3 则包含 Foundation 的初步版本。到 1993 年前后，Sun 和 NeXT 已在 OpenStep 项目上联手，该项目旨在将 NeXTSTEP 的上层——最值得注意的是 Application Kit 和 Display PostScript——迁移到 Solaris；名称中的“Open”指向两家公司计划共同发布的开放 API 规范。1994 年 9 月发布的官方 OpenStep API 首次将 API 划分为 Foundation 和 Application Kit，并首次使用“NS”前缀；Application Kit 最终简称为 AppKit。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup>

到 1996 年 6 月，NeXT 已发布面向英特尔、Sparc 和惠普计算机的 OpenStep 4.0 版本，以及面向 Windows 系统的 OpenStep 运行时；Sun 也已完成其 Solaris 移植，并将其作为 Network Object Computing Environment 的一部分发布。OpenStep 从未成为 Sun 整体战略的重要部分。苹果于 1997 年收购 NeXT Software 后，OpenStep 成为 Yellow Box，随 OS X Server（又称 Rhapsody）和 Windows 一同提供；随着 OS X 战略的演进，它最终更名为 Cocoa。<sup>[1](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)</sup>

## Cocoa 与 SwiftUI

AppKit 仍是 macOS 的原生界面框架，并且现在可直接与苹果的声明式框架 SwiftUI 互操作。开发者可以将 AppKit 视图和视图控制器放入 SwiftUI 视图中，反之亦然：托管控制器将一组 SwiftUI 视图包装成可添加到基于 storyboard 的 AppKit 应用中的形式，而可表示对象则包装 AppKit 视图或视图控制器以供 SwiftUI 使用，并促进被包装对象与 SwiftUI 视图之间的通信。<sup>[4](https://developer.apple.com/documentation/swiftui/appkit-integration?changes=l_7_6)</sup> AppKit 文档同样指出，该框架可与 SwiftUI 协同工作，因此 AppKit 应用的部分内容可以用 SwiftUI 实现，或在两个框架之间混合界面元素。<sup>[5](https://developer.apple.com/documentation/appkit?language=objc)</sup> 近期的 AppKit 版本继续与 Swift 集成，使 NSColor、NSColorSpace、NSGradient、NSShadow 和 NSTouch 等类型遵循 Sendable 协议，并使 NSImage、NSColor 和 NSSound 遵循 Transferable 协议。<sup>[8](https://developer.apple.com/documentation/updates/appkit?changes=latest_ma_3)</sup>

## References

1. [What Is Cocoa? Apple Developer Documentation (Cocoa Fundamentals Guide)](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html)
2. [Cocoa (Touch), Apple Developer Documentation (DevPedia-CocoaCore)](https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/Cocoa.html)
3. [About Memory Management, Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html?language=objc)
4. [AppKit integration, Apple Developer Documentation](https://developer.apple.com/documentation/swiftui/appkit-integration?changes=l_7_6)
5. [AppKit, Apple Developer Documentation](https://developer.apple.com/documentation/appkit?language=objc)
6. [Migrating from Cocoa Touch, Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/OSX_Technology_Overview/MigratingFromCocoaTouch/MigratingFromCocoaTouch.html)
7. [Garbage Collection Architecture, Apple Developer Documentation](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/GarbageCollection/Articles/gcArchitecture.html)
8. [AppKit updates, Apple Developer Documentation](https://developer.apple.com/documentation/updates/appkit?changes=latest_ma_3)

---
*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: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Mobile and platform frameworks

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/cocoa-api

English original: https://www.edgepedia.org/cocoa-api
