From 19d0077816050667be82d28f926bd332f15327ca Mon Sep 17 00:00:00 2001 From: Aaron McTavish Date: Wed, 4 Jan 2017 14:35:51 +0000 Subject: [PATCH] Fix build errors --- Brunel.xcodeproj/project.pbxproj | 2 +- .../xcschemes/Brunel iOS.xcscheme | 2 +- .../xcschemes/Brunel tvOS.xcscheme | 2 +- Sources/API/TFLLineAPI.swift | 22 +++++++++++----- Sources/API/TFLRestAPI-Line-GET.swift | 22 ++++++++-------- Sources/API/TFLRestAPI-StopPoint-GET.swift | 8 +++--- Sources/API/TFLRestAPI.swift | 18 ++++++------- Sources/API/TFLStopPointAPI.swift | 18 ++++++++++--- Sources/API/WikipediaAPI.swift | 26 ++++++++++++------- Sources/API/WikipediaRestAPI-GET.swift | 8 +++--- Sources/API/WikipediaRestAPI.swift | 14 +++++----- .../LineSearchTableViewController.swift | 2 +- .../Extensions/Moya-ErrorDescriptions.swift | 16 ++++++------ Sources/Extensions/UIColor-Brightness.swift | 5 +++- Sources/Model/TFLLine.swift | 6 ++--- Sources/Model/TFLLineSearch.swift | 4 +-- 16 files changed, 102 insertions(+), 73 deletions(-) diff --git a/Brunel.xcodeproj/project.pbxproj b/Brunel.xcodeproj/project.pbxproj index 3b24877..340bc4b 100644 --- a/Brunel.xcodeproj/project.pbxproj +++ b/Brunel.xcodeproj/project.pbxproj @@ -544,7 +544,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0800; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = ustwo; TargetAttributes = { 008280F21C4CFE6500186513 = { diff --git a/Brunel.xcodeproj/xcshareddata/xcschemes/Brunel iOS.xcscheme b/Brunel.xcodeproj/xcshareddata/xcschemes/Brunel iOS.xcscheme index b88e404..1c1f16f 100644 --- a/Brunel.xcodeproj/xcshareddata/xcschemes/Brunel iOS.xcscheme +++ b/Brunel.xcodeproj/xcshareddata/xcschemes/Brunel iOS.xcscheme @@ -1,6 +1,6 @@ String { diff --git a/Sources/API/TFLRestAPI-Line-GET.swift b/Sources/API/TFLRestAPI-Line-GET.swift index 94181f4..5844720 100644 --- a/Sources/API/TFLRestAPI-Line-GET.swift +++ b/Sources/API/TFLRestAPI-Line-GET.swift @@ -12,19 +12,19 @@ import Foundation extension TFLRestAPI { func getLineStatus(_ modes: [TFLModes], completion: @escaping ([TFLLine]?, RestAPIError?) -> Void) { - tfl(lineProvider, target: .ModeStatus(modes: modes, detail: true), completion: { resultJSON, error in + tfl(lineProvider, target: .modeStatus(modes: modes, detail: true), completion: { resultJSON, error in if let resultJSON = resultJSON { var lines = [TFLLine]() guard let jsonArray = resultJSON.array else { - completion(nil, .InvalidJSON(localizedDescription: "Expected array.")) + completion(nil, .invalidJSON(localizedDescription: "Expected array.")) return } for jsonItem in jsonArray { guard let line = TFLLine(jsonObject: jsonItem) else { - completion(nil, .InvalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) + completion(nil, .invalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) return } @@ -36,19 +36,19 @@ extension TFLRestAPI { completion(nil, error) return } else { - completion(nil, .UnknownError) + completion(nil, .unknownError) return } }) } func getLineSearch(_ query: String, completion: @escaping ([TFLLineSearch]?, RestAPIError?) -> Void) { - tfl(lineProvider, target: .Search(query: query), completion: { resultJSON, error in + tfl(lineProvider, target: .search(query: query), completion: { resultJSON, error in if let resultJSON = resultJSON { var lines = [TFLLineSearch]() guard let jsonArray = resultJSON["searchMatches"].array else { - completion(nil, .InvalidJSON(localizedDescription: "Expected array.")) + completion(nil, .invalidJSON(localizedDescription: "Expected array.")) return } @@ -64,26 +64,26 @@ extension TFLRestAPI { completion(nil, error) return } else { - completion(nil, .UnknownError) + completion(nil, .unknownError) return } }) } func getLineStatus(_ lines: [String], completion: @escaping ([TFLLine]?, RestAPIError?) -> Void) { - tfl(lineProvider, target: .Status(lines: lines, detail: true), completion: { resultJSON, error in + tfl(lineProvider, target: .status(lines: lines, detail: true), completion: { resultJSON, error in if let resultJSON = resultJSON { var lines = [TFLLine]() guard let jsonArray = resultJSON.array else { - completion(nil, .InvalidJSON(localizedDescription: "Expected array.")) + completion(nil, .invalidJSON(localizedDescription: "Expected array.")) return } for jsonItem in jsonArray { guard let line = TFLLine(jsonObject: jsonItem) else { - completion(nil, .InvalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) + completion(nil, .invalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) return } @@ -95,7 +95,7 @@ extension TFLRestAPI { completion(nil, error) return } else { - completion(nil, .UnknownError) + completion(nil, .unknownError) return } }) diff --git a/Sources/API/TFLRestAPI-StopPoint-GET.swift b/Sources/API/TFLRestAPI-StopPoint-GET.swift index e62e668..3d12801 100644 --- a/Sources/API/TFLRestAPI-StopPoint-GET.swift +++ b/Sources/API/TFLRestAPI-StopPoint-GET.swift @@ -12,21 +12,21 @@ import Foundation extension TFLRestAPI { func getStopPointList(_ modes: [TFLModes], completion: @escaping ([TFLLine]?, RestAPIError?) -> Void) { - tfl(stopPointProvider, target: .StopPointList(modes: modes), completion: { resultJSON, error in + tfl(stopPointProvider, target: .stopPointList(modes: modes), completion: { resultJSON, error in if let resultJSON = resultJSON { print(resultJSON.rawString()!) var lines = [TFLLine]() guard let jsonArray = resultJSON.array else { - completion(nil, .InvalidJSON(localizedDescription: "Expected array.")) + completion(nil, .invalidJSON(localizedDescription: "Expected array.")) return } for jsonItem in jsonArray { guard let line = TFLLine(jsonObject: jsonItem) else { - completion(nil, .InvalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) + completion(nil, .invalidJSON(localizedDescription: "Bad JSON to initialize `TFLLine`.")) return } @@ -38,7 +38,7 @@ extension TFLRestAPI { completion(nil, error) return } else { - completion(nil, .UnknownError) + completion(nil, .unknownError) return } }) diff --git a/Sources/API/TFLRestAPI.swift b/Sources/API/TFLRestAPI.swift index fb85e60..257d955 100644 --- a/Sources/API/TFLRestAPI.swift +++ b/Sources/API/TFLRestAPI.swift @@ -12,7 +12,7 @@ import Moya import SwiftyJSON -enum RestAPIError: Error { +enum RestAPIError: Swift.Error { case genericError(localizedDescription: String) case invalidJSON(localizedDescription: String) case unknownError @@ -57,7 +57,7 @@ struct TFLRestAPI { */ static func TFLEndpointClosure(_ appID: String, appKey: String) -> ((T) -> Endpoint) { return { (target: T) -> Endpoint in - var newParameters = [String : AnyObject]() + var newParameters = [String : Any]() newParameters["app_id"] = appID newParameters["app_key"] = appKey @@ -68,7 +68,7 @@ struct TFLRestAPI { } } - let endpoint: Endpoint = Endpoint(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: newParameters, parameterEncoding: .URL, httpHeaderFields: nil) + let endpoint: Endpoint = Endpoint(url: url(target), sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: newParameters, parameterEncoding: URLEncoding.default, httpHeaderFields: nil) return endpoint } @@ -81,21 +81,21 @@ struct TFLRestAPI { - parameter target: Endpoint from which to request data. - parameter completion: Completion handler that returns either a valid JSON object or an error based on the response. */ - internal func tfl(_ provider: MoyaProvider, target: T, completion: (JSON?, RestAPIError?) -> Void) { + internal func tfl(_ provider: MoyaProvider, target: T, completion: @escaping (JSON?, RestAPIError?) -> Void) { provider.request(target, completion: { result in switch result { - case let .Success(response): + case let .success(response): do { let jsonObj = try response.mapJSON() let resultJSON = JSON(jsonObj) completion(resultJSON, nil) } catch let error as NSError { - completion(nil, .GenericError(localizedDescription: error.localizedDescription)) + completion(nil, .genericError(localizedDescription: error.localizedDescription)) return } - case let .Failure(error): - completion(nil, .GenericError(localizedDescription: error.description)) + case let .failure(error): + completion(nil, .genericError(localizedDescription: error.description)) return } }) @@ -112,5 +112,5 @@ struct TFLRestAPI { - returns: URL string for endpoint. */ func url(_ route: TargetType) -> String { - return route.baseURL.URLByAppendingPathComponent(route.path)!.absoluteString! + return route.baseURL.appendingPathComponent(route.path).absoluteString } diff --git a/Sources/API/TFLStopPointAPI.swift b/Sources/API/TFLStopPointAPI.swift index 043623a..c21d941 100644 --- a/Sources/API/TFLStopPointAPI.swift +++ b/Sources/API/TFLStopPointAPI.swift @@ -22,6 +22,7 @@ enum TFLStopPointAPI { extension TFLStopPointAPI: TargetType { + var baseURL: URL { return URL(string: "https://api.tfl.gov.uk")! } @@ -36,11 +37,19 @@ extension TFLStopPointAPI: TargetType { var method: Moya.Method { switch self { case .stopPointList: - return .GET + return .get } } - var parameters: [String: AnyObject]? { + var multipartBody: [MultipartFormData]? { + return nil + } + + var parameterEncoding: ParameterEncoding { + return URLEncoding.default + } + + var parameters: [String: Any]? { switch self { case .stopPointList: return nil @@ -52,10 +61,11 @@ extension TFLStopPointAPI: TargetType { return emptyStringData } - var multipartBody: [MultipartFormData]? { - return nil + var task: Task { + return .request } + // MARK: - Convenience fileprivate func csvFromArray(_ value: [String]) -> String { diff --git a/Sources/API/WikipediaAPI.swift b/Sources/API/WikipediaAPI.swift index 3656ebb..dc360d2 100644 --- a/Sources/API/WikipediaAPI.swift +++ b/Sources/API/WikipediaAPI.swift @@ -30,18 +30,26 @@ extension WikipediaAPI: TargetType { var method: Moya.Method { switch self { case .queryTitle: - return .GET + return .get } } - var parameters: [String: AnyObject]? { + var multipartBody: [MultipartFormData]? { + return nil + } + + var parameterEncoding: ParameterEncoding { + return URLEncoding.default + } + + var parameters: [String: Any]? { switch self { case let .queryTitle(title): - return ["action" : "query" as AnyObject, - "prop" : "extracts" as AnyObject, - "exintro" : "" as AnyObject, - "explaintext" : "" as AnyObject, - "titles" : title as AnyObject] + return ["action" : "query", + "prop" : "extracts", + "exintro" : "", + "explaintext" : "", + "titles" : title] } } @@ -50,8 +58,8 @@ extension WikipediaAPI: TargetType { return emptyStringData } - var multipartBody: [MultipartFormData]? { - return nil + var task: Task { + return .request } } diff --git a/Sources/API/WikipediaRestAPI-GET.swift b/Sources/API/WikipediaRestAPI-GET.swift index 770640f..a962b8d 100644 --- a/Sources/API/WikipediaRestAPI-GET.swift +++ b/Sources/API/WikipediaRestAPI-GET.swift @@ -12,17 +12,17 @@ import Foundation extension WikipediaRestAPI { func getQueryTitle(_ title: String, completion: @escaping (String?, RestAPIError?) -> Void) { - wikipedia(provider, target: .QueryTitle(title: title), completion: { resultJSON, error in + wikipedia(provider, target: .queryTitle(title: title), completion: { resultJSON, error in if let resultJSON = resultJSON { guard let pagesDictionary = resultJSON["query"]["pages"].dictionary else { - completion(nil, .InvalidJSON(localizedDescription: "Expected `pages` dictionary.")) + completion(nil, .invalidJSON(localizedDescription: "Expected `pages` dictionary.")) return } let firstKey = Array(pagesDictionary.keys)[0] guard let extract = pagesDictionary[firstKey]?["extract"].string else { - completion(nil, .InvalidJSON(localizedDescription: "Expected `extract`.")) + completion(nil, .invalidJSON(localizedDescription: "Expected `extract`.")) return } @@ -31,7 +31,7 @@ extension WikipediaRestAPI { completion(nil, error) return } else { - completion(nil, .UnknownError) + completion(nil, .unknownError) return } }) diff --git a/Sources/API/WikipediaRestAPI.swift b/Sources/API/WikipediaRestAPI.swift index fa536cc..a801180 100644 --- a/Sources/API/WikipediaRestAPI.swift +++ b/Sources/API/WikipediaRestAPI.swift @@ -37,7 +37,7 @@ struct WikipediaRestAPI { */ static func WikipediaEndpointClosure() -> ((T) -> Endpoint) { return { (target: T) -> Endpoint in - var newParameters = [String : AnyObject]() + var newParameters = [String : Any]() newParameters["format"] = "json" @@ -47,7 +47,7 @@ struct WikipediaRestAPI { } } - let endpoint: Endpoint = Endpoint(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: newParameters, parameterEncoding: .URL, httpHeaderFields: nil) + let endpoint: Endpoint = Endpoint(url: url(target), sampleResponseClosure: {.networkResponse(200, target.sampleData)}, method: target.method, parameters: newParameters, parameterEncoding: URLEncoding.default, httpHeaderFields: nil) return endpoint } @@ -60,21 +60,21 @@ struct WikipediaRestAPI { - parameter target: Endpoint from which to request data. - parameter completion: Completion handler that returns either a valid JSON object or an error based on the response. */ - internal func wikipedia(_ provider: MoyaProvider, target: T, completion: (JSON?, RestAPIError?) -> Void) { + internal func wikipedia(_ provider: MoyaProvider, target: T, completion: @escaping (JSON?, RestAPIError?) -> Void) { provider.request(target, completion: { result in switch result { - case let .Success(response): + case let .success(response): do { let jsonObj = try response.mapJSON() let resultJSON = JSON(jsonObj) completion(resultJSON, nil) } catch let error as NSError { - completion(nil, .GenericError(localizedDescription: error.localizedDescription)) + completion(nil, .genericError(localizedDescription: error.localizedDescription)) return } - case let .Failure(error): - completion(nil, .GenericError(localizedDescription: error.description)) + case let .failure(error): + completion(nil, .genericError(localizedDescription: error.description)) return } }) diff --git a/Sources/Controller/LineSearchTableViewController.swift b/Sources/Controller/LineSearchTableViewController.swift index a4bd4d8..fed7497 100644 --- a/Sources/Controller/LineSearchTableViewController.swift +++ b/Sources/Controller/LineSearchTableViewController.swift @@ -230,7 +230,7 @@ final class LineSearchTableViewController: UITableViewController, UISearchResult } } - modes.sorted { $0.description < $1.description } + modes.sort { $0.description < $1.description } } fileprivate func updateSearchArray() { diff --git a/Sources/Extensions/Moya-ErrorDescriptions.swift b/Sources/Extensions/Moya-ErrorDescriptions.swift index 1f4d9b4..e187365 100644 --- a/Sources/Extensions/Moya-ErrorDescriptions.swift +++ b/Sources/Extensions/Moya-ErrorDescriptions.swift @@ -11,21 +11,21 @@ import Foundation import Moya -extension Moya.Error: CustomStringConvertible { +extension MoyaError: CustomStringConvertible { public var description: String { switch self { - case .Data: - return "Data Error" - case .ImageMapping: + case .imageMapping: return "Image Mapping Error" - case .JSONMapping: + case .jsonMapping: return "JSON Mapping Error" - case .StatusCode: + case .requestMapping: + return "Request Error" + case .statusCode: return "Status Code Error" - case .StringMapping: + case .stringMapping: return "String Mapping Error" - case .Underlying: + case .underlying: let nsError = self as NSError return nsError.localizedDescription } diff --git a/Sources/Extensions/UIColor-Brightness.swift b/Sources/Extensions/UIColor-Brightness.swift index 1020ab6..973d68f 100644 --- a/Sources/Extensions/UIColor-Brightness.swift +++ b/Sources/Extensions/UIColor-Brightness.swift @@ -18,7 +18,10 @@ extension UIColor { - returns: Whether or not the color is light. */ func isLight() -> Bool { - let components = self.cgColor.components + guard let components = self.cgColor.components else { + return true + } + let numerator = (components[0] * 299) + (components[1] * 587) + (components[2] * 114) let brightness = numerator / 1000 diff --git a/Sources/Model/TFLLine.swift b/Sources/Model/TFLLine.swift index 08f958a..0290076 100644 --- a/Sources/Model/TFLLine.swift +++ b/Sources/Model/TFLLine.swift @@ -63,7 +63,7 @@ struct TFLLine { } lineStatusesArray.append(lineStatus) } - lineStatuses = lineStatusesArray.sort { $0.severity > $1.severity } + lineStatuses = lineStatusesArray.sorted { $0.severity > $1.severity } var serviceTypesArray = [TFLLineServiceType]() for jsonServiceType in jsonServiceTypes { @@ -76,13 +76,13 @@ struct TFLLine { serviceTypes = serviceTypesArray if mode == .Underground, - let undergroundIndex = TFLUnderground.allValues.indexOf({ $0.description.uppercaseString == jsonName.uppercaseString }) { + let undergroundIndex = TFLUnderground.allValues.index(where: { $0.description.uppercased() == jsonName.uppercased() }) { let undergroundLine = TFLUnderground.allValues[undergroundIndex] color = undergroundLine.color } else if mode == .NationalRail, - let railIndex = TFLNationalRail.allValues.indexOf({ $0.description.uppercaseString == jsonName.uppercaseString }) { + let railIndex = TFLNationalRail.allValues.index(where: { $0.description.uppercased() == jsonName.uppercased() }) { let railLine = TFLNationalRail.allValues[railIndex] diff --git a/Sources/Model/TFLLineSearch.swift b/Sources/Model/TFLLineSearch.swift index 8861fd1..a7cafab 100644 --- a/Sources/Model/TFLLineSearch.swift +++ b/Sources/Model/TFLLineSearch.swift @@ -43,13 +43,13 @@ struct TFLLineSearch { name = jsonName if mode == .Underground, - let undergroundIndex = TFLUnderground.allValues.indexOf({ $0.description.uppercaseString == jsonName.uppercaseString }) { + let undergroundIndex = TFLUnderground.allValues.index(where: { $0.description.uppercased() == jsonName.uppercased() }) { let undergroundLine = TFLUnderground.allValues[undergroundIndex] color = undergroundLine.color } else if mode == .NationalRail, - let railIndex = TFLNationalRail.allValues.indexOf({ $0.description.uppercaseString == jsonName.uppercaseString }) { + let railIndex = TFLNationalRail.allValues.index(where: { $0.description.uppercased() == jsonName.uppercased() }) { let railLine = TFLNationalRail.allValues[railIndex]