使用自定义模板在Xcode 8中创建分组

39

我已经成功创建了一个适用于 Xcode 8 的模板,可以响应以下操作:

  1. 您已经有一个工作的项目
  2. 您想将自定义 Swift 文件添加到此项目中

步骤如下:

我在下面创建了:

/Users/*YOUR_USER_NAME*/Library/Developer/Xcode/Templates/File Templates

文件夹名称为Custom/Interactor.xctemplate

该文件夹内包含以下文件:

  • ___FILEBASENAME___Interactor
  • TemplateInfo.plist
  • TemplateIcon.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>

我的尝试:

我尝试将这个回答插入到我的 TemplateInfo.plist 中,但没有任何反应。我认为这可能与作用域有关 - 也许我没有把 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>

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

任何帮助将不胜感激。

谢谢。


4
我这里也遇到了完全相同的问题...看起来只有项目模板会考虑“定义”和“节点”,并且将文件添加到模板文件夹中会导致XCode导入文件夹,而不是创建组... - Pablo A.
4
@OhadM 如果您找到答案,请发布回复。 - George
3
我也想知道。 - Sajjon
4
是的,在2020年,我们仍在寻找答案...苹果应该真正记录这个。 - Gerald Eersteling
1
2021年:仍在寻找 =) - IrelDev
显示剩余10条评论
2个回答

1
如果有人需要的话,这里提供一个与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>

该组名为Some,其中包含一个文件。还有一个名为Other的子组。在Other中有一个名为TestClass的文件。

结果

Screenshot of the Xcode file navigator showing a top-level folder, Some, with a nested file and folder, Other.


1
如何在一个组中包含多个文件? - glassomoss
1
定义和节点应该放在哪里?在根目录下吗? - Pastre

1
你可以使用 Nodes 字典在项目目录中创建组和文件夹:
<key>Swift</key>
<dict>
    <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>ViewController.swift:implementation:methods:didReceiveMemoryWarning(override func didReceiveMemoryWarning(\))</string>
         <string>ViewController.swift:implementation:methods:didReceiveMemoryWarning:super</string>
         <string>Constant/CommonExtension.swift</string>
    </array>
</dict>

您还需要提供定义,就像这样:

<key>Definitions</key>
<dict>
    <key>Base.lproj/Main.storyboard</key>
    <dict>
        <key>Path</key>
        <string>Main.storyboard</string>
        <key>SortOrder</key>
        <integer>99</integer>
    </dict>
    <key>Group</key>
    <array>
        <string>Singletone</string>
    </array>
    <key>Constant/CommonExtension.swift</key>
    <dict>
        <key>Path</key>
        <string>Constant/CommonExtension.swift</string>
        <key>Group</key>
        <array>
            <string>Constant</string>
        </array>
    </dict>
</dict>

1
这个是在现有项目上工作还是只能在创建新项目时使用? - OhadM
这是使用我们的自定义模板与新项目一起工作。 - Pratik Lad
10
谢谢,但我知道这适用于新项目。我正在寻找现有项目的解决方案。 - OhadM
2
@PratikLad 你知道如何为分组设置排序顺序吗? - imike

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接