Creating a custom template in Xcode - how can I make a required option based on a checkbox?
Asked Answered
C

2

6

I am trying to create a custom template in Xcode. In my TemplateInfo.plist, for key Options, I have the code pasted below. This template will be for a class that more often than not (but not always) will use delegation when events occur.

The issue I am having is with the value at the very bottom, RequiredOptions. I want the text box to be enabled only if the withProtocol checkbox is checked. However, I can't figure out what value and type of value to use. I have tried the following:

  • <true/> (as below) - The text box is always enabled.
  • <string>YES</string> - The text box is always disabled.
  • <integer>1</integer> - The text box is always enabled.

Does anyone have any ideas for what else I could try? Better yet, does anyone know of a decent reference for Xcode templates?

I have already read through Apple's plist manual pages and the article at this website.

<array>
    <dict>
        <key>Description</key>
        <string>The name of the class to create</string>
        <key>Identifier</key>
        <string>productName</string>
        <key>Name</key>
        <string>Class</string>
        <key>NotPersisted</key>
        <true/>
        <key>Required</key>
        <true/>
        <key>Type</key>
        <string>text</string>
    </dict>
    <dict>
        <key>Default</key>
        <string>false</string>
        <key>Identifier</key>
        <string>withXIB</string>
        <key>Name</key>
        <string>With XIB for user interface</string>
        <key>Type</key>
        <string>checkbox</string>
    </dict>
    <dict>
        <key>Description</key>
        <string>Choose whether or not a delegate skeleton is included.</string>
        <key>Default</key>
        <string>false</string>
        <key>Identifier</key>
        <string>withProtocol</string>
        <key>Name</key>
        <string>With delegate skeleton</string>
        <key>Type</key>
        <string>checkbox</string>
    </dict>
    <dict>
        <key>Description</key>
        <string>The name of the protocol used for delegation.</string>
        <key>Identifier</key>
        <string>protocolName</string>
        <key>Name</key>
        <string>Protocol</string>
        <key>NotPersisted</key>
        <true/>
        <key>Required</key>
        <true/>
        <key>Type</key>
        <string>text</string>
        <key>RequiredOptions</key>
        <dict>
            <key>withProtocol</key>
            <true/>
        </dict>
    </dict>
</array>
Cantaloupe answered 14/2, 2013 at 20:31 Comment(2)
when i use custom template it automatically use file written in templateinfo.plist , can i restrict them ? means if i choose checkbox only when files comes to my app.Wang
I'm sorry, I don't understand what you mean. Can you reword that?Cantaloupe
C
3

I fixed my own problem by replacing <true/> with <string>true</string>.

Cantaloupe answered 14/2, 2013 at 21:13 Comment(1)
This doesn't work in Xcode 11. Is there an alternative that works?Prevaricate
H
0

Correct way of adding a required option based on a checkbox

The key of the RequiredOptions dictionary must be the identifier of the other option, and the value of the dictionary must be the array of subset of values from the option that allow the current option to be enabled.

<key>RequiredOptions</key>
<dict>
    <key>withXIB</key>
    <array>
        <string>true</string>
    </array>
</dict>

In your case, true gets the job done.

Tested on Xcode 12 ✅

Headon answered 4/12, 2020 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.