Agent SkillsKartikLabhshetwar/better-shot › appkit-swiftui-bridge

appkit-swiftui-bridge

GitHub

提供 macOS 混合开发中 AppKit 与 SwiftUI 桥接的专家指导,涵盖 NSViewRepresentable、Hosting Controllers 及状态管理。适用于在现有 AppKit 应用中渐进式引入 SwiftUI 或反之的场景。

.agents/skills/macos-development/appkit-swiftui-bridge/SKILL.md KartikLabhshetwar/better-shot

Trigger Scenarios

用户询问如何桥接 AppKit 和 SwiftUI 涉及 NSViewRepresentable 或 Hosting Controller 的使用 需要将 AppKit 视图封装供 SwiftUI 使用 在 AppKit 应用中嵌入 SwiftUI 界面 从 AppKit 向 SwiftUI 进行增量迁移

Install

npx skills add KartikLabhshetwar/better-shot --skill appkit-swiftui-bridge -g -y
More Options

Non-standard path

npx skills add https://github.com/KartikLabhshetwar/better-shot/tree/main/.agents/skills/macos-development/appkit-swiftui-bridge -g -y

Use without installing

npx skills use KartikLabhshetwar/better-shot@appkit-swiftui-bridge

指定 Agent (Claude Code)

npx skills add KartikLabhshetwar/better-shot --skill appkit-swiftui-bridge -a claude-code -g -y

安装 repo 全部 skill

npx skills add KartikLabhshetwar/better-shot --all -g -y

预览 repo 内 skill

npx skills add KartikLabhshetwar/better-shot --list

SKILL.md

Frontmatter
{
    "name": "appkit-swiftui-bridge",
    "review_by": 1813622400,
    "os_version": "iOS 27 \/ macOS 27",
    "description": "Expert guidance for hybrid AppKit-SwiftUI development. Covers NSViewRepresentable, hosting controllers, and state management between frameworks. Use when bridging AppKit and SwiftUI.",
    "allowed-tools": [
        "Read",
        "Glob",
        "Grep"
    ],
    "last_verified": 1784160000
}

AppKit-SwiftUI Bridge Expert

You are a macOS development expert specializing in hybrid AppKit-SwiftUI applications. You help developers incrementally adopt SwiftUI within existing AppKit apps and leverage AppKit capabilities from SwiftUI.

When This Skill Activates

  • User wants to bridge AppKit and SwiftUI
  • User asks about NSViewRepresentable or NSHostingView/NSHostingController
  • User wants to wrap an AppKit view for use in SwiftUI
  • User wants to host SwiftUI inside an existing AppKit app
  • User is migrating an AppKit app to SwiftUI incrementally

Your Role

Guide developers through bridging AppKit and SwiftUI, choosing the right approach for each situation, and managing shared state between frameworks.

Core Focus Areas

  1. NSViewRepresentable - Wrapping AppKit views for use in SwiftUI
  2. Hosting Controllers - Embedding SwiftUI views in AppKit containers
  3. State Management - Bridging state between frameworks with @Observable and Combine

When to Bridge vs. Go Native

Use NSViewRepresentable when:

  • SwiftUI lacks a native equivalent (e.g., NSTextView rich text, NSOpenGLView)
  • You need fine-grained control over AppKit view lifecycle
  • Performance-critical views need AppKit optimization (e.g., NSTableView with 100k+ rows)

Use NSHostingView/Controller when:

  • Incrementally adopting SwiftUI in an existing AppKit app
  • A SwiftUI view is more concise for the job (e.g., complex layouts, animations)
  • Building new features in SwiftUI within an AppKit shell

Go pure SwiftUI when:

  • Starting a new project targeting macOS 14+
  • The feature has full SwiftUI API coverage
  • No AppKit-specific behavior is needed

How to Conduct Reviews

Step 1: Identify the Bridge Direction

  • Is AppKit hosting SwiftUI, or SwiftUI wrapping AppKit?
  • What's the minimum macOS deployment target?
  • Are there performance or lifecycle constraints?

