xcode - 使用自定义模板 xcode 8 创建组

标签 xcode templates xcode8 xcode-template

我已经成功创建了一个 Xcode 8 模板来响应这些操作:

  • 你已经有一个工作项目
  • 你想将你的自定义 swift 文件添加到这个项目

  • 一步一步,如何实现:

    我在以下创建:
    /Users/*YOUR_USER_NAME*/Library/Developer/Xcode/Templates/File Templates
    

    文件夹名称 Custom/Interactor.xctemplate
    该文件夹内的文件:
  • ___FILEBASENAME___Interactor
  • TemplateInfo.plist
  • 模板图标.png
  • TemplateIcon@2x.png

  • 因此,当我右键单击并创建新文件时,我将选择自定义文件:

    create new file

    Creating custom file

    上面的示例有效并创建了我的自定义文件:

    successfully creating a custom file

    我想要实现的目标:

    我正在尝试创建一个包含新自定义文件的组。不需要实际的文件夹或复杂的任务,只需一个包含实际文件的组。

    所以最终结果将是:

    enter image description here

    由于没有解释如何创建自定义模板的文档,我使用了许多引用资料:ref1ref2ref3ref4

    这是 TemplateInfo.plist 文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>DefaultCompletionName</key>
        <string>MyCustomInteractor</string>
      <key>Description</key>
      <string>Custom interactor</string>
      <key>Kind</key>
      <string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
      <key>Options</key>
      <array>
            <dict>
                <key>Description</key>
                <string>Custom interactor</string>
                <key>Identifier</key>
                <string>fileName</string>
                <key>Name</key>
                <string>Custom name</string>
                <key>NotPersisted</key>
                <true/>
                <key>Required</key>
                <true/>
                <key>Type</key>
                <string>text</string>
            </dict>
        <dict>
                <key>Default</key>
                <string>___VARIABLE_fileName:identifier___</string>
                <key>Identifier</key>
                <string>productName</string>
                <key>Type</key>
                <string>static</string>
        </dict>
        <dict>
                <key>Default</key>
                <string>___VARIABLE_fileName:identifier___Interactor</string>
                <key>Description</key>
                <string>The interactor name</string>
                <key>Identifier</key>
                <string>interactorName</string>
                <key>Name</key>
                <string>Interactor Name:</string>
                <key>Required</key>
                <true/>
                <key>Type</key>
                <string>static</string>
        </dict>
      </array>
      <key>Platforms</key>
      <array>
            <string>com.apple.platform.iphoneos</string>
      </array>
        <key>SortOrder</key>
        <string>99</string>
    </dict>
    </plist>
    

    我尝试过的:

    我试图将 this 答案插入到我的 TemplateInfo.plist 中,但没有任何 react 。在我看来,这可能与范围问题有关——也许我没有在正确的位置插入 DefinitionsNodes 键。我一直在努力解决的 xml DefinitionsNodes 代码片段:
    <key>Definitions</key>
    <dict>
        <key>___FILEBASENAME___Interactor.swift</key>
        <dict>
            <key>Group</key>
            <string>Custom Interactor</string>
            <key>Path</key>
            <string>___FILEBASENAME___Interactor.swift</string>
        </dict>
    </dict>
    
    <key>Nodes</key>
    <array>
        <string>___FILEBASENAME___Interactor.swift</string>
    </array>
    

    那么我应该在哪里插入这些键或者我做错了什么?

    任何 帮助将不胜感激。

    谢谢。

    最佳答案

    万一这有助于某人,Xcode 12.4 - 项目模板。

                        <key>Nodes</key>
                        <array>
                            <string>ViewController.swift:comments</string>
                            <string>ViewController.swift:imports:importCocoa</string>
                            <string>ViewController.swift:implementation(___FILEBASENAME___: UIViewController)</string>
                            <string>ViewController.swift:implementation:methods:viewDidLoad(override func viewDidLoad(\))</string>
                            <string>ViewController.swift:implementation:methods:viewDidLoad:super</string>
                            <string>Info.plist:UIMainStoryboardFile</string>
                            <string>Info.plist:UIApplicationSceneManifest:UISceneStoryboardFile</string>
                            <string>Some/MainCoordinator.swift</string>
                            <string>Some/Other/TestClass.swift</string>
                        </array>
                        <key>Definitions</key>
                        <dict>
                            <key>Some/MainCoordinator.swift</key>
                            <dict>
                                <key>Group</key>
                                <array>
                                    <string>Some</string>
                                </array>
                                <key>Path</key>
                                <string>MainCoordinator.swift</string>
                            </dict>
                            <key>Some/Other/TestClass.swift</key>
                            <dict>
                                <key>Group</key>
                                <array>
                                    <string>Some</string>
                                    <string>Other</string>
                                </array>
                                <key>Path</key>
                                <string>TestClass.swift</string>
                            </dict>
                        </dict>
    
    该组是一些。
    里面有一个文件。
    它内部还有一个子组,称为“其他”。
    Other里面有一个文件叫做TestClass。
    enter image description here

    关于xcode - 使用自定义模板 xcode 8 创建组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484822/

    相关文章:

    ios - 为什么是 "Unknown class"?

    iPhone 5 屏幕调整大小不调整

    objective-c - 不支持 iPad 4.2.1 的 iPad App

    c++11 - CUDA 8.0 : Compile Error with Template Friend in Namespace

    swift - 是否有与 Swift 3's ' 数据类型等效的 writeToFile?

    ios - iOS 10/Xcode 8 设备上的 NSLog 似乎会被截断?为什么?

    objective-c - 无法使用 NSOpenPanel 打开图像

    c++ - 最烦人的 friend ?友化专门的自由函数模板会引发编译错误(重载方法时)

    c++ - 在编译时从可能类型的集合中获取整数?

    ios - 无法创建托管对象的子类