iOS Faster your development/ Automated MVVM View & ViewModel Template — XCode

Rajasekhar Tolapu
3 min readAug 14, 2020

Yes Templates, In this real-world everyone loves if we have templates to create some content easily like what Google Docs, Sheets, and PowerPoint provide templates. How cool is that if we have custom templates for Apple development for our project.

Benefits:

  1. Speed up your Development
  2. There will be sync between team members
  3. Many More

I will demonstrate in two steps — First, how to Create templates and secondly, an example of how to use it.

Let’s Start Template Creation :

Use the following path to see Apple’s / Existing templates.

(Click FINDER -> GO -> GO TO FOLDER and Paste the following path. It will redirect to the templates folder)

/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates

Now create a folder with a name in which all your custom templates will be kept there (I create Folder named MVVM basically I want to create Automated Basic ViewContoller and ViewModel code).

As you can see in the above picture that you can find all types of empty templates (Eg: Swift, Objective-C, XIB, StoryBoard, Swift UI etc ) which Apple already provided.

We will be creating a swift file with some custom methods inside it. Now copy Swift File.xctemplate and paste it on your create folder (@here it is MVVM)

I pasted two times and named as MVVMViewController.xctemplate and other as MVVMViewModel.xctemplate

Now try to open the Xcode project and add a new file. (FILE-NEW-FILE)

You can see both MVVM’s VC and VM will be reflected in the list. See below Image

Successful here, We have created Template and now let’s focus on template design

Photo by Andre Hunter on Unsplash

Let’s Start Template Example :

Place below code in ___FILEBASENAME___.swift under MVVMViewController.xctemplate

//___FILEHEADER___import UIKitclass ___FILEBASENAMEASIDENTIFIER___ViewController: UIViewController {
var viewModel = ___FILEBASENAMEASIDENTIFIER___ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
}
}extension ___FILEBASENAMEASIDENTIFIER___ViewController: ___FILEBASENAMEASIDENTIFIER___ViewModelDelegate {// Table View / Collection View / Protocols func reloadData() {
}
}

Place below code in ___FILEBASENAME___.swift under MVVMViewModel.xctemplate

//___FILEHEADER___import Foundationprotocol ___FILEBASENAMEASIDENTIFIER___ViewModelDelegate: class {            
func
reloadData()
}
class ___FILEBASENAMEASIDENTIFIER___ViewModel {
weak var delegate: ___FILEBASENAMEASIDENTIFIER___ViewModelDelegate?
init() {
// If required
}
/* Eg
func someMethod {
delegate?.reloadTable()
}
*/
}

Now we create templates for View and ViewModel for MVVM. Use the same name while creating ViewContoller and ViewModel and Later change file names which are created in files accordingly (watch Video below).

Photo by Razvan Chisu on Unsplash

Source Code & Example:

Conclusion

This makes faster development. Some ideas like you can create templates for Swift UI Classes, Architecture classes, etc…

--

--