-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to Swift 3 #40
Changes from 7 commits
1d8e71f
c7df0e4
e720a02
9bf294c
16a3924
a4af7a2
d8764b1
92a40ff
a3e7741
39f8d46
ebbafc3
51eb803
f585b12
ca0909a
76bb282
92e20f7
89f4e49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ public struct Compass { | |
concreteMatchCount: Int, | ||
wildcardMatchCount: Int) | ||
|
||
private static var internalScheme = "" | ||
fileprivate static var internalScheme = "" | ||
public static var delimiter: String = ":" | ||
|
||
public static var scheme: String { | ||
|
@@ -18,16 +18,16 @@ public struct Compass { | |
|
||
public static var routes = [String]() | ||
|
||
public static func parse(url: NSURL, payload: Any? = nil) -> Location? { | ||
let path = url.absoluteString.substringFromIndex(scheme.endIndex) | ||
public static func parse(_ url: URL, payload: Any? = nil) -> Location? { | ||
let path = url.absoluteString.substring(from: scheme.endIndex) | ||
|
||
guard !(path.containsString("?") || path.containsString("#")) else { | ||
guard !(path.contains("?") || path.contains("#")) else { | ||
return parseAsURL(url, payload: payload) | ||
} | ||
|
||
let results: [Result] = routes.flatMap { | ||
return findMatch($0, pathString: path) | ||
}.sort { (r1: Result, r2: Result) in | ||
}.sorted { (r1: Result, r2: Result) in | ||
if r1.concreteMatchCount == r2.concreteMatchCount { | ||
return r1.wildcardMatchCount > r2.wildcardMatchCount | ||
} | ||
|
@@ -42,10 +42,10 @@ public struct Compass { | |
return nil | ||
} | ||
|
||
static func parseAsURL(url: NSURL, payload: Any? = nil) -> Location? { | ||
static func parseAsURL(_ url: URL, payload: Any? = nil) -> Location? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that |
||
guard let route = url.host else { return nil } | ||
|
||
let urlComponents = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) | ||
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) | ||
var arguments = [String : String]() | ||
|
||
urlComponents?.queryItems?.forEach { queryItem in | ||
|
@@ -59,7 +59,7 @@ public struct Compass { | |
return Location(path: route, arguments: arguments, payload: payload) | ||
} | ||
|
||
static func findMatch(routeString: String, pathString: String) -> Result? { | ||
static func findMatch(_ routeString: String, pathString: String) -> Result? { | ||
let routes = routeString.split(delimiter) | ||
let paths = pathString.split(delimiter) | ||
|
||
|
@@ -94,8 +94,8 @@ public struct Compass { | |
|
||
extension Compass { | ||
|
||
public static func navigate(urn: String, scheme: String = Compass.scheme) { | ||
guard let url = NSURL(string: "\(scheme)\(urn)") else { return } | ||
public static func navigate(_ urn: String, scheme: String = Compass.scheme) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense |
||
guard let url = URL(string: "\(scheme)\(urn)") else { return } | ||
openURL(url) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
public enum RouteError: ErrorType { | ||
case NotFound | ||
case InvalidArguments(Location) | ||
case InvalidPayload(Location) | ||
public enum RouteError: Error { | ||
case notFound | ||
case invalidArguments(Location) | ||
case invalidPayload(Location) | ||
} | ||
|
||
public protocol Routable { | ||
|
@@ -11,7 +11,7 @@ public protocol Routable { | |
|
||
public protocol ErrorRoutable { | ||
|
||
func handle(routeError: ErrorType, from currentController: Controller) | ||
func handle(_ routeError: Error, from currentController: Controller) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave |
||
} | ||
|
||
public struct Router: Routable { | ||
|
@@ -23,7 +23,7 @@ public struct Router: Routable { | |
|
||
public func navigate(to location: Location, from currentController: Controller) { | ||
guard let route = routes[location.path] else { | ||
errorRoute?.handle(RouteError.NotFound, from: currentController) | ||
errorRoute?.handle(RouteError.notFound, from: currentController) | ||
return | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,25 @@ import Foundation | |
|
||
extension String { | ||
|
||
func split(delimiter: String) -> [String] { | ||
let components = componentsSeparatedByString(delimiter) | ||
func split(_ delimiter: String) -> [String] { | ||
let components = self.components(separatedBy: delimiter) | ||
return components != [""] ? components : [] | ||
} | ||
|
||
func replace(string: String, with withString: String) -> String { | ||
return stringByReplacingOccurrencesOfString(string, withString: withString) | ||
func replace(_ string: String, with withString: String) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just a syntactic sugar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but it makes less sense in Swift 3 I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, lets get rid of the Sugar that are fixed by Apple in Swift 3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @onmyway133 Would you mind to remove this functions an use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
return replacingOccurrences(of: string, with: withString) | ||
} | ||
|
||
func queryParameters() -> [String: String] { | ||
var parameters = [String: String]() | ||
|
||
let separatorCharacters = NSCharacterSet(charactersInString: "&;") | ||
self.componentsSeparatedByCharactersInSet(separatorCharacters).forEach { (pair) in | ||
let separatorCharacters = CharacterSet(charactersIn: "&;") | ||
self.components(separatedBy: separatorCharacters).forEach { (pair) in | ||
|
||
if let equalSeparator = pair.rangeOfString("=") { | ||
let name = pair.substringToIndex(equalSeparator.startIndex) | ||
let value = pair.substringFromIndex(equalSeparator.startIndex.advancedBy(1)) | ||
let cleaned = value.stringByRemovingPercentEncoding ?? value | ||
if let equalSeparator = pair.range(of: "=") { | ||
let name = pair.substring(to: equalSeparator.lowerBound) | ||
let value = pair.substring(from: pair.index(equalSeparator.lowerBound, offsetBy: 1)) | ||
let cleaned = value.removingPercentEncoding ?? value | ||
|
||
parameters[name] = cleaned | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,13 @@ | |
#if os(OSX) | ||
public typealias Controller = NSViewController | ||
|
||
func openURL(URL: NSURL) { | ||
NSWorkspace.sharedWorkspace().openURL(URL) | ||
func openURL(_ url: URL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting the _ seems to be the default everywhere tho. Which is a bit stupid, since, if everyone needs underscore, why don't we delete the underscore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RamonGilabert I think the idea is to move the last part of the function's name to be an argument name, like in the example above (like in good old Objective C times) So we have to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, sorry, I thought you suggested |
||
NSWorkspace.shared().open(url) | ||
} | ||
#else | ||
public typealias Controller = UIViewController | ||
|
||
func openURL(URL: NSURL) { | ||
UIApplication.sharedApplication().openURL(URL) | ||
func openURL(_ url: Foundation.URL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need Foundation here. |
||
UIApplication.shared.openURL(url) | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave it like
public static func parse(url: NSURL, payload: Any? = nil)
. Thought? @onmyway133 @zenangst @RamonGilabert ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, remove
_
. I like labels :)