AppKit vs SwiftUIAppKit vs SwiftUI

/ NSButton · Button /

MenuBarExtraSwiftUI = NSStatusItemAppKit

AppKit is the Mac’s original toolbox — a workshop where you pick up each part (a window, a button, a menu) and tell it exactly what to do. Its names start with NS: NSWindow, NSMenu, NSStatusItem.

AppKit 是 Mac 最早的一套工具箱——像一间工坊:你拿起每个零件(一个窗口、一个按钮、一个菜单),一件件告诉它该做什么。它的名字都以 NS 开头:NSWindow、NSMenu、NSStatusItem。

SwiftUI is Apple’s newer way: instead of assembling parts, you describe the finished room — “a sidebar, then a list, then this button” — and the system builds and updates it. Same elements on screen, plainer names: Button, Menu, MenuBarExtra.

SwiftUI 是 Apple 的新方式:你不再拼装零件,而是描述造好的房间——“一个边栏,接着一个列表,再放上这个按钮”——由系统负责构建和更新。屏幕上的元素相同,名字却更朴素:Button、Menu、MenuBarExtra。

They’re not two kinds of app — one Mac app can (and usually does) mix both. Which means the thing you’re pointing at almost always has two valid names, and using the right one makes your agent dramatically more precise.

它们不是两类 App——一个 Mac App 可以(而且通常确实)两者混用。这意味着你指着的那个东西几乎总有两个都成立的名字;用对名字,能让你的 AI 代理精确得多。

Which name do I say? — three rules该说哪个名字?三条规则

  1. 1

    Match the project.

    跟着项目走。

    Files say import SwiftUI and views are structs? Use SwiftUI names. NS-classes and import AppKit? Use AppKit names.

    文件里是 import SwiftUI、视图是 struct?就用 SwiftUI 的名字。是 NS 开头的类和 import AppKit?就用 AppKit 的名字。

  2. 2

    In a mixed app, name the layer.

    混用的 App,指明要哪一层。

    “The SwiftUI view inside the AppKit window”, “the NSPanel presented from my SwiftUI app.” The boundary is where agents get lost.

    比如说“AppKit 窗口里的那个 SwiftUI 视图”“从我的 SwiftUI App 里弹出的那个 NSPanel”。AI 代理最容易在两层交界处迷路。

  3. 3

    When unsure, give both.

    拿不准,就两个都给。

    “The menu bar extra (SwiftUI: MenuBarExtra / AppKit: NSStatusItem)” — the agent inspects the project and picks the right one.

    比如说“菜单栏附加项(SwiftUI: MenuBarExtra / AppKit: NSStatusItem)”——代理会检查项目,自己挑出对的那个。

Worked prompts — paste into your agent写好的 prompt——直接粘给你的代理

SwiftUI projectSwiftUI 项目

In this macOS SwiftUI app, make the MenuBarExtra open a popover-style window, and keep the menu bar extra visible when the main window closes.

在这个 macOS SwiftUI App 里,让 MenuBarExtra 打开一个 popover 风格的窗口,并在主窗口关闭后仍然保留菜单栏附加项。

AppKit projectAppKit 项目

In this AppKit macOS app, update the NSStatusItem in NSStatusBar.system: its button should use a template image and show an NSMenu when clicked.

在这个 AppKit macOS App 里,更新 NSStatusBar.system 中的 NSStatusItem:它的按钮使用模板图像(template image),点击时显示一个 NSMenu。

Not sure which不确定是哪个

Fix the little app icon beside the Mac clock — the menu bar extra (SwiftUI: MenuBarExtra, AppKit: NSStatusItem). Inspect the project and change the existing implementation, don't add a second one.

修好 Mac 时钟旁边那个小小的 App 图标——也就是菜单栏附加项(SwiftUI: MenuBarExtra / AppKit: NSStatusItem)。检查项目,在现有实现上改,不要再加第二个。

Don’t confuse them with别和它们搞混

The questions everyone asks人人都会问的问题

Is SwiftUI replacing AppKit?SwiftUI 正在取代 AppKit 吗?

Slowly, and not yet. Apple recommends SwiftUI for new apps but still ships and documents new AppKit APIs. Practical rule: new screens in SwiftUI; reach for AppKit when the Mac-specific behavior you need isn't exposed in SwiftUI.

在取代,但很慢,而且还没完成。Apple 建议新 App 用 SwiftUI,但仍在推出并为新的 AppKit API 写文档。实用原则:新界面用 SwiftUI;需要的 Mac 特有行为在 SwiftUI 里没暴露出来时,就去找 AppKit。

Can one app use both?一个 App 能两个都用吗?

Yes, and most serious Mac apps do. NSViewRepresentable puts an AppKit view inside SwiftUI; NSHostingView does the reverse. That's why naming the layer you want changed matters.

能,而且大多数认真的 Mac App 都这么做。NSViewRepresentable 把 AppKit 视图放进 SwiftUI,NSHostingView 则反过来。所以说清要改的是哪一层,才显得重要。

Is every SwiftUI control secretly an AppKit control?每个 SwiftUI 控件背地里都是 AppKit 控件吗?

Don't assume so. Some are bridged, many aren't, and Apple treats it as an implementation detail. Treat Button and NSButton as different APIs that happen to look alike.

别这么假设。有些做了桥接,很多没有,Apple 把这当作实现细节。把 Button 和 NSButton 当成两个碰巧长得像的 API 就好。

Are AppKit and UIKit the same thing?AppKit 和 UIKit 是一回事吗?

No — AppKit is Mac, UIKit is iPhone/iPad. The prefix gives it away: NSButton is Mac, UIButton is iOS. Mac Catalyst is a third thing: an iPad app wearing a Mac coat.

不是——AppKit 属于 Mac,UIKit 属于 iPhone/iPad。看前缀就露馅:NSButton 是 Mac,UIButton 是 iOS。Mac Catalyst 是第三种东西:披着 Mac 外衣的 iPad App。

Every macOS entry on NameThatUI lists both names in its IN CODE table, and the Translation Table holds all 60+ mappings in one place — start from the thing you can see, take whichever name your project needs.

NameThatUI 上每个 macOS 词条的 IN CODE 表格都列出两个名字;翻译对照表(Translation Table)把 60 多组映射收在一处——从你看得见的东西出发,拿走你项目需要的那个名字。