Swift: Create custom Playground templates

Its’ really easy.

    1. Hop over to
      /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templates/Source
    2. Copy Playground.xctemplate to your desktop. Rename it to MyPlayground.xctemplate or something similar but unique This name is shown in the new file dialog.
    3. If the folder does not yet exist, create /Users/ericasadun/Library/Developer/Xcode/Templates/File Templates/Playgrounds
    4. Move the new template into the File Templates/Playgrounds folder in your home library
    5. In the .template folder edit the two icon files (if desired) to give them a visual distinction
    6. If desired, edit the TemplateInfo.plist file and update the DefaultCompletionName to the standard file name you want to be offered to save to
    7. Select ___FILEBASENAME___.playground. Right-click it and choose Show Package Contents.
    8. Edit section-1.swift however you like. My version now imports Foundation and provides a couple of convenient functions:
// My Custom Playground
// Created by ___FULLUSERNAME___ on ___DATE___

import UIKit
import Foundation

func Peek(object : AnyObject?)
 println(object ? "\(object)" : "<nil>")

func Peek(label: String, object : AnyObject?)
 println(object ? "\(label) \(object)" : "\(label) <nil>")

And that’s about all there is to it. I find this a lot less trouble now than constantly pulling in snippets.

Comments are closed.