A set of snippets for Xcode.
Xcode 7.3.1 or later.
To install or update the snippets you need:
- Quit Xcode
- On the command line:
cd ~/Downloads git clone https://github.com/ismetanin/XcodeCodeSnippets mkdir -p $HOME/Library/Developer/Xcode/UserData/CodeSnippets cp XcodeCodeSnippets/CodeSnippets/* $HOME/Library/Developer/Xcode/UserData/CodeSnippets rm -rf XcodeCodeSnippets Or if you have a cloned repository:
- On the command line, cd into the directory with snippets and write
sh ./install.sh
There are MARKs for Swift and Objective-C. For Swift there is prefix before each // MARK: - and for Objective-C prefix is #pragma mark -
| Snippet | Shortcut |
|---|---|
Just empty mark (// MARK: - or #pragma mark - ) | MARK |
Nested types | Nested types |
Constants | Constants |
Subviews | Subviews |
NSLayoutConstraints | NSLayoutConstraints |
Properties | Properties |
Public properties | Public properties |
Readonly properties | Readonly properties |
IBOutlets | IBOutlets |
Initialization and deinitialization | Initialization and deinitialization |
UITableViewCell | UITableViewCell |
UIViewController | UIViewController |
Actions | Actions |
IBActions | IBActions |
Public methods | Public methods |
Internal methods | Internal methods |
Private methods | Private methods |
-
A template for creating TableViewAdapter, shortcut:
Table View AdapterCode
import UIKit protocol <#Your#>ViewAdapterOutput { } final class <#Your#>TableViewAdapter: NSObject { // MARK: - Constants private let output: <#Your#>ViewAdapterOutput // MARK: - Properties private var items: [<#ItemsType#>] private (set) var tableView: UITableView { didSet { tableView.register(UINib(nibName: <#CellName#>, bundle: nil), forCellReuseIdentifier: <#CellName#>) } } // MARK: - Initialization and deinitialization init(output: <#Your#>ViewAdapterOutput) { self.output = output } // MARK: - Internal helpers func set(tableView: UITableView) { self.tableView = tableView } func configure(with items: <#ItemsType#>) { self.items = items } } // MARK: - UITableViewDataSource extension <#Your#>TableViewAdapter: UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() return cell } } // MARK: - UITableViewDelegate extension <#Your#>TableViewAdapter: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } }
-
A code block for creating user property in UserDefaults extension, shortcut:
Defaults KeyCode
var <#defaultsKey#>: <#Type#> { get { return <#typeof#>(forKey: #function) } set { set(newValue, forKey: #function) } }
-
A code block for layouting child view anchors equal to the parent view anchors, shortcut:
Constraints - layout child as parentCode
<#childView#>.translatesAutoresizingMaskIntoConstraints = false if #available(iOS 11.0, *) { NSLayoutConstraint.activate([ <#childView#>.topAnchor.constraint(equalTo: <#parentView#>.safeAreaLayoutGuide.topAnchor, constant: 0), <#childView#>.bottomAnchor.constraint(equalTo: <#parentView#>.safeAreaLayoutGuide.bottomAnchor, constant: 0), <#childView#>.leadingAnchor.constraint(equalTo: <#parentView#>.safeAreaLayoutGuide.leadingAnchor, constant: 0), <#childView#>.trailingAnchor.constraint(equalTo: <#parentView#>.safeAreaLayoutGuide.trailingAnchor, constant: 0) ]) } else { NSLayoutConstraint.activate([ <#childView#>.topAnchor.constraint(equalTo: <#parentView#>.topAnchor, constant: 0), <#childView#>.bottomAnchor.constraint(equalTo: <#parentView#>.bottomAnchor, constant: 0), <#childView#>.leadingAnchor.constraint(equalTo: <#parentView#>.leadingAnchor, constant: 0), <#childView#>.trailingAnchor.constraint(equalTo: <#parentView#>.trailingAnchor, constant: 0) ]) }
-
A code block for user property creating in NSNotification.Name extension, shortcut:
NSNotification NameCode
static let <#notificationName#> = NSNotification.Name("<#projectName#>.notifications.<#notificationName#>")
-
A code block for creating template comments for unit test, shortcut:
testtemplateCode
// given // when // then
-
A code block for creating template constants enum, shortcut:
Constants enumCode
// MARK: - Nested types private enum Constants { }
-
A code block for creating keyboard notifications detector, shortcut:
Keyboard detectorCode
func addKeyboardObservers() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil) } @objc func keyboardWillShow(notification: NSNotification) { guard let frame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else { return } } @objc func keyboardWillHide() { }
Ivan Smetanin, smetanin23@yandex.ru
XcodeCodeSnippets is available under the MIT license. See the LICENSE file for more info.