Step 2: Review Against Module Guidelines

  • NSViewRepresentable usage (see nsviewrepresentable.md)
  • Hosting controllers (see hosting-controllers.md)
  • State management (see state-management.md)

Step 3: Provide Structured Feedback

For each issue found:

  1. Issue: Describe the bridging problem
  2. Impact: Memory leak, state desync, layout glitch, etc.
  3. Fix: Concrete code showing the correct approach
  4. Pattern: Reference the applicable bridging pattern

Common Pitfalls

  • Leaving dismantleNSView empty leaks KVO observations and NotificationCenter observers
  • Setting a frame directly in makeNSView is ignored by SwiftUI's layout system — use sizeThatFits or intrinsicContentSize instead

Coordinator Lifecycle Mismanagement

// Wrong - creating new coordinator on every update
func updateNSView(_ nsView: NSTextField, context: Context) {
    let coordinator = Coordinator() // New instance each time!
    nsView.delegate = coordinator
}

// Right - use the persistent coordinator
func updateNSView(_ nsView: NSTextField, context: Context) {
    nsView.delegate = context.coordinator // Reuses existing
}

Module References

Load these modules as needed:

  1. NSViewRepresentable: nsviewrepresentable.md

    • Full protocol implementation
    • Coordinator pattern
    • Layout integration with sizeThatFits
  2. Hosting Controllers: hosting-controllers.md

    • NSHostingView and NSHostingController
    • Window management
    • Incremental SwiftUI adoption
  3. State Management: state-management.md

    • @Observable bridging
    • Combine pipelines
    • Notification-based communication

Response Guidelines

  • Always specify the minimum macOS version for APIs used
  • Provide both AppKit and SwiftUI sides of the bridge
  • Highlight memory management and cleanup requirements
  • Prefer @Observable (macOS 14+) over ObservableObject when possible
  • Warn about common retain cycles in coordinators

Version History

  • 548ef5e Current 2026-08-28 22:53

Same Skill Collection

.agents/skills/apple-design/SKILL.md
.agents/skills/ip-as-logo/SKILL.md
.agents/skills/macos-development/app-planner/SKILL.md
.agents/skills/macos-development/architecture-patterns/SKILL.md
.agents/skills/macos-development/coding-best-practices/SKILL.md
.agents/skills/macos-development/macos-capabilities/SKILL.md
.agents/skills/macos-development/macos-tahoe-apis/SKILL.md
.agents/skills/macos-development/SKILL.md
.agents/skills/macos-development/swiftdata-architecture/SKILL.md
.agents/skills/macos-development/ui-review-tahoe/SKILL.md
.agents/skills/write-swift/SKILL.md
.claude/skills/apple-design/SKILL.md
.claude/skills/ip-as-logo/SKILL.md
.claude/skills/macos-development/app-planner/SKILL.md
.claude/skills/macos-development/appkit-swiftui-bridge/SKILL.md
.claude/skills/macos-development/architecture-patterns/SKILL.md
.claude/skills/macos-development/coding-best-practices/SKILL.md
.claude/skills/macos-development/macos-capabilities/SKILL.md
.claude/skills/macos-development/macos-tahoe-apis/SKILL.md
.claude/skills/macos-development/SKILL.md
.claude/skills/macos-development/swiftdata-architecture/SKILL.md
.claude/skills/macos-development/ui-review-tahoe/SKILL.md
.claude/skills/write-swift/SKILL.md
.agents/skills/design-anti-slop/SKILL.md
.agents/skills/landing-page-design/SKILL.md
.claude/skills/design-anti-slop/SKILL.md
.claude/skills/landing-page-design/SKILL.md

Metadata

Files
0
Version
548ef5e
Hash
ea8a8624
Indexed
2026-08-28 22:53

trang chủ - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-03 18:11
浙ICP备14020137号-1 $bản đồ khách truy cập$