Skip to content

Commit 6d0b668

Browse files
committed
Update for Swift 3.0 syntax
1 parent b503dd8 commit 6d0b668

35 files changed

+217
-217
lines changed

C4/Core/EventSource.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ public protocol EventSource {
3131
///
3232
/// - parameter notificationName: The notification name to listen for
3333
/// - parameter executionBlock: A block of code to run when the receiver "hears" the specified notification name
34-
func on(event notificationName: String, run: (Void) -> Void) -> AnyObject
34+
func on(event notificationName: String, run: @escaping (Void) -> Void) -> AnyObject
3535

3636
/// Register an action to run when an event is triggered by the specified sender. Returns an observer handle you can use to cancel the action.
3737
///
3838
/// - parameter notificationName: The notification name to listen for
3939
/// - parameter sender: The object from which to listen for the notification
4040
/// - parameter executionBlock: A block of code to run when the receiver "hears" the specified notification name
41-
func on(event notificationName: String, from sender: AnyObject?, run executionBlock: (Void) -> Void) -> AnyObject
41+
func on(event notificationName: String, from sender: AnyObject?, run executionBlock: @escaping () -> Void) -> AnyObject
4242

4343
/// Cancel a previously registered action from an observer handle.
4444
func cancel(_ observer: AnyObject)
4545
}
4646

