logo
logo
Sign in

XCode Templates tutorial – How To Create Custom Template Step By Step

XCode Templates tutorial – How To Create Custom Template Step By Step

With Xcode, every day we create files and groups. As a app developers improving work processes is always on their mind. We need tools and solutions to speed up the coding, testing, or organizing of our work. We usually create files for our classes, storyboards, or XIBs. We organize them into app development folders to have a logically organized project. Our preferred IDE provides several useful built-in web development templates that we can use to create various types of projects or files. I believe this article will be useful in improving your daily tasks as well.

What are the XCode templates?

XCode iOS app developers Templates is a tool for making code snippets to give you a better starting point to accomplish your task with fulfill your needs and speed up the flutter development work. In this article, we will be preparing a custom template for MVVM project architecture.

Often, we need to make from scratch the structure and files for a new software development module, and this process more similar every time. For instance, in the MVVM pattern, to build a Login module we need to create folders and at least four classes:

  1. Login Module Folder.
  2. Login View.
  3. Login View Model.
  4. Login View Controller.
  5. Login Model.

Adding each class in your necessary code is time-consuming. To avoid this we adding XCode templates to our web development project. Let’s see how to configure a template for use with a new MVVM module.

Installation of XCode Templates

Initially, we need to add a new folder which will consist of our custom templates for install in XCode iOS app developers templates.

XCode iOS templates location

Each Xcode custom template files are located in  ~/Library/Developer/Xcode/Templates/  and grouped into segments by folder name. You can add it manually or using the terminal by running the following command:

mkdir ~/Library/Developer/Xcode/Templates/Custom Templates

File template structure

The main folder for XCode templates

Every XCode file template is a distinct folder with the extension .xctemplate. If you need a template named “View, Model & ViewModel”, you should create a folder named “View, Model & ViewModel.xctemplate in “~/Library/Developer/Xcode/Templates/File Templates/Custom Templates”.

Internal folders and files in the XCode templates

          Kind

Xcode.IDEKit.TextSubstitutionFileTemplateKind

          Platforms

com.apple.platform.iphoneos

          Options

                              Identifier

                              productName

                              Required

                             

                              Name

Module Name

                              Description

                              The name of the Model, View and ViewModel to create

                              Type

                              text

                              Default

                              Module1

The TemplateInfo.plist file consists of the description of the basic template, implementation gives us the ability to type app development Module name while creating XCode app development templates with the wizard. You can’t need to put pretty much anything into the actual template file. Text macros like ___FILEBASENAME___ to refer to the filename. The name is fetched from the productName option from our TemplateInfo.info file which is set in the new file wizard.

Implementation and structure

 

1. Implementation of viewcontroller

import UIKit

class ___FILEBASENAMEASIDENTIFIER___: UIViewController {

 let viewModel: ___VARIABLE_productName___ViewModel

    let mainView: ___VARIABLE_productName___View

    init() {

        viewModel = ___VARIABLE_productName___ViewModel(withModel: ___VARIABLE_productName___())

        mainView = ___VARIABLE_productName___View()

        super.init(nibName: nil, bundle: nil)

        mainView.configure(withViewModel: viewModel) }

    required init?(coder _: NSCoder) {

        fatalError(“init(coder:) has not been implemented”) }

  override func viewDidLoad() {

        super.viewDidLoad()

        setupView() }

       private func setupView() {

        view.addSubview(mainView)

        mainView.snp.makeConstraints {

            make in

            make.top.leading.trailing.bottom.equalToSuperview() } }}

 As you can see we implemented the following:

  • Declaring app developers variables of viewModel and mainView. It will consist of our productName from TemplateInfo.plist set in file wizard.
  • Variables initializing.
  • Default initializer.
  • Required initializer.
  • viewDidLoad implementation.
  • setupView flutter development function adding mainView and set SnapKit constraints.

2. Implementation of model

import Foundation

class ___FILEBASENAMEASIDENTIFIER___ { }

This class will be generated automatically by XCode so the above is just an instance.

3. Implementation of view

import UIKit

class ___FILEBASENAMEASIDENTIFIER___: UIView {

     init() { super.init(frame: CGRect.zero) }

   required init?(coder _: NSCoder) {

        fatalError(“init(coder:) has not been implemented”) }

    func configure(with viewModel: ___VARIABLE_productName:identifier___ViewModel) {

        // configure the view with a ___VARIABLE_productName:identifier___ViewModel }}

Class view consist of default initializer, and a required initializer, both required to initialize the View from the app development code. We also need to configure function to bind viewModel and the view. Note that ViewModel name is the same as the ViewModel name in the ViewController.

4. Implementing viewmodel

import Foundation

class ___FILEBASENAMEASIDENTIFIER___ {

    private let model: ___VARIABLE_productName:identifier___

    init(withModel model: ___VARIABLE_productName:identifier___) {

        self.model = model    }}

ViewModel is initialized with our Model created in p.2.

How to use files in XCode templates?

Step 1: To get started you require to click File -> New -> File and Find your template in the list.

 

Step 2: Next enter the model name.

 

Step 3: Then add it to your project.

collect
0
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more