add-to-xcode

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Add to Xcode

向Xcode添加文件

⚠️ MANDATORY: Run After Creating Source Files

⚠️ 强制要求:创建源文件后必须运行

Every time you create a
.swift
,
.m
,
.mm
,
.c
,
.cpp
, or
.h
file in an Xcode project, you MUST run:
bash
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb <filepath>
If you skip this step:
  • ❌ File will NOT appear in Xcode's navigator
  • ❌ File will NOT compile with the app
  • ❌ User will have to manually add it

每次在Xcode项目中创建
.swift
.m
.mm
.c
.cpp
.h
文件时,你必须运行:
bash
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb <filepath>
如果跳过此步骤:
  • ❌ 文件不会在Xcode的导航器中显示
  • ❌ 文件无法随应用一起编译
  • ❌ 用户必须手动添加该文件

Quick Reference

快速参考

bash
undefined
bash
undefined

ALWAYS do this after creating any source file:

创建任何源文件后务必执行此操作:

Use subshell to get latest version (handles multiple cached versions)

使用子shell获取最新版本(处理多个缓存版本)

ruby "$(ls -1d ~/.claude/plugins/cache/michaelboeding-skills/skills/*/skills/add-to-xcode/scripts/add_to_xcode.rb 2>/dev/null | sort -V | tail -1)" NewFile.swift

---
ruby "$(ls -1d ~/.claude/plugins/cache/michaelboeding-skills/skills/*/skills/add-to-xcode/scripts/add_to_xcode.rb 2>/dev/null | sort -V | tail -1)" NewFile.swift

---

Supported File Types

支持的文件类型

ExtensionAdded to Compile Sources
.swift
✅ Yes
.m
✅ Yes
.mm
✅ Yes
.c
✅ Yes
.cpp
✅ Yes
.h
❌ No (reference only)
扩展名是否加入编译源文件
.swift
✅ 是
.m
✅ 是
.mm
✅ 是
.c
✅ 是
.cpp
✅ 是
.h
❌ 否(仅作为引用)

Workflow

工作流程

Step 1: Create the file normally

步骤1:正常创建文件

bash
undefined
bash
undefined

Example: Create a new Swift file

示例:创建一个新的Swift文件

cat > Sources/Features/MyFeature.swift << 'EOF' import Foundation
class MyFeature { // Implementation } EOF
undefined
cat > Sources/Features/MyFeature.swift << 'EOF' import Foundation
class MyFeature { // 实现代码 } EOF
undefined

Step 2: Add to Xcode project

步骤2:添加到Xcode项目

bash
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb Sources/Features/MyFeature.swift
Output:
✓ Added Sources/Features/MyFeature.swift to MyApp.xcodeproj (target: MyApp)
bash
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb Sources/Features/MyFeature.swift
输出:
✓ Added Sources/Features/MyFeature.swift to MyApp.xcodeproj (target: MyApp)

What the Script Does

脚本功能说明

