Skip to content

Commit 755c6af

Browse files
committed
Update to Swift 4.2
- Incorporate conditional compilation blocks for Swift 4.2 into the main code, replacing older Swift code. - Add CaseIterable to OctopusDirection - Replace arc4random and other legacy randomization features with native Swift randomization. - Remove CGFloat.randomInteger in favor of CGFloat(Int(CGFloat.random(in:))) - Improve formatting in Int+OctopusKit.
1 parent 9fc92d0 commit 755c6af

File tree

9 files changed

+13
-88
lines changed

9 files changed

+13
-88
lines changed

Sources/OctopusKit/Apple API Extensions/CoreGraphics/CGFloat+OctopusKit.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ import CoreGraphics
1010

1111
extension CGFloat {
1212

13-
// MARK: - Random Numbers
13+
// NOTE: Randomization extensions removed in favor of `CGFloat(Int(CGFloat.random(in:)))` etc.
1414

15-
/// Returns a random **integer** between the lower and upper bounds, **inclusive**.
16-
///
17-
/// Equivalent to calling `Int.random(from:to:)` (an OctopusKit extension) and converting it to `CGFloat`.
18-
///
19-
/// Uses `arc4random_uniform(_:)`. For GameplayKit-based randomization, use the extensions of `GKRandom`.
20-
public static func randomInteger(from lowerBound: CGFloat, to upperBound: CGFloat) -> CGFloat {
21-
// TODO: CHECK: Relevance after Swift 4.2 is released.
22-
23-
// Provided as a convenience to avoid having to explicitly cast from `Int` because working with SpriteKit primarily involves `CGFloat`.
24-
return CGFloat(Int.random(from: Int(lowerBound), to: Int(upperBound)))
25-
}
2615
}

