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 14 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
2 changes: 2 additions & 0 deletions Compass.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ Pod::Spec.new do |s|

s.source_files = 'Sources/**/*'
s.frameworks = 'Foundation'

s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0' }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks

end
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
32 changes: 16 additions & 16 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,21 +18,21 @@ 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 {
return parseAsURL(url, payload: payload)
guard !(path.contains("?") || path.contains("#")) else {
return parseComponents(url: url, payload: payload)
}

let results: [Result] = routes.flatMap {
return findMatch($0, pathString: path)
}.sort { (r1: Result, r2: Result) in
if r1.concreteMatchCount == r2.concreteMatchCount {
return r1.wildcardMatchCount > r2.wildcardMatchCount
}
return findMatch(routeString: $0, pathString: path)
}.sorted { (r1: Result, r2: Result) in
if r1.concreteMatchCount == r2.concreteMatchCount {
return r1.wildcardMatchCount > r2.wildcardMatchCount
}

return r1.concreteMatchCount > r2.concreteMatchCount
return r1.concreteMatchCount > r2.concreteMatchCount
}

if let result = results.first {
Expand All @@ -42,10 +42,10 @@ public struct Compass {
return nil
}

static func parseAsURL(url: NSURL, payload: Any? = nil) -> Location? {
static func parseComponents(url: URL, payload: Any? = nil) -> Location? {
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 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 }
openURL(url)
public static func navigate(to urn: String, scheme: String = Compass.scheme) {
guard let url = URL(string: "\(scheme)\(urn)") else { return }
open(url: url)
}
}
14 changes: 7 additions & 7 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: Error, from currentController: Controller)
}

public struct Router: Routable {
Expand All @@ -23,14 +23,14 @@ 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: RouteError.notFound, from: currentController)
return
}

do {
try route.navigate(to: location, from: currentController)
} catch {
errorRoute?.handle(error, from: currentController)
errorRoute?.handle(routeError: error, from: currentController)
}
}
}
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 open(url: URL) {
NSWorkspace.shared().open(url)
}
#else
public typealias Controller = UIViewController

func openURL(URL: NSURL) {
UIApplication.sharedApplication().openURL(URL)
func open(url: URL) {
UIApplication.shared.openURL(url)
}
#endif
Loading