Skip to content

Commit

Permalink
Merge pull request #40 from hyperoslo/swift-3
Browse files Browse the repository at this point in the history
Update to Swift 3
  • Loading branch information
zenangst authored Oct 13, 2016
2 parents 84242c0 + 89f4e49 commit d5833d6
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 108 deletions.
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' }
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
35 changes: 18 additions & 17 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 All @@ -71,7 +71,8 @@ public struct Compass {

for (route, path) in zip(routes, paths) {
if route.hasPrefix("{") {
let key = route.replace("{", with: "").replace("}", with: "")
let key = route.replacingOccurrences(of: "{", with: "")
.replacingOccurrences(of: "}", with: "")
arguments[key] = path

wildcardMatchCount += 1
Expand All @@ -94,8 +95,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)
}
}
18 changes: 9 additions & 9 deletions Sources/Router.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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 {

func navigate(to location: Location, from currentController: Controller) throws
func navigate(to location: Location, from currentController: CurrentController) throws
}

public protocol ErrorRoutable {

func handle(routeError: ErrorType, from currentController: Controller)
func handle(routeError error: Error, from currentController: CurrentController)
}

public struct Router: Routable {
Expand All @@ -21,16 +21,16 @@ public struct Router: Routable {

public init() {}

public func navigate(to location: Location, from currentController: Controller) {
public func navigate(to location: Location, from currentController: CurrentController) {
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: 8 additions & 12 deletions Sources/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ 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 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
12 changes: 6 additions & 6 deletions Sources/TypeAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#endif

#if os(OSX)
public typealias Controller = NSViewController
public typealias CurrentController = NSViewController

func openURL(URL: NSURL) {
NSWorkspace.sharedWorkspace().openURL(URL)
func open(url: URL) {
NSWorkspace.shared().open(url)
}
#else
public typealias Controller = UIViewController
public typealias CurrentController = UIViewController

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

0 comments on commit d5833d6

Please sign in to comment.