From fe5fff0d5d8fe7cd0e5f3e5e53c30b4bef533a93 Mon Sep 17 00:00:00 2001 From: Martin Conte Mac Donell Date: Fri, 10 Apr 2015 21:11:20 -0700 Subject: [PATCH 1/2] Simplifications and swift 1.2 syntax --- Source/Constraint.swift | 16 ++++++++-------- Source/ConstraintItem.swift | 14 ++------------ Source/View+Snap.swift | 2 +- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index b671494..124153f 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -313,7 +313,7 @@ public class Constraint { NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise() return [] } - return (self.installedLayoutConstraints?.allObjects as? Array)! + return self.installedLayoutConstraints?.allObjects as! Array } var newLayoutConstraints = Array() @@ -465,9 +465,9 @@ public class Constraint { private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint { if other.attributes != ConstraintAttributes.None { - var toLayoutAttributes = other.attributes.layoutAttributes + let toLayoutAttributes = other.attributes.layoutAttributes if toLayoutAttributes.count > 1 { - var fromLayoutAttributes = self.fromItem.attributes.layoutAttributes + let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes if toLayoutAttributes != fromLayoutAttributes { NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise() return self @@ -509,22 +509,22 @@ public class Constraint { } private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? { - var views = NSMutableSet() + var views = Set() var fromView = fromView var toView = toView do { if let view = toView { - if views.containsObject(view) { + if views.contains(view) { return view } - views.addObject(view) + views.insert(view) toView = view.superview } if let view = fromView { - if views.containsObject(view) { + if views.contains(view) { return view } - views.addObject(view) + views.insert(view) fromView = view.superview } } while (fromView != nil || toView != nil) diff --git a/Source/ConstraintItem.swift b/Source/ConstraintItem.swift index 1c3c69d..135af1e 100644 --- a/Source/ConstraintItem.swift +++ b/Source/ConstraintItem.swift @@ -41,22 +41,12 @@ public class ConstraintItem { internal var attributes: ConstraintAttributes internal var view: View? { - get { - if let view = self.object as? View { - return view - } - return nil - } + return self.object as? View } #if os(iOS) internal var layoutSupport: UILayoutSupport? { - get { - if let layoutSupport = self.object as? UILayoutSupport { - return layoutSupport - } - return nil - } + return self.object as? UILayoutSupport } #endif } diff --git a/Source/View+Snap.swift b/Source/View+Snap.swift index 09d1bab..31039ef 100644 --- a/Source/View+Snap.swift +++ b/Source/View+Snap.swift @@ -95,7 +95,7 @@ public extension View { if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? Array { return constraints } - return Array() + return [] } set { objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) From 9cec048c44458f91f316605f5d8c0633e0ff8933 Mon Sep 17 00:00:00 2001 From: Martin Conte Mac Donell Date: Fri, 10 Apr 2015 21:15:40 -0700 Subject: [PATCH 2/2] @noescape on methods --- Source/ConstraintMaker.swift | 8 ++++---- Source/View+Snap.swift | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index 49e03d2..d68bc9b 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -61,13 +61,13 @@ public class ConstraintMaker { return constraint } - internal class func prepareConstraints(view: View, block: (make: ConstraintMaker) -> Void) -> Array { + internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> Array { let maker = ConstraintMaker(view: view) block(make: maker) return maker.constraints } - internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { + internal class func makeConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else @@ -80,7 +80,7 @@ public class ConstraintMaker { } } - internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { + internal class func remakeConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else @@ -99,7 +99,7 @@ public class ConstraintMaker { } } - internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> Void) { + internal class func updateConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) { #if os(iOS) view.setTranslatesAutoresizingMaskIntoConstraints(false) #else diff --git a/Source/View+Snap.swift b/Source/View+Snap.swift index 09d1bab..3d7ae28 100644 --- a/Source/View+Snap.swift +++ b/Source/View+Snap.swift @@ -68,19 +68,19 @@ public extension View { public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } #endif - public func snp_prepareConstraints(block: (make: ConstraintMaker) -> Void) -> Array { + public func snp_prepareConstraints(@noescape block: (make: ConstraintMaker) -> Void) -> Array { return ConstraintMaker.prepareConstraints(self, block: block) } - public func snp_makeConstraints(block: (make: ConstraintMaker) -> Void) { + public func snp_makeConstraints(@noescape block: (make: ConstraintMaker) -> Void) { ConstraintMaker.makeConstraints(self, block: block) } - public func snp_updateConstraints(block: (make: ConstraintMaker) -> Void) { + public func snp_updateConstraints(@noescape block: (make: ConstraintMaker) -> Void) { ConstraintMaker.updateConstraints(self, block: block) } - public func snp_remakeConstraints(block: (make: ConstraintMaker) -> Void) { + public func snp_remakeConstraints(@noescape block: (make: ConstraintMaker) -> Void) { ConstraintMaker.remakeConstraints(self, block: block) }