Sources/OctopusKit/Apple API Extensions/Foundation/Array+OctopusKit.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ import GameplayKit
1515
extension Array {
1616

1717
// MARK: - Randomization
18-
19-
#if swift(>=4.2)
20-
// TODO: Remove `randomElement()` after Swift 4.2 is released.
21-
#else
22-
/// Returns a random element from this array.
23-
///
24-
/// Uses `arc4random_uniform(_:)`.
25-
///
26-
/// Can be used with array literals, e.g.: `[4, 8, 16, 42].randomElement()`
27-
public func randomElement() -> Element {
28-
return self[Int(arc4random_uniform(UInt32(self.count)))]
29-
}
30-
#endif
31-
18+
3219
/// Returns a random element from this array whose index is not in `skippingIndices`.
3320
///
3421
/// If the array is empty or if the list of exclusions prevents any acceptable value within `maximumAttempts`, `nil` is returned.

Sources/OctopusKit/Apple API Extensions/Foundation/Double+OctopusKit.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ extension Double {
1818
public static func unitRandom() -> Double {
1919
// CREDIT: Apple Adventure Sample
2020

21-
#if swift(>=4.2)
22-
// TODO: Remove `arc4random` variant after Swift 4.2 is released.
2321
let quotient = Double(Int.random(in: 0...Int.max)) / Double(Int.max) // CHECK: Is `0...Int.max` the same range as `arc4random()`?
24-
#else
25-
let quotient = Double(arc4random()) / Double(UInt32.max)
26-
#endif
2722

2823
return quotient
2924
}

Sources/OctopusKit/Apple API Extensions/Foundation/Int+OctopusKit.swift

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@ extension Int {
1414

1515
// MARK: - Random Numbers
1616

17-
/// Returns a random integer between the lower and upper bounds, **inclusive**.
18-
///
19-
/// Uses `arc4random_uniform(_:)`. For GameplayKit-based randomization, use the extensions of `GKRandom`.
20-
public static func random(from lowerBound: Int, to upperBound: Int) -> Int {
21-
assert(lowerBound < upperBound, "`lowerBound` should be less than `upperBound`") // Assert instead of silently failing, because anything that relies on random numbers better get random numbers.
22-
23-
#if swift(>=4.2)
24-
// TODO: Remove `random(from:to:)` after Swift 4.2 is released.
25-
return self.random(in: lowerBound...upperBound)
26-
#else
27-
let upperBoundInclusive = (upperBound + 1) - lowerBound // + 1 so the upper bound is included.
28-
let randomNumber = Int(arc4random_uniform(UInt32(upperBoundInclusive)))
29-
return randomNumber + lowerBound
30-
#endif
31-
}
32-
3317
/// Returns a random integer from `0` to `upperBound-1` (**not** including `upperBound`) that does not match any of the numbers provided in the exclusion list.
3418
///
3519
/// This method repeatedly calls the random number generator until it returns a number that is not in the exclusions list. If no acceptable number can be generated in `maximumAttempts` then `nil` will returned.
@@ -60,31 +44,20 @@ extension Int {
6044
// If there are no exclusions, just return the first random number generated.
6145

6246
if exclusions.isEmpty {
63-
#if swift(>=4.2)
64-
// TODO: Remove `arc4random` variant after Swift 4.2 is released.
6547
return self.random(in: 0 ..< upperBound)
66-
#else
67-
return Int(arc4random_uniform(UInt32(upperBound)))
68-
#endif
6948
}
7049
else {
71-
7250
var randomNumber: Int
7351
var attempts = 0
7452

7553
// Keep generating random numbers until we find one that's not in the exclusions list, or we've made the maximum number of attempts, so that we don't get stuck in an infinite loop.
7654

7755
repeat {
78-
79-
#if swift(>=4.2)
80-
// TODO: Remove `arc4random` variant after Swift 4.2 is released.
8156
randomNumber = self.random(in: 0 ..< upperBound)
82-
#else
83-
randomNumber = Int(arc4random_uniform(UInt32(upperBound)))
84-
#endif
85-
8657
attempts += 1
87-
} while exclusions.contains(randomNumber) && attempts < maximumAttempts
58+
59+
} while exclusions.contains(randomNumber)
60+
&& attempts < maximumAttempts
8861

8962
if !exclusions.contains(randomNumber) {
9063
return randomNumber

Sources/OctopusKit/Apple API Extensions/SpriteKit/SKNode+OctopusKit.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ extension SKNode {
1818
public class func nodeWithName<T>(name: String) -> T? {
1919
// CREDIT: Apple Adventure Sample
2020

21-
// TODO: Review for Swift 4.2 and iOS 12 when released.
22-
// return (try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [SKNode.self], from: Data(contentsOf: Bundle.main.url(forResource: name, withExtension: "sks")!))) as? T
23-
24-
return NSKeyedUnarchiver.unarchiveObject(withFile: Bundle.main.path(forResource: name, ofType: "sks")!) as? T
21+
// TODO: Verify the functionality of this Swift 4.2/iOS 12 update.
22+
return (try? NSKeyedUnarchiver.unarchivedObject(ofClasses: [SKNode.self], from: Data(contentsOf: Bundle.main.url(forResource: name, withExtension: "sks")!))) as? T
2523
}
2624

2725
/// MARK: - Initializers

Sources/OctopusKit/Components/Input/ImpactVibrationComponent.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
// Copyright © 2018 Invading Octopus. Licensed under Apache License v2.0 (see LICENSE.txt)
77
//
88

9+
import SpriteKit
910
import GameplayKit
1011

11-
#if swift(>=4.2)
12-
// TODO: Remove this temporary alias and incorporate changes into main code after Swift 4.2 launches.
13-
public typealias UIImpactFeedbackStyle = UIImpactFeedbackGenerator.FeedbackStyle
14-
#endif
15-
1612
#if os(iOS)
1713

1814
public final class ImpactVibrationComponent: VibrationComponent<UIImpactFeedbackGenerator> {
1915

20-
public var style: UIImpactFeedbackStyle
16+
public var style: UIImpactFeedbackGenerator.FeedbackStyle
2117

22-
public init(style: UIImpactFeedbackStyle = .light) {
18+
public init(style: UIImpactFeedbackGenerator.FeedbackStyle = .light) {
2319
self.style = style
2420
super.init()
2521
}

Sources/OctopusKit/Core/Launch/OctopusAppDelegate-iOS.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ class OctopusAppDelegate: UIResponder, UIApplicationDelegate {
2828
/// The Xcode project templates usually include a synthesized declaration of the property automatically for the app delegate. The default value of this synthesized property is `nil`, which causes the app to create a generic `UIWindow` object and assign it to the property. If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.
2929
var window: UIWindow?
3030

31-
#if swift(>=4.2)
32-
// TODO: Remove this temporary alias and incorporate changes into main code after Swift 4.2 launches.
33-
public typealias UIApplicationLaunchOptionsKey = UIApplication.LaunchOptionsKey
34-
#endif
35-
3631
/// Override point for customization after game launch.
37-
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
32+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
3833

3934
OctopusKit.logForFramework.add()
4035

Sources/OctopusKit/Core/Launch/OctopusSceneController-Universal.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,9 @@ public class OctopusSceneController: OSViewController, OctopusSceneDelegate {
112112
didSet { self.setNeedsUpdateOfHomeIndicatorAutoHidden() }
113113
}
114114

115-
#if swift(>=4.2)
116-
// TODO: Remove this conditional compilation flag and incorporate changes into main code after Swift 4.2 launches.
117115
public override var prefersHomeIndicatorAutoHidden: Bool {
118116
return prefersHomeIndicatorAutoHiddenOverride
119117
}
120-
#else
121-
public override func prefersHomeIndicatorAutoHidden() -> Bool {
122-
return prefersHomeIndicatorAutoHiddenOverride
123-
}
124-
#endif
125118

126119
/// Specifies whether whether the scene controller'€™s contents should auto rotate.
127120
///

Sources/OctopusKit/Miscellaneous/OctopusDirection.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
// Copyright © 2018 Invading Octopus. Licensed under Apache License v2.0 (see LICENSE.txt)
77
//
88

9-
// TODO: `CaseIterable` in Swift 4.2
10-
// TODO: Support for degrees
9+
// TODO: Support for degrees?
1110
// CHECK: Internationalization for string representations?
1211

1312
import SpriteKit
1413

15-
public enum OctopusDirection: String, CustomStringConvertible {
14+
public enum OctopusDirection: String, CustomStringConvertible, CaseIterable {
1615

1716
case north
1817
case northEast

0 commit comments

Comments
 (0)