after downloading Xcode 9 beta I noticed change in file templates system.
For example I had a simple template that was creating 2 files (it probably shouldn't work that way at all). Base file names are
___FILEBASENAME___.swift
and
___FILEBASENAME___View.swift
and its creates TableCell.swift and TableCellView.swift, here are codes:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
import Foundation
import UIKit
class ___FILEBASENAME___: UITableViewCell {
let mainView = ___FILEBASENAME___View()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
contentView.addSubview(mainView)
mainView.setupView()
}
fileprivate func setupCell() {
backgroundColor = UIColor.clear
selectionStyle = .none
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
And View file:
//
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//
import Foundation
import UIKit
import SnapKit
fileprivate struct Constants {
}
class ___FILEBASENAME___View: UIView {
func setupView() {
setupSelf()
}
fileprivate func setupSelf() {
snp.makeConstraints { (make) in
make.leading.trailing.top.bottom.equalTo(0)
}
}
}
To create this files I was just picking template from
New Files...
menu, add an Name for ex. TableCell and hit enter. Now when I do this my output looked like this:
import Foundation
import UIKit
class TableCell: UITableViewCell {
let mainView = TableCellView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupCell()
contentView.addSubview(mainView)
mainView.setupView()
}
fileprivate func setupCell() {
backgroundColor = UIColor.clear
selectionStyle = .none
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and
import Foundation
import UIKit
import SnapKit
fileprivate struct Constants {
}
class NewCellView: UIView {
func setupView() {
setupSelf()
}
fileprivate func setupSelf() {
snp.makeConstraints { (make) in
make.leading.trailing.top.bottom.equalTo(0)
}
}
}
Now the problem is, in Xcode 9 they changed things in templates (something with templates in playgrounds, how newbie would use templates in playgrounds anyway?).
Back to the problem, now after creating files from template, TableCell.swift looks the same but TableCellView.swift goes wild due to this change.
___FILEBASENAME___View
becomes new
___FILEBASENAME___
so now when I create TableCellView it looks like this:
import Foundation
import UIKit
import SnapKit
fileprivate struct Constants {
}
class TableCellViewView: UIView {
func setupView() {
setupSelf()
}
fileprivate func setupSelf() {
snp.makeConstraints { (make) in
make.leading.trailing.top.bottom.equalTo(0)
}
}
}
Now the problem becomes more complicated when I create multiple files with dependencies from one another for example I have TableCellManager with delegate to TableCellViewControllerDelegate, when creating files now it looks like this
TableCellManagerViewControllerDelegate
instead just
TableViewControllerDelegate
___FILEBASENAME___ is replaced depending on scope, if newly created file is for example
___FILEBASENAME___View.swift
using keyword "Table" creates TableView.swift - in which ___FILEBASENAME___ is not "Table" but "TableView"
Can anyone tell me if there is a way to handle with it? Maybe there is something new like ___KEYWORD___? At creation of new file I want to enter a keyword that would work like ___FILEBASENAME___ in the older versions of Xcode. HELP!
In order to make this work with Xcode 15+ instead of
___VARIABLE_moduleName___
you have to use
___VARIABLE_productName___
also instead of custom header with filename and author you can use
//___FILEHEADER___