【问题标题】:HowTo make Xcode recognize custom templates如何让 Xcode 识别自定义模板
【发布时间】:2016-11-06 10:57:32
【问题描述】:

我一直在使用Generamba 为 iOS 的 VIPER 架构下的每个模块创建模板文件。

它节省了大量时间,但仍然需要通过终端命令来运行 Generamba 并创建文件。有没有人知道如何将这些模板直接嵌入到 XCode 8 中?

【问题讨论】:

    标签: ios xcode viper-architecture


    【解决方案1】:

    您可以使用templates inside Xcode 来生成您的VIPER 类。看看这个repository,它已经为你实现了基本的VIPER文件。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我不熟悉Generamba,但要让Xcode 普遍识别您的模板:

      • 将您的 Template.swift 文件放在名为 MyTemplate.xctemplate 的文件夹中

      • 通过将Templateinfo.plist 添加到MyTemplate.xctemplate 来告诉Xcode 有关您模板的一些详细信息(请参阅下面的示例)。

      • MyTemplate.xctemplate复制到~/Library/Developer/Xcode/Templates/File\ Templates/Custom

      完成后,模板会显示在Xcodes new File 模板选择菜单的底部。

      例子:


      您可以使用被Xcode 替换的环境变量占位符。

      这是一个名为Worker.swift的简单示例模板:

      //
      //  ___FILENAME___
      //  ___PROJECTNAME___
      //
      //  Created by ___FULLUSERNAME___ on ___DATE___.
      //  Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
      //
      //  This file was generated. DO NOT MODIFY !
      //
      
      import Foundation
      
      class ___FILEBASENAMEASIDENTIFIER___Worker {
      
          //implementation goes here
      }
      

      还有它的例子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>MyWorker</string>
          <key>Description</key>
          <string>This generates a new worker.</string>
          <key>Kind</key>
          <string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
          <key>Options</key>
          <array>
              <dict>
                  <key>Default</key>
                  <string>___VARIABLE_sceneName:identifier___Worker</string>
                  <key>Description</key>
                  <string>The worker name</string>
                  <key>Identifier</key>
                  <string>workerName</string>
                  <key>Name</key>
                  <string>Worker 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>4</string>
          <key>Summary</key>
          <string>Summery</string>
      </dict>
      

      您还可以在MyTemplate.xctemplate 目录中放置多个文件,以使 Xcode 一次创建多个文件。对于您的VIPER 模板,您可以让Xcode 一次创建一个完整的VIPER 场景。

      您可以找到工作示例以及 makefile in this "Clean Swift" template repo(清洁 Swift 是 Swift 的另一种清洁架构方法)。

      【讨论】:

      • 如何自定义设置macOS部分下的模板?
      • @A.C:我认为你只能创建新的部分。
      • 是的,我已经为 macOS 创建了一个新部分。谢谢!
      【解决方案3】:

      看看ViperC,它同时支持Objective-CSwift。您还可以为您创建的模块创建测试类。测试类使用 QuickExpecta 用于 Objective-CQuickNimble 用于 Swift

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 2017-12-02
        • 2011-03-04
        • 2016-02-27
        • 1970-01-01
        • 2019-01-23
        相关资源
        最近更新 更多