  1. Finds the
    .xcodeproj
    - Searches current directory and parents
  2. Creates group hierarchy - Matches the file's directory structure
  3. Adds file reference - Registers with the project
  4. Adds to build target - Source files (
    .swift
    ,
    .m
    ,
    .mm
    ,
    .c
    ,
    .cpp
    ) are added to the first target's compile sources
  1. 查找
    .xcodeproj
    文件
    - 搜索当前目录及父目录
  2. 创建组层级结构 - 与文件的目录结构保持一致
  3. 添加文件引用 - 在项目中注册文件
  4. 添加到构建目标 - 源文件(
    .swift
    .m
    .mm
    .c
    .cpp
    )会被添加到第一个目标的编译源列表中

Requirements

环境要求

Ruby with the
xcodeproj
gem:
bash
gem install xcodeproj
安装了
xcodeproj
gem的Ruby环境:
bash
gem install xcodeproj

Examples

示例

Adding a new Swift file

添加新的Swift文件

bash
undefined
bash
undefined

Create the file

创建文件

cat > MyApp/ViewModels/ProfileViewModel.swift << 'EOF' import SwiftUI
@Observable class ProfileViewModel { var name: String = "" var email: String = "" } EOF
cat > MyApp/ViewModels/ProfileViewModel.swift << 'EOF' import SwiftUI
@Observable class ProfileViewModel { var name: String = "" var email: String = "" } EOF

Add to Xcode

添加到Xcode

ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/ViewModels/ProfileViewModel.swift
undefined
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/ViewModels/ProfileViewModel.swift
undefined

Adding a header file

添加头文件

bash
undefined
bash
undefined

Create header

创建头文件

cat > MyApp/Bridge/MyApp-Bridging-Header.h << 'EOF' #import <SomeLibrary/SomeLibrary.h> EOF
cat > MyApp/Bridge/MyApp-Bridging-Header.h << 'EOF' #import <SomeLibrary/SomeLibrary.h> EOF

Add to Xcode (headers are added but not to compile sources)

添加到Xcode(头文件会被添加但不会加入编译源)

ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/Bridge/MyApp-Bridging-Header.h
undefined
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/Bridge/MyApp-Bridging-Header.h
undefined

Adding Objective-C files

添加Objective-C文件

bash
undefined
bash
undefined

Create implementation

创建实现文件

cat > MyApp/Legacy/LegacyManager.m << 'EOF' #import "LegacyManager.h"
@implementation LegacyManager // Implementation @end EOF
cat > MyApp/Legacy/LegacyManager.m << 'EOF' #import "LegacyManager.h"
@implementation LegacyManager // 实现代码 @end EOF

Add to Xcode

添加到Xcode

ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/Legacy/LegacyManager.m
undefined
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb MyApp/Legacy/LegacyManager.m
undefined

Agent Integration

Agent集成

When working in an Xcode project, agents should:
  1. Check for
    .xcodeproj
    before creating source files
  2. Create the file using standard file creation
  3. Run add_to_xcode.rb immediately after file creation
bash
undefined
在Xcode项目中工作时,Agent应遵循以下步骤:
  1. **创建源文件前检查
    .xcodeproj
    **是否存在
  2. 使用标准方式创建文件
  3. 创建文件后立即运行add_to_xcode.rb
bash
undefined

Pattern for agents:

Agent遵循的流程:

1. Create file

1. 创建文件

cat > NewFile.swift << 'EOF' // content EOF
cat > NewFile.swift << 'EOF' // 文件内容 EOF

2. Register with Xcode

2. 在Xcode中注册文件

ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb NewFile.swift
undefined
ruby ${CLAUDE_PLUGIN_ROOT}/skills/add-to-xcode/scripts/add_to_xcode.rb NewFile.swift
undefined

Troubleshooting

故障排除

"No .xcodeproj found"

提示“未找到.xcodeproj文件”

  • Make sure you're running from within the Xcode project directory or a subdirectory
  • 确保你在Xcode项目目录或其子目录中运行脚本

"gem not found: xcodeproj"

提示“未找到gem:xcodeproj”

  • Install with:
    gem install xcodeproj
  • On macOS with system Ruby, you may need:
    sudo gem install xcodeproj
  • 执行以下命令安装:
    gem install xcodeproj
  • 在使用系统Ruby的macOS上,可能需要:
    sudo gem install xcodeproj

File added but not compiling

文件已添加但无法编译

  • Check that the file extension is recognized (
    .swift
    ,
    .m
    ,
    .mm
    ,
    .c
    ,
    .cpp
    )
  • Verify the target exists and has a source build phase
  • Header files (
    .h
    ) are not added to compile sources (this is correct)
  • 检查文件扩展名是否被识别(
    .swift
    .m
    .mm
    .c
    .cpp
  • 验证目标是否存在且包含源构建阶段
  • 头文件(
    .h
    )不会被加入编译源(这是正常现象)

Related Skills

相关技能

SkillUse Case
ios-to-android
Convert iOS code to Android
android-to-ios
Convert Android code to iOS
技能使用场景
ios-to-android
将iOS代码转换为Android代码
android-to-ios
将Android代码转换为iOS代码