4747
/// This extension allows any NSObject to post and listen for events in the same way as C4 objects.
4848
extension NSObject : EventSource {
49+
4950
/// Posts a new notification originating from the receiver.
5051
///
5152
/// ````
@@ -56,7 +57,7 @@ extension NSObject : EventSource {
5657
///
5758
/// - parameter event: The notification name for the event
5859
public func post(_ event: String) {
59-
NotificationCenter.default().post(name: Notification.Name(rawValue: event), object: self)
60+
NotificationCenter.default.post(name: Notification.Name(rawValue: event), object: self)
6061
}
6162

6263
/// An action to run on receipt of a given event.
@@ -70,8 +71,7 @@ extension NSObject : EventSource {
7071
/// - parameter event: The notification name to listen for
7172
/// - parameter run: A block of code to run when the receiver "hears" the specified event name
7273
/// - returns: A token to use for cancelling the action.
73-
@discardableResult
74-
public func on(event notificationName: String, run executionBlock: (Void) -> Void) -> AnyObject {
74+
public func on(event notificationName: String, run executionBlock: @escaping (Void) -> Void) -> AnyObject {
7575
return on(event: notificationName, from: nil, run: executionBlock)
7676
}
7777

@@ -87,9 +87,9 @@ extension NSObject : EventSource {
8787
/// - parameter sender: The object from which to listen for the notification
8888
/// - parameter executionBlock: A block of code to run when the receiver "hears" the specified notification name
8989
/// - returns: A token to use for cancelling the action.
90-
public func on(event notificationName: String, from sender: AnyObject?, run executionBlock: (Void) -> Void) -> AnyObject {
91-
let nc = NotificationCenter.default()
92-
return nc.addObserver(forName: NSNotification.Name(rawValue: notificationName), object: sender, queue: OperationQueue.current(), using: { notification in
90+
public func on(event notificationName: String, from sender: AnyObject?, run executionBlock: @escaping (Void) -> Void) -> AnyObject {
91+
let nc = NotificationCenter.default
92+
return nc.addObserver(forName: NSNotification.Name(rawValue: notificationName), object: sender, queue: OperationQueue.current, using: { notification in
9393
executionBlock()
9494
})
9595
}
@@ -102,7 +102,7 @@ extension NSObject : EventSource {
102102
///
103103
/// - parameter token: A token returned from a call to `on(event:run:)` method
104104
public func cancel(_ token: AnyObject) {
105-
let nc = NotificationCenter.default()
105+
let nc = NotificationCenter.default
106106
nc.removeObserver(token)
107107
}
108108
}

C4/Core/Foundation.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public func C4Log<T>(_ value: T) {
4444
/// - returns: The smallest CGRect that contains all of the points in the specified array
4545
public func CGRectMakeFromPoints(_ points: [CGPoint]) -> CGRect {
4646
let path = CGMutablePath()
47-
path.moveTo(nil, x: points[0].x, y: points[0].y)
47+
path.move(to: points[0])
4848
for i in 1..<points.count {
49-
path.addLineTo(nil, x: points[i].x, y: points[i].y)
49+
path.addLine(to: points[i])
5050
}
5151
return path.boundingBox
5252
}
@@ -61,6 +61,6 @@ public func CGRectMakeFromPoints(_ points: [CGPoint]) -> CGRect {
6161
///
6262
/// - parameter delay: The amount of time in seconds to wait before executing the block of code.
6363
/// - parameter action: A block of code to perform after the delay.
64-
public func wait(_ seconds: Double, action: ()->()) {
65-
DispatchQueue.main.after(walltime: DispatchWallTime.now() + seconds, execute: action)
64+
public func wait(_ seconds: Double, action: @escaping ()->()) {
65+
DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + seconds, execute: action)
6666
}

C4/Core/Path.swift

+18-17
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public enum FillRule {
2525
/// Specifies the non-zero winding rule. Count each left-to-right path as +1 and each right-to-left path as -1. If the
2626
/// sum of all crossings is 0, the point is outside the path. If the sum is nonzero, the point is inside the path and
2727
/// the region containing it is filled.
28-
case nonZero
28+
case NonZero
2929

3030
/// Specifies the even-odd winding rule. Count the total number of path crossings. If the number of crossings is even,
3131
/// the point is outside the path. If the number of crossings is odd, the point is inside the path and the region
3232
/// containing it should be filled.
33-
case evenOdd
33+
case EvenOdd
3434
}
3535

3636

@@ -42,7 +42,7 @@ public class Path: Equatable {
4242
/// Initializes an empty Path.
4343
public init() {
4444
internalPath = CGMutablePath()
45-
internalPath.moveTo(nil, x: 0, y: 0)
45+
internalPath.move(to: CGPoint())
4646
}
4747

4848
/// Initializes a new Path from an existing CGPathRef.
@@ -73,8 +73,9 @@ public class Path: Equatable {
7373
/// - parameter point: The point to test.
7474
/// - parameter fillRule: The fill rule to use when testing for containment.
7575
/// - returns: `true` if `point` is inside the path, `false` otherwise.
76-
public func containsPoint(_ point: Point, fillRule: FillRule = .nonZero) -> Bool {
77-
return internalPath.containsPoint(nil, point: CGPoint(point), eoFill: fillRule == .evenOdd)
76+
public func containsPoint(_ point: Point, fillRule: FillRule = .NonZero) -> Bool {
77+
let rule = fillRule == .EvenOdd ? CGPathFillRule.evenOdd : CGPathFillRule.winding
78+
return internalPath.contains(CGPoint(point), using: rule, transform: CGAffineTransform.identity)
7879
}
7980

8081
/// Create a copy of the path
@@ -96,7 +97,7 @@ public class Path: Equatable {
9697
/// - parameter right: the second path to compare
9798
/// - returns: a boolean, `true` if the patrhs are equal, otherwise `false`
9899
public func == (left: Path, right: Path) -> Bool {
99-
return left.internalPath.equalTo(right.internalPath)
100+
return left.internalPath == right.internalPath
100101
}
101102

102103
extension Path {
@@ -114,21 +115,21 @@ extension Path {
114115
/// Move the current point of the current subpath.
115116
/// - parameter point: A Point
116117
public func moveToPoint(_ point: Point) {
117-
internalPath.moveTo(nil, x: CGFloat(point.x), y: CGFloat(point.y))
118+
internalPath.move(to: CGPoint(point))
118119
}
119120

120121
/// Append a straight-line segment fron the current point to `point` and move the current point to `point`.
121122
/// - parameter point: A Point
122123
public func addLineToPoint(_ point: Point) {
123-
internalPath.addLineTo(nil, x: CGFloat(point.x), y: CGFloat(point.y))
124+
internalPath.addLine(to: CGPoint(point))
124125
}
125126

126127
/// Append a quadratic curve from the current point to `point` with control point `control` and move the current
127128
/// point to `point`.
128129
/// - parameter control: A Point used to shape the curve
129130
/// - parameter point: A Point
130131
public func addQuadCurveToPoint(_ point: Point, control: Point) {
131-
internalPath.addQuadCurve(nil, cpx: CGFloat(control.x), cpy: CGFloat(control.y), endingAtX: CGFloat(point.x), y: CGFloat(point.y))
132+
internalPath.addQuadCurve(to: CGPoint(point), control: CGPoint(control))
132133
}
133134

134135
/// Append a cubic Bézier curve from the current point to `point` with control points `control1` and `control2`
@@ -137,7 +138,7 @@ extension Path {
137138
/// - parameter control2: A Point used to shape the curve
138139
/// - parameter point: A Point
139140
public func addCurveToPoint(_ point: Point, control1: Point, control2: Point) {
140-
internalPath.addCurve(nil, cp1x: CGFloat(control1.x), cp1y: CGFloat(control1.y), cp2x: CGFloat(control2.x), cp2y: CGFloat(control2.y), endingAtX: CGFloat(point.x), y: CGFloat(point.y))
141+
internalPath.addCurve(to: CGPoint(point), control1: CGPoint(control1), control2: CGPoint(control2))
141142
}
142143

143144
/// Append a line from the current point to the starting point of the current subpath and end the subpath.
@@ -148,7 +149,7 @@ extension Path {
148149
/// Add a rectangle to the path.
149150
/// - parameter rect: a Rect to add to the path
150151
public func addRect(_ rect: Rect) {
151-
internalPath.addRect(nil, rect: CGRect(rect))
152+
internalPath.addRect(CGRect(rect))
152153
}
153154

154155
/// Add a rounded rectangle to the path. The rounded rectangle coincides with the edges of `rect`. Each corner consists
@@ -159,7 +160,7 @@ extension Path {
159160
/// - parameter cornerWidth: the width of the shape's rounded corners
160161
/// - parameter cornerHeight: the width of the shape's rounded corners
161162
public func addRoundedRect(_ rect: Rect, cornerWidth: Double, cornerHeight: Double) {
162-
internalPath.addRoundedRect(nil, rect: CGRect(rect), cornerWidth: CGFloat(cornerWidth), cornerHeight: CGFloat(cornerHeight))
163+
internalPath.addRoundedRect(in: CGRect(rect), cornerWidth: CGFloat(cornerWidth), cornerHeight: CGFloat(cornerHeight))
163164
}
164165

165166
/// Add an ellipse (an oval) inside `rect`. The ellipse is approximated by a sequence of Bézier curves. The center of
@@ -169,7 +170,7 @@ extension Path {
169170
/// a "move to" and ends with a "close subpath" --- oriented in the clockwise direction.
170171
/// - parameter rect: a Rect into which an ellipse will be created and added to the path
171172
public func addEllipse(_ rect: Rect) {
172-
internalPath.addEllipseIn(nil, rect: CGRect(rect))
173+
internalPath.addEllipse(in: CGRect(rect))
173174
}
174175

175176
/// Add an arc of a circle, possibly preceded by a straight line segment. The arc is approximated by a sequence of
@@ -182,7 +183,7 @@ extension Path {
182183
/// - parameter delta: The angle between `startAngle` and the second endpoint of the arc, in radians. If `delta' is positive,
183184
/// then the arc is drawn counter-clockwise; if negative, clockwise.
184185
public func addRelativeArc(_ center: Point, radius: Double, startAngle: Double, delta: Double) {
185-
internalPath.addRelativeArc(matrix: nil, x: CGFloat(center.x), y: CGFloat(center.y), radius: CGFloat(radius), startAngle: CGFloat(startAngle), delta: CGFloat(delta))
186+
internalPath.addRelativeArc(center: CGPoint(center), radius: CGFloat(radius), startAngle: CGFloat(startAngle), delta: CGFloat(delta))
186187
}
187188

188189
/// Add an arc of a circle, possibly preceded by a straight line segment. The arc is approximated by a sequence of
@@ -203,7 +204,7 @@ extension Path {
203204
/// - parameter endAngle: The angle to the second endpoint of the arc.
204205
/// - parameter clockwise: If true the arc is drawn clockwise.
205206
public func addArc(_ center: Point, radius: Double, startAngle: Double, endAngle: Double, clockwise: Bool) {
206-
internalPath.addArc(nil, x: CGFloat(center.x), y: CGFloat(center.y), radius: CGFloat(radius), startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: clockwise)
207+
internalPath.addArc(center: CGPoint(center), radius: CGFloat(radius), startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: clockwise)
207208
}
208209

209210
/// Add an arc of a circle, possibly preceded by a straight line segment. The arc is approximated by a sequence of
@@ -213,14 +214,14 @@ extension Path {
213214
/// - parameter point2: the end point of the arc
214215
/// - parameter radius: the radius of the arc
215216
public func addArcToPoint(_ point1: Point, point2: Point, radius: Double) {
216-
internalPath.addArc(nil, x1: CGFloat(point1.x), y1: CGFloat(point1.y), x2: CGFloat(point2.x), y2: CGFloat(point2.y), radius: CGFloat(radius))
217+
internalPath.addArc(tangent1End: CGPoint(point1), tangent2End: CGPoint(point2), radius: CGFloat(radius))
217218
}
218219

219220
/// Append a path.
220221
///
221222
/// - parameter path: A new Path that is added to the end of the receiver.
222223
public func addPath(_ path: Path) {
223-
internalPath.addPath(nil, path: path.internalPath)
224+
internalPath.addPath(path.internalPath)
224225
}
225226

226227
/// Transform a path.

C4/Core/Transform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import QuartzCore
2525
///
2626
/// Transform can translate, rotate, scale.
2727
public struct Transform: Equatable {
28-
private var matrix = [Double](repeating: 0, count: 16)
28+
var matrix = [Double](repeating: 0, count: 16)
2929

3030
public subscript(row: Int, col: Int) -> Double {
3131
get {

C4/Core/Vector.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public struct Vector: Equatable, CustomStringConvertible {
142142
/// - parameter vec: The vector used to calcuate the angle to the receiver
143143
/// - returns: The angle, measured in radians, between the receiver and `vec`
144144
public func angleTo(_ vec: Vector) -> Double {
145-
return acos(self vec / (self.magnitude * vec.magnitude))
145+
return acos(self (vec / (self.magnitude * vec.magnitude)))
146146
}
147147

148148
/// The angle between two vectors, based on a provided point
@@ -162,7 +162,7 @@ public struct Vector: Equatable, CustomStringConvertible {
162162
vecA -= basedOn
163163
vecB -= basedOn
164164

165-
return acos(vecA vecB / (vecA.magnitude * vecB.magnitude))
165+
return acos(vecA (vecB / (vecA.magnitude * vecB.magnitude)))
166166
}
167167

168168
/// Return the dot product. **You should use the ⋅ operator instead.**
@@ -320,7 +320,7 @@ public func - (lhs: Vector, rhs: Vector) -> Vector {
320320
return Vector(x: lhs.x - rhs.x, y: lhs.y - rhs.y, z: lhs.z - rhs.z)
321321
}
322322

323-
infix operator { associativity left precedence 150 }
323+
infix operator
324324

325325
/// Returns a new vector that is the dot product of the both input vectors. **Use this instead of v.dot(v)**
326326
///

C4/UI/Animation.swift

+16-16
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public class Animation {
3131
/// ````
3232
public enum Curve {
3333
/// A linear animation curve causes an animation to occur evenly over its duration.
34-
case linear
34+
case Linear
3535
/// An ease-out curve causes the animation to begin quickly, and then slow down as it completes.
36-
case easeOut
36+
case EaseOut
3737
/// An ease-in curve causes the animation to begin slowly, and then speed up as it progresses.
38-
case easeIn
38+
case EaseIn
3939
/// An ease-in ease-out curve causes the animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. This is the default curve for most animations.
40-
case easeInOut
40+
case EaseInOut
4141
}
4242

4343
/// Determines if the animation plays in the reverse upon completion.
@@ -64,7 +64,7 @@ public class Animation {
6464
public var duration: TimeInterval = 1
6565

6666
/// The animation curve that the receiver will apply to the changes it is supposed to animate.
67-
public var curve: Curve = .easeInOut
67+
public var curve: Curve = .EaseInOut
6868

6969
private var completionObservers: [AnyObject] = []
7070
private var cancelObservers: [AnyObject] = []
@@ -81,7 +81,7 @@ public class Animation {
8181
}
8282

8383
deinit {
84-
let nc = NotificationCenter.default()
84+
let nc = NotificationCenter.default
8585
for observer in completionObservers {
8686
nc.removeObserver(observer)
8787
}
@@ -100,9 +100,9 @@ public class Animation {
100100
/// - parameter action: a block of code to be executed at the end of an animation.
101101
///
102102
/// - returns: the observer object.
103-
public func addCompletionObserver(_ action: () -> Void) -> AnyObject {
104-
let nc = NotificationCenter.default()
105-
let observer = nc.addObserver(forName: NSNotification.Name(rawValue: AnimationCompletedEvent), object: self, queue: OperationQueue.current(), using: { notification in
103+
public func addCompletionObserver(_ action: @escaping () -> Void) -> AnyObject {
104+
let nc = NotificationCenter.default
105+
let observer = nc.addObserver(forName: NSNotification.Name(rawValue: AnimationCompletedEvent), object: self, queue: OperationQueue.current, using: { notification in
106106
action()
107107
})
108108
completionObservers.append(observer)
@@ -113,7 +113,7 @@ public class Animation {
113113
///
114114
/// - parameter observer: the observer object to remove.
115115
public func removeCompletionObserver(_ observer: AnyObject) {
116-
let nc = NotificationCenter.default()
116+
let nc = NotificationCenter.default
117117
nc.removeObserver(observer, name: NSNotification.Name(rawValue: AnimationCompletedEvent), object: self)
118118
}
119119

@@ -122,7 +122,7 @@ public class Animation {
122122
/// This method is triggered when an animation completes. This can be used in place of `addCompletionObserver` for objects outside the scope of the context in which the animation is created.
123123
public func postCompletedEvent() {
124124
DispatchQueue.main.async {
125-
NotificationCenter.default().post(name: Notification.Name(rawValue: AnimationCompletedEvent), object: self)
125+
NotificationCenter.default.post(name: Notification.Name(rawValue: AnimationCompletedEvent), object: self)
126126
}
127127
}
128128

@@ -133,9 +133,9 @@ public class Animation {
133133
/// - parameter action: a block of code to be executed when an animation is canceled.
134134
///
135135
/// - returns: the observer object.
136-
public func addCancelObserver(_ action: () -> Void) -> AnyObject {
137-
let nc = NotificationCenter.default()
138-
let observer = nc.addObserver(forName: NSNotification.Name(rawValue: AnimationCancelledEvent), object: self, queue: OperationQueue.current(), using: { notification in
136+
public func addCancelObserver(_ action: @escaping () -> Void) -> AnyObject {
137+
let nc = NotificationCenter.default
138+
let observer = nc.addObserver(forName: NSNotification.Name(rawValue: AnimationCancelledEvent), object: self, queue: OperationQueue.current, using: { notification in
139139
action()
140140
})
141141
cancelObservers.append(observer)
@@ -146,7 +146,7 @@ public class Animation {
146146
///
147147
/// - parameter observer: the cancel observer object to remove.
148148
public func removeCancelObserver(_ observer: AnyObject) {
149-
let nc = NotificationCenter.default()
149+
let nc = NotificationCenter.default
150150
nc.removeObserver(observer, name: NSNotification.Name(rawValue: AnimationCancelledEvent), object: self)
151151
}
152152

@@ -155,7 +155,7 @@ public class Animation {
155155
/// This method is triggered when an animation is canceled. This can be used in place of `addCancelObserver` for objects outside the scope of the context in which the animation is created.
156156
public func postCancelledEvent() {
157157
DispatchQueue.main.async {
158-
NotificationCenter.default().post(name: Notification.Name(rawValue: AnimationCancelledEvent), object: self)
158+
NotificationCenter.default.post(name: Notification.Name(rawValue: AnimationCancelledEvent), object: self)
159159
}
160160
}
161161
}

C4/UI/Arc.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class Arc: Shape {
5252
super.init()
5353

5454
let arc = CGMutablePath()
55-
arc.addArc(nil, x: CGFloat(center.x), y: CGFloat(center.y), radius: CGFloat(radius), startAngle: CGFloat(start), endAngle: CGFloat(end), clockwise: !clockwise)
55+
arc.addArc(center: CGPoint(center), radius: CGFloat(radius), startAngle: CGFloat(start), endAngle: CGFloat(end), clockwise: !clockwise)
5656
path = Path(path: arc)
5757
adjustToFitPath()
5858
}

0 commit comments

Comments
 (0)