Skip to content
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

Merged
merged 17 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
os:
- osx
osx_image: xcode7.3
osx_image: xcode8
language: objective-c

install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./swiftlint.sh; fi
script:
- xcodebuild clean build -project Compass.xcodeproj -scheme Compass-iOS -sdk iphonesimulator
- xcodebuild test -project Compass.xcodeproj -scheme Compass-iOS -sdk iphonesimulator
- xcodebuild test -project Compass.xcodeproj -scheme "Compass-iOS" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0'
- xcodebuild clean build -project Compass.xcodeproj -scheme Compass-Mac -sdk macosx
- xcodebuild test -project Compass.xcodeproj -scheme Compass-Mac -sdk macosx
notifications:
Expand Down
10 changes: 10 additions & 0 deletions Compass.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@
TargetAttributes = {
D5B2E89E1C3A780C00C0327D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
};
D5B2E8A81C3A780C00C0327D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
};
D5C6293F1C3A7FAA007F7B7C = {
CreatedOnToolsVersion = 7.2;
Expand Down Expand Up @@ -495,6 +497,7 @@
PRODUCT_NAME = Compass;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -514,6 +517,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "no.hyper.Compass-iOS";
PRODUCT_NAME = Compass;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -527,6 +531,7 @@
PRODUCT_BUNDLE_IDENTIFIER = no.hyper.CompassTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -539,6 +544,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = no.hyper.CompassTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -563,6 +569,7 @@
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -586,6 +593,7 @@
PRODUCT_NAME = Compass;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -601,6 +609,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "no.hyper.Compass-MacTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -616,6 +625,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "no.hyper.Compass-MacTests";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![License](https://img.shields.io/cocoapods/l/Compass.svg?style=flat)](http://cocoadocs.org/docsets/Compass)
[![Platform](https://img.shields.io/cocoapods/p/Compass.svg?style=flat)](http://cocoadocs.org/docsets/Compass)
[![CI Status](http://img.shields.io/travis/hyperoslo/Compass.svg?style=flat)](https://travis-ci.org/hyperoslo/Compass)
![Swift](https://img.shields.io/badge/%20in-swift%203.0-orange.svg)

Compass helps you setup a central navigation system for your application.
This has many benefits, one of them being that controllers can now be
Expand Down
20 changes: 10 additions & 10 deletions Sources/Compass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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? {
Copy link
Contributor

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 ?

Copy link
Contributor

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 :)

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
}
Expand All @@ -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? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe parseAsUrl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that parseAsURL is actually dealing with url components, so I rename it to parseComponents

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
Expand All @@ -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)

Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe navigate(to urn

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
}
}
12 changes: 6 additions & 6 deletions Sources/Router.swift
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 {
Expand All @@ -11,7 +11,7 @@ public protocol Routable {

public protocol ErrorRoutable {

func handle(routeError: ErrorType, from currentController: Controller)
func handle(_ routeError: Error, from currentController: Controller)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave routeError in function signature, or maybe just error.

}

public struct Router: Routable {
Expand All @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a syntactic sugar

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it makes less sense in Swift 3 I think.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onmyway133 Would you mind to remove this functions an use replacingOccurrences(of: string, with: withString) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/TypeAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
#if os(OSX)
public typealias Controller = NSViewController

func openURL(URL: NSURL) {
NSWorkspace.sharedWorkspace().openURL(URL)
func openURL(_ url: URL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think func open(url: URL) is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 _ as less as possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry, I thought you suggested func openURL(url: URL).

NSWorkspace.shared().open(url)
}
#else
public typealias Controller = UIViewController

func openURL(URL: NSURL) {
UIApplication.sharedApplication().openURL(URL)
func openURL(_ url: Foundation.URL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need Foundation here.

UIApplication.shared.openURL(url)
}
#endif
Loading