Skip to content

Instantly share code, notes, and snippets.

@fbartho
Created August 4, 2017 17:33
Show Gist options
  • Save fbartho/7125c77d889dbcb6696b468810a4b428 to your computer and use it in GitHub Desktop.
Save fbartho/7125c77d889dbcb6696b468810a4b428 to your computer and use it in GitHub Desktop.
Example of a Swift file that breaks swiftformat --self remove
swiftformat --self remove tmp.swift
import UIKit
/// Something about this `override var text` breaks swiftformat if it's present when trying to format the file.
/// Comment it out, and the setupView method below gets all the `self` references stripped.
class DangerField: UITextField {
override var text: String? {
get {
return nil
}
set(newValue) {}
}
}
class SomeView: UIView, UITextFieldDelegate {
fileprivate func setupView() {
self.addSubview(self.numberField)
self.addSubview(self.dateField)
self.addSubview(self.cvcField)
self.addSubview(self.postalCodeField)
self.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: self.numberField, attribute: .top, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: self.numberField, attribute: .leading, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: self.numberField, attribute: .trailing, multiplier: 1, constant: 0))
self.lineSpacingConstraint = NSLayoutConstraint(item: self.dateField, attribute: .top, relatedBy: .equal, toItem: self.numberField, attribute: .bottom, multiplier: 1, constant: self.lineSpacing)
self.lineSpacingConstraint!.priority = UILayoutPriorityDefaultHigh
self.addConstraint(self.lineSpacingConstraint!)
self.addConstraint(NSLayoutConstraint(item: self.dateField, attribute: .top, relatedBy: .equal, toItem: self.cvcField, attribute: .top, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.dateField, attribute: .top, relatedBy: .equal, toItem: self.postalCodeField, attribute: .top, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: self.dateField, attribute: .leading, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.dateField, attribute: .trailing, relatedBy: .equal, toItem: self.cvcField, attribute: .leading, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.cvcField, attribute: .trailing, relatedBy: .equal, toItem: self.postalCodeField, attribute: .leading, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.postalCodeField, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.dateField, attribute: .width, relatedBy: .equal, toItem: self.cvcField, attribute: .width, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.dateField, attribute: .width, relatedBy: .equal, toItem: self.postalCodeField, attribute: .width, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: self.dateField, attribute: .bottom, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: self.cvcField, attribute: .bottom, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: self.postalCodeField, attribute: .bottom, multiplier: 1, constant: 0))
self.lineHeightConstraint = NSLayoutConstraint(item: self.numberField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.lineHeight)
self.addConstraint(self.lineHeightConstraint!)
self.addConstraint(NSLayoutConstraint(item: self.numberField, attribute: .height, relatedBy: .equal, toItem: self.dateField, attribute: .height, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.numberField, attribute: .height, relatedBy: .equal, toItem: self.cvcField, attribute: .height, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: self.numberField, attribute: .height, relatedBy: .equal, toItem: self.postalCodeField, attribute: .height, multiplier: 1, constant: 0))
self.updateCardImage()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment