From 542e034ca1b7d1cfdf4ccd951178a4031234a3eb Mon Sep 17 00:00:00 2001 From: Norman Basham Date: Mon, 13 Feb 2017 18:25:38 -0800 Subject: [PATCH] Upgraded to Swift 3. --- .../Contents.swift | 20 +++++++------- .../Original.xcplaygroundpage/Contents.swift | 14 +++++----- .../Step 1.xcplaygroundpage/Contents.swift | 13 +++++---- .../Step 2.xcplaygroundpage/Contents.swift | 19 +++++++------ .../Step 3.xcplaygroundpage/Contents.swift | 16 +++++------ .../Sources/Helpers.swift | 27 +++++++++---------- .../contents.xcplayground | 10 +------ 7 files changed, 52 insertions(+), 67 deletions(-) diff --git a/StackViews with Enums.playground/Pages/Final Code.xcplaygroundpage/Contents.swift b/StackViews with Enums.playground/Pages/Final Code.xcplaygroundpage/Contents.swift index 31e0b1c..a858d66 100644 --- a/StackViews with Enums.playground/Pages/Final Code.xcplaygroundpage/Contents.swift +++ b/StackViews with Enums.playground/Pages/Final Code.xcplaygroundpage/Contents.swift @@ -5,15 +5,15 @@ final class CallbackButton: UIView { let onTap: () -> () let button: UIButton - init(title: String, onTap: () -> ()) { + init(title: String, onTap: @escaping () -> ()) { self.onTap = onTap - self.button = UIButton(type: .System) + self.button = UIButton(type: .system) super.init(frame: .zero) addSubview(button) button.translatesAutoresizingMaskIntoConstraints = false button.constrainEdges(to: self) - button.setTitle(title, forState: .Normal) - button.addTarget(self, action: #selector(tapped), forControlEvents: .TouchUpInside) + button.setTitle(title, for: .normal) + button.addTarget(self, action: #selector(tapped), for: .touchUpInside) } required init?(coder aDecoder: NSCoder) { @@ -53,9 +53,9 @@ extension UIStackView { convenience init(elements: [ContentElement]) { self.init() translatesAutoresizingMaskIntoConstraints = false - axis = .Vertical + axis = .vertical spacing = 10 - + for element in elements { addArrangedSubview(element.view) } @@ -74,21 +74,21 @@ final class StackViewController: UIViewController { required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = .whiteColor() + view.backgroundColor = .white let stack = UIStackView(elements: elements) view.addSubview(stack) - stack.constrainEqual(.Width, to: view) + stack.constrainEqual(attribute: .width, to: view) stack.center(in: view) } } let elements: [ContentElement] = [ - .image([#Image(imageLiteral: "objc-logo-white.png")#]), + .image(#imageLiteral(resourceName: "objc-logo-white.png")), .label("To use the Swift Talk app please login as a subscriber"), .button("Login with GitHub", { print("Button tapped") diff --git a/StackViews with Enums.playground/Pages/Original.xcplaygroundpage/Contents.swift b/StackViews with Enums.playground/Pages/Original.xcplaygroundpage/Contents.swift index 40c17be..8b97aa9 100644 --- a/StackViews with Enums.playground/Pages/Original.xcplaygroundpage/Contents.swift +++ b/StackViews with Enums.playground/Pages/Original.xcplaygroundpage/Contents.swift @@ -4,18 +4,18 @@ import UIKit final class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = .whiteColor() + view.backgroundColor = .white let stack = UIStackView() stack.translatesAutoresizingMaskIntoConstraints = false - stack.axis = .Vertical + stack.axis = .vertical stack.spacing = 10 view.addSubview(stack) - stack.constrainEqual(.Width, to: view) + stack.constrainEqual(attribute: .width, to: view) stack.center(in: view) - let image = UIImageView(image: [#Image(imageLiteral: "objc-logo-white.png")#]) + let image = UIImageView(image: #imageLiteral(resourceName: "objc-logo-white.png")) stack.addArrangedSubview(image) let text1 = UILabel() @@ -23,8 +23,8 @@ final class ViewController: UIViewController { text1.text = "To use the Swift Talk app please login as a subscriber" stack.addArrangedSubview(text1) - let button = UIButton(type: .System) - button.setTitle("Login with GitHub", forState: .Normal) + let button = UIButton(type: .system) + button.setTitle("Login with GitHub", for: .normal) stack.addArrangedSubview(button) let text2 = UILabel() @@ -39,5 +39,3 @@ let vc = ViewController() vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) vc.view - - diff --git a/StackViews with Enums.playground/Pages/Step 1.xcplaygroundpage/Contents.swift b/StackViews with Enums.playground/Pages/Step 1.xcplaygroundpage/Contents.swift index 77b2c65..e9a0b03 100644 --- a/StackViews with Enums.playground/Pages/Step 1.xcplaygroundpage/Contents.swift +++ b/StackViews with Enums.playground/Pages/Step 1.xcplaygroundpage/Contents.swift @@ -16,8 +16,8 @@ extension ContentElement { label.text = text return label case .button(let title): - let button = UIButton(type: .System) - button.setTitle(title, forState: .Normal) + let button = UIButton(type: .system) + button.setTitle(title, for: .normal) return button case .image(let image): return UIImageView(image: image) @@ -29,18 +29,18 @@ extension ContentElement { final class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = .whiteColor() + view.backgroundColor = .white let stack = UIStackView() stack.translatesAutoresizingMaskIntoConstraints = false - stack.axis = .Vertical + stack.axis = .vertical stack.spacing = 10 view.addSubview(stack) - stack.constrainEqual(.Width, to: view) + stack.constrainEqual(attribute: .width, to: view) stack.center(in: view) - let image = ContentElement.image([#Image(imageLiteral: "objc-logo-white.png")#]).view + let image = ContentElement.image(#imageLiteral(resourceName: "objc-logo-white.png")).view stack.addArrangedSubview(image) let text1 = ContentElement.label("To use the Swift Talk app please login as a subscriber").view @@ -60,4 +60,3 @@ let vc = ViewController() vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) vc.view - diff --git a/StackViews with Enums.playground/Pages/Step 2.xcplaygroundpage/Contents.swift b/StackViews with Enums.playground/Pages/Step 2.xcplaygroundpage/Contents.swift index b8f155c..f7072ab 100644 --- a/StackViews with Enums.playground/Pages/Step 2.xcplaygroundpage/Contents.swift +++ b/StackViews with Enums.playground/Pages/Step 2.xcplaygroundpage/Contents.swift @@ -16,8 +16,8 @@ extension ContentElement { label.text = text return label case .button(let title): - let button = UIButton(type: .System) - button.setTitle(title, forState: .Normal) + let button = UIButton(type: .system) + button.setTitle(title, for: .normal) return button case .image(let image): return UIImageView(image: image) @@ -30,7 +30,7 @@ extension UIStackView { convenience init(elements: [ContentElement]) { self.init() translatesAutoresizingMaskIntoConstraints = false - axis = .Vertical + axis = .vertical spacing = 10 for element in elements { @@ -43,23 +43,23 @@ extension UIStackView { final class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = .whiteColor() - - let image = ContentElement.image([#Image(imageLiteral: "objc-logo-white.png")#]) + view.backgroundColor = .white + + let image = ContentElement.image(#imageLiteral(resourceName: "objc-logo-white.png")) let text1 = ContentElement.label("To use the Swift Talk app please login as a subscriber") let button = ContentElement.button("Login with GitHub") let text2 = ContentElement.label("If you're not registered yet, please visit http://objc.io for more information") - + let stack = UIStackView(elements: [image, text1, button, text2]) stack.translatesAutoresizingMaskIntoConstraints = false - stack.axis = .Vertical + stack.axis = .vertical stack.spacing = 10 view.addSubview(stack) - stack.constrainEqual(.Width, to: view) + stack.constrainEqual(attribute: .width, to: view) stack.center(in: view) } } @@ -70,4 +70,3 @@ let vc = ViewController() vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) vc.view - diff --git a/StackViews with Enums.playground/Pages/Step 3.xcplaygroundpage/Contents.swift b/StackViews with Enums.playground/Pages/Step 3.xcplaygroundpage/Contents.swift index 50dbf2d..779d1ea 100644 --- a/StackViews with Enums.playground/Pages/Step 3.xcplaygroundpage/Contents.swift +++ b/StackViews with Enums.playground/Pages/Step 3.xcplaygroundpage/Contents.swift @@ -16,8 +16,8 @@ extension ContentElement { label.text = text return label case .button(let title): - let button = UIButton(type: .System) - button.setTitle(title, forState: .Normal) + let button = UIButton(type: .system) + button.setTitle(title, for: .normal) return button case .image(let image): return UIImageView(image: image) @@ -30,7 +30,7 @@ extension UIStackView { convenience init(elements: [ContentElement]) { self.init() translatesAutoresizingMaskIntoConstraints = false - axis = .Vertical + axis = .vertical spacing = 10 for element in elements { @@ -43,10 +43,10 @@ extension UIStackView { final class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - view.backgroundColor = .whiteColor() + view.backgroundColor = .white let elements: [ContentElement] = [ - .image([#Image(imageLiteral: "objc-logo-white.png")#]), + .image(#imageLiteral(resourceName: "objc-logo-white.png")), .label("To use the Swift Talk app please login as a subscriber"), .button("Login with GitHub"), .label("If you're not registered yet, please visit http://objc.io for more information") @@ -54,11 +54,11 @@ final class ViewController: UIViewController { let stack = UIStackView(elements: elements) stack.translatesAutoresizingMaskIntoConstraints = false - stack.axis = .Vertical + stack.axis = .vertical stack.spacing = 10 view.addSubview(stack) - stack.constrainEqual(.Width, to: view) + stack.constrainEqual(attribute: .width, to: view) stack.center(in: view) } } @@ -68,5 +68,3 @@ let vc = ViewController() vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) vc.view - - diff --git a/StackViews with Enums.playground/Sources/Helpers.swift b/StackViews with Enums.playground/Sources/Helpers.swift index f295319..78d91ae 100644 --- a/StackViews with Enums.playground/Sources/Helpers.swift +++ b/StackViews with Enums.playground/Sources/Helpers.swift @@ -2,33 +2,32 @@ import UIKit extension UIView { public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, multiplier: CGFloat = 1, constant: CGFloat = 0) { - constrainEqual(attribute, to: to, attribute, multiplier: multiplier, constant: constant) + constrainEqual(attribute: attribute, to: to, attribute, multiplier: multiplier, constant: constant) } public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, _ toAttribute: NSLayoutAttribute, multiplier: CGFloat = 1, constant: CGFloat = 0) { - NSLayoutConstraint.activateConstraints([ - NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .Equal, toItem: to, attribute: toAttribute, multiplier: multiplier, constant: constant) - ]) + NSLayoutConstraint.activate([ + NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .equal, toItem: to, attribute: toAttribute, multiplier: multiplier, constant: constant) + ]) } public func constrainEdges(to view: UIView) { - constrainEqual(.Top, to: view, .Top) - constrainEqual(.Leading, to: view, .Leading) - constrainEqual(.Trailing, to: view, .Trailing) - constrainEqual(.Bottom, to: view, .Bottom) + constrainEqual(attribute: .top, to: view, .top) + constrainEqual(attribute: .leading, to: view, .leading) + constrainEqual(attribute: .trailing, to: view, .trailing) + constrainEqual(attribute: .bottom, to: view, .bottom) } - + /// If the `view` is nil, we take the superview. public func center(in view: UIView? = nil) { guard let container = view ?? self.superview else { fatalError() } - centerXAnchor.constrainEqual(container.centerXAnchor) - centerYAnchor.constrainEqual(container.centerYAnchor) + centerXAnchor.constrainEqual(anchor: container.centerXAnchor) + centerYAnchor.constrainEqual(anchor: container.centerYAnchor) } } extension NSLayoutAnchor { public func constrainEqual(anchor: NSLayoutAnchor, constant: CGFloat = 0) { - let constraint = constraintEqualToAnchor(anchor, constant: constant) - constraint.active = true + constraint(equalTo: anchor, constant: constant).isActive = true } -} \ No newline at end of file +} diff --git a/StackViews with Enums.playground/contents.xcplayground b/StackViews with Enums.playground/contents.xcplayground index 038f071..8d8c40c 100644 --- a/StackViews with Enums.playground/contents.xcplayground +++ b/StackViews with Enums.playground/contents.xcplayground @@ -1,10 +1,2 @@ - - - - - - - - - \ No newline at end of file + \ No newline at end of file