From 9615a669fb686ae9cbe3e993bf3000c82a505c0f Mon Sep 17 00:00:00 2001 From: Bobby Sudekum Date: Thu, 30 Mar 2017 07:31:51 -0700 Subject: [PATCH] Fix swift 3.1 warnings (#119) * Fix swift 3.1 warnings * Fixed warnings in test code * Fail gracefully decoding invalid maneuver direction --- MapboxDirections/MBIntersection.swift | 2 +- MapboxDirections/MBRouteStep.swift | 6 ++++-- MapboxDirections/MBWaypoint.swift | 2 +- MapboxDirectionsTests/Fixture.swift | 6 +++--- MapboxDirectionsTests/V4Tests.swift | 4 ++-- MapboxDirectionsTests/V5Tests.swift | 4 ++-- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/MapboxDirections/MBIntersection.swift b/MapboxDirections/MBIntersection.swift index b7a58fc64..4a5d87b35 100644 --- a/MapboxDirections/MBIntersection.swift +++ b/MapboxDirections/MBIntersection.swift @@ -101,7 +101,7 @@ public class Intersection: NSObject, NSSecureCoding { outletIndex = decoder.decodeInteger(forKey: "outletIndex") approachLanes = decoder.decodeObject(of: [NSArray.self, Lane.self], forKey: "approachLanes") as? [Lane] - usableApproachLanes = decoder.decodeObject(of: NSIndexSet.self, forKey: "usableApproachLanes") as? IndexSet + usableApproachLanes = decoder.decodeObject(of: NSIndexSet.self, forKey: "usableApproachLanes") as IndexSet? } open static var supportsSecureCoding = true diff --git a/MapboxDirections/MBRouteStep.swift b/MapboxDirections/MBRouteStep.swift index 2d141fda2..d49ed7e9b 100644 --- a/MapboxDirections/MBRouteStep.swift +++ b/MapboxDirections/MBRouteStep.swift @@ -557,7 +557,9 @@ open class RouteStep: NSObject, NSSecureCoding { return nil } maneuverType = ManeuverType(description: maneuverTypeDescription) - let maneuverDirectionDescription = decoder.decodeObject(of: NSString.self, forKey: "maneuverDirection") as! String + guard let maneuverDirectionDescription = decoder.decodeObject(of: NSString.self, forKey: "maneuverDirection") as String? else { + return nil + } maneuverDirection = ManeuverDirection(description: maneuverDirectionDescription) if let maneuverLocationDictionary = decoder.decodeObject(of: [NSDictionary.self, NSString.self, NSNumber.self], forKey: "maneuverLocation") as? [String: CLLocationDegrees], @@ -574,7 +576,7 @@ open class RouteStep: NSObject, NSSecureCoding { expectedTravelTime = decoder.decodeDouble(forKey: "expectedTravelTime") names = decoder.decodeObject(of: [NSArray.self, NSString.self], forKey: "names") as? [String] - guard let transportTypeDescription = decoder.decodeObject(of: NSString.self, forKey: "transportType") as? String else { + guard let transportTypeDescription = decoder.decodeObject(of: NSString.self, forKey: "transportType") as String? else { return nil } transportType = TransportType(description: transportTypeDescription) diff --git a/MapboxDirections/MBWaypoint.swift b/MapboxDirections/MBWaypoint.swift index acc37bd24..83d7ee507 100644 --- a/MapboxDirections/MBWaypoint.swift +++ b/MapboxDirections/MBWaypoint.swift @@ -67,7 +67,7 @@ open class Waypoint: NSObject, NSCopying, NSSecureCoding { coordinateAccuracy = decoder.decodeDouble(forKey: "coordinateAccuracy") heading = decoder.decodeDouble(forKey: "heading") headingAccuracy = decoder.decodeDouble(forKey: "headingAccuracy") - name = decoder.decodeObject(of: NSString.self, forKey: "name") as? String + name = decoder.decodeObject(of: NSString.self, forKey: "name") as String? } open func encode(with coder: NSCoder) { diff --git a/MapboxDirectionsTests/Fixture.swift b/MapboxDirectionsTests/Fixture.swift index 87b284bcd..3509d5148 100644 --- a/MapboxDirectionsTests/Fixture.swift +++ b/MapboxDirectionsTests/Fixture.swift @@ -15,17 +15,17 @@ internal class Fixture { } } - internal class func JSONFromFileNamed(name: String) -> [String: AnyObject] { + internal class func JSONFromFileNamed(name: String) -> [String: Any] { guard let path = Bundle(for: self).path(forResource: name, ofType: "json") else { XCTAssert(false, "Fixture \(name) not found.") return [:] } - guard let data = NSData(contentsOfFile: path) as? Data else { + guard let data = NSData(contentsOfFile: path) as Data? else { XCTAssert(false, "No data found at \(path).") return [:] } do { - return try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject] + return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] } catch { XCTAssert(false, "Unable to decode JSON fixture at \(path): \(error).") return [:] diff --git a/MapboxDirectionsTests/V4Tests.swift b/MapboxDirectionsTests/V4Tests.swift index e9edad279..5c037981c 100644 --- a/MapboxDirectionsTests/V4Tests.swift +++ b/MapboxDirectionsTests/V4Tests.swift @@ -35,7 +35,7 @@ class V4Tests: XCTestCase { options.includesAlternativeRoutes = true var route: Route? let task = Directions(accessToken: BogusToken).calculate(options) { (waypoints, routes, error) in - XCTAssertNil(error, "Error: \(error)") + XCTAssertNil(error, "Error: \(error!)") XCTAssertNotNil(routes) XCTAssertEqual(routes!.count, 2) @@ -46,7 +46,7 @@ class V4Tests: XCTestCase { XCTAssertNotNil(task) waitForExpectations(timeout: 2) { (error) in - XCTAssertNil(error, "Error: \(error)") + XCTAssertNil(error, "Error: \(error!)") XCTAssertEqual(task.state, .completed) } diff --git a/MapboxDirectionsTests/V5Tests.swift b/MapboxDirectionsTests/V5Tests.swift index 2a62c1716..4bf3cfbc1 100644 --- a/MapboxDirectionsTests/V5Tests.swift +++ b/MapboxDirectionsTests/V5Tests.swift @@ -37,7 +37,7 @@ class V5Tests: XCTestCase { options.routeShapeResolution = .full var route: Route? let task = Directions(accessToken: BogusToken).calculate(options) { (waypoints, routes, error) in - XCTAssertNil(error, "Error: \(error)") + XCTAssertNil(error, "Error: \(error!)") XCTAssertNotNil(routes) XCTAssertEqual(routes!.count, 2) @@ -48,7 +48,7 @@ class V5Tests: XCTestCase { XCTAssertNotNil(task) waitForExpectations(timeout: 2) { (error) in - XCTAssertNil(error, "Error: \(error)") + XCTAssertNil(error, "Error: \(error!)") XCTAssertEqual(task.state, .completed) }