Skip to content

Commit 578df1d

Browse files
committed
updating for Swift 2.0
1 parent bfbafc4 commit 578df1d

File tree

7 files changed

+67
-53
lines changed

7 files changed

+67
-53
lines changed

Geocoder Example.xcodeproj/project.pbxproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@
260260
DD342B4819A140EE00219F77 /* Project object */ = {
261261
isa = PBXProject;
262262
attributes = {
263-
LastUpgradeCheck = 0600;
263+
LastSwiftMigration = 0700;
264+
LastSwiftUpdateCheck = 0700;
265+
LastUpgradeCheck = 0700;
264266
ORGANIZATIONNAME = Mapbox;
265267
TargetAttributes = {
266268
DD342B4F19A140EE00219F77 = {
@@ -382,6 +384,7 @@
382384
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
383385
COPY_PHASE_STRIP = NO;
384386
ENABLE_STRICT_OBJC_MSGSEND = YES;
387+
ENABLE_TESTABILITY = YES;
385388
GCC_C_LANGUAGE_STANDARD = gnu99;
386389
GCC_DYNAMIC_NO_PIC = NO;
387390
GCC_OPTIMIZATION_LEVEL = 0;
@@ -448,6 +451,7 @@
448451
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
449452
INFOPLIST_FILE = "Geocoder Example/Info.plist";
450453
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
454+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
451455
PRODUCT_NAME = "Geocoder (Swift)";
452456
};
453457
name = Debug;
@@ -459,6 +463,7 @@
459463
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
460464
INFOPLIST_FILE = "Geocoder Example/Info.plist";
461465
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
466+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
462467
PRODUCT_NAME = "Geocoder (Swift)";
463468
};
464469
name = Release;
@@ -478,6 +483,7 @@
478483
INFOPLIST_FILE = MBGeocoder/Info.plist;
479484
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
480485
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
486+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
481487
PRODUCT_NAME = "$(TARGET_NAME)";
482488
SKIP_INSTALL = YES;
483489
VERSIONING_SYSTEM = "apple-generic";
@@ -496,6 +502,7 @@
496502
INFOPLIST_FILE = MBGeocoder/Info.plist;
497503
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
498504
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
505+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
499506
PRODUCT_NAME = "$(TARGET_NAME)";
500507
SKIP_INSTALL = YES;
501508
VERSIONING_SYSTEM = "apple-generic";
@@ -514,6 +521,7 @@
514521
);
515522
INFOPLIST_FILE = "Geocoder Example/Info.plist";
516523
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
524+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
517525
PRODUCT_NAME = "Geocoder (Objective-C)";
518526
};
519527
name = Debug;
@@ -525,6 +533,7 @@
525533
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
526534
INFOPLIST_FILE = "Geocoder Example/Info.plist";
527535
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
536+
PRODUCT_BUNDLE_IDENTIFIER = "com.mapbox.$(PRODUCT_NAME:rfc1034identifier)";
528537
PRODUCT_NAME = "Geocoder (Objective-C)";
529538
};
530539
name = Release;

Geocoder Example.xcodeproj/xcshareddata/xcschemes/Example (Objective-C).xcscheme

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0600"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -23,10 +23,10 @@
2323
</BuildActionEntries>
2424
</BuildAction>
2525
<TestAction
26+
buildConfiguration = "Debug"
2627
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2728
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
28-
shouldUseLaunchSchemeArgsEnv = "YES"
29-
buildConfiguration = "Debug">
29+
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
3131
</Testables>
3232
<MacroExpansion>
@@ -38,17 +38,21 @@
3838
ReferencedContainer = "container:Geocoder Example.xcodeproj">
3939
</BuildableReference>
4040
</MacroExpansion>
41+
<AdditionalOptions>
42+
</AdditionalOptions>
4143
</TestAction>
4244
<LaunchAction
45+
buildConfiguration = "Debug"
4346
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4447
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4548
launchStyle = "0"
4649
useCustomWorkingDirectory = "NO"
47-
buildConfiguration = "Debug"
4850
ignoresPersistentStateOnLaunch = "NO"
4951
debugDocumentVersioning = "YES"
52+
debugServiceExtension = "internal"
5053
allowLocationSimulation = "YES">
51-
<BuildableProductRunnable>
54+
<BuildableProductRunnable
55+
runnableDebuggingMode = "0">
5256
<BuildableReference
5357
BuildableIdentifier = "primary"
5458
BlueprintIdentifier = "DDC2472919A1C60E0054B0C0"
@@ -61,12 +65,13 @@
6165
</AdditionalOptions>
6266
</LaunchAction>
6367
<ProfileAction
68+
buildConfiguration = "Release"
6469
shouldUseLaunchSchemeArgsEnv = "YES"
6570
savedToolIdentifier = ""
6671
useCustomWorkingDirectory = "NO"
67-
buildConfiguration = "Release"
6872
debugDocumentVersioning = "YES">
69-
<BuildableProductRunnable>
73+
<BuildableProductRunnable
74+
runnableDebuggingMode = "0">
7075
<BuildableReference
7176
BuildableIdentifier = "primary"
7277
BlueprintIdentifier = "DDC2472919A1C60E0054B0C0"

Geocoder Example.xcodeproj/xcshareddata/xcschemes/Example (Swift).xcscheme

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0600"
3+
LastUpgradeVersion = "0700"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"
@@ -23,10 +23,10 @@
2323
</BuildActionEntries>
2424
</BuildAction>
2525
<TestAction
26+
buildConfiguration = "Debug"
2627
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2728
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
28-
shouldUseLaunchSchemeArgsEnv = "YES"
29-
buildConfiguration = "Debug">
29+
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
3131
</Testables>
3232
<MacroExpansion>
@@ -38,17 +38,21 @@
3838
ReferencedContainer = "container:Geocoder Example.xcodeproj">
3939
</BuildableReference>
4040
</MacroExpansion>
41+
<AdditionalOptions>
42+
</AdditionalOptions>
4143
</TestAction>
4244
<LaunchAction
45+
buildConfiguration = "Debug"
4346
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
4447
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
4548
launchStyle = "0"
4649
useCustomWorkingDirectory = "NO"
47-
buildConfiguration = "Debug"
4850
ignoresPersistentStateOnLaunch = "NO"
4951
debugDocumentVersioning = "YES"
52+
debugServiceExtension = "internal"
5053
allowLocationSimulation = "YES">
51-
<BuildableProductRunnable>
54+
<BuildableProductRunnable
55+
runnableDebuggingMode = "0">
5256
<BuildableReference
5357
BuildableIdentifier = "primary"
5458
BlueprintIdentifier = "DD342B4F19A140EE00219F77"
@@ -61,12 +65,13 @@
6165
</AdditionalOptions>
6266
</LaunchAction>
6367
<ProfileAction
68+
buildConfiguration = "Release"
6469
shouldUseLaunchSchemeArgsEnv = "YES"
6570
savedToolIdentifier = ""
6671
useCustomWorkingDirectory = "NO"
67-
buildConfiguration = "Release"
6872
debugDocumentVersioning = "YES">
69-
<BuildableProductRunnable>
73+
<BuildableProductRunnable
74+
runnableDebuggingMode = "0">
7075
<BuildableReference
7176
BuildableIdentifier = "primary"
7277
BlueprintIdentifier = "DD342B4F19A140EE00219F77"

Geocoder Example/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>com.mapbox.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

Geocoder Example/ViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ViewController: UIViewController, MKMapViewDelegate {
2222
super.viewDidLoad()
2323

2424
mapView = MKMapView(frame: view.bounds)
25-
mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
25+
mapView.autoresizingMask = [ .FlexibleWidth, .FlexibleHeight ]
2626
mapView.delegate = self
2727
view.addSubview(mapView)
2828

@@ -48,11 +48,11 @@ class ViewController: UIViewController, MKMapViewDelegate {
4848
geocoder.cancelGeocode()
4949
geocoder.reverseGeocodeLocation(CLLocation(latitude: mapView.centerCoordinate.latitude,
5050
longitude: mapView.centerCoordinate.longitude)) { [unowned self] (results, error) in
51-
if (error != nil) {
51+
if let error = error {
5252
NSLog("%@", error)
53-
} else if results.count > 0 {
53+
} else if let results = results where results.count > 0 {
5454
// self.resultsLabel.text = (results[0] as CLPlacemark).name
55-
self.resultsLabel.text = (results[0] as! MBPlacemark).name
55+
self.resultsLabel.text = (results[0] as MBPlacemark).name
5656
} else {
5757
self.resultsLabel.text = "No results"
5858
}

MBGeocoder/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>com.mapbox.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

MBGeocoder/MapboxGeocoder.swift

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Foundation
22
import CoreLocation
33

4-
public typealias MBGeocodeCompletionHandler = CLGeocodeCompletionHandler
4+
// like CLGeocodeCompletionHandler
5+
public typealias MBGeocodeCompletionHandler = ([MBPlacemark]?, NSError?) -> Void
56

67
// MARK: - Geocoder
78

@@ -37,7 +38,7 @@ public class MBGeocoder: NSObject,
3738
}
3839

3940
public func reverseGeocodeLocation(location: CLLocation, completionHandler: MBGeocodeCompletionHandler) {
40-
if (!self.geocoding) {
41+
if !self.geocoding {
4142
self.completionHandler = completionHandler
4243
let requestString = "https://api.mapbox.com/v4/geocode/mapbox.places/" +
4344
"\(location.coordinate.longitude),\(location.coordinate.latitude).json" +
@@ -51,10 +52,10 @@ public class MBGeocoder: NSObject,
5152
// completionHandler: MBGeocodeCompletionHandler)
5253

5354
public func geocodeAddressString(addressString: String, proximity: CLLocationCoordinate2D? = nil, completionHandler: MBGeocodeCompletionHandler) {
54-
if (!self.geocoding) {
55+
if !self.geocoding {
5556
self.completionHandler = completionHandler
5657
var requestString = "https://api.mapbox.com/v4/geocode/mapbox.places/" +
57-
addressString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! +
58+
addressString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())! +
5859
".json?access_token=\(accessToken)"
5960
if let proximityCoordinate = proximity {
6061
requestString += "&proximity=\(proximityCoordinate.longitude),\(proximityCoordinate.latitude)"
@@ -82,7 +83,7 @@ public class MBGeocoder: NSObject,
8283

8384
public func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
8485
let statusCode = (response as! NSHTTPURLResponse).statusCode
85-
if (statusCode != 200) {
86+
if statusCode != 200 {
8687
self.connection?.cancel()
8788
self.connection = nil
8889
self.completionHandler?(nil, NSError(domain: MBGeocoderErrorDomain,
@@ -98,24 +99,25 @@ public class MBGeocoder: NSObject,
9899
}
99100

100101
public func connectionDidFinishLoading(connection: NSURLConnection) {
101-
var parseError: NSError?
102-
let response = NSJSONSerialization.JSONObjectWithData(self.receivedData!, options: nil, error: &parseError) as? NSDictionary
103-
if (parseError != nil) {
104-
self.completionHandler?(nil, NSError(domain: MBGeocoderErrorDomain,
105-
code: MBGeocoderErrorCode.ParseError.rawValue,
106-
userInfo: [ NSLocalizedDescriptionKey: "Unable to parse results" ]))
107-
} else if let features = response?["features"] as? NSArray {
108-
var results: [MBPlacemark] = []
109-
for feature in features {
110-
if let feature = feature as? NSDictionary {
111-
if let placemark = MBPlacemark(featureJSON: feature) {
102+
var response: NSDictionary?
103+
do {
104+
response = try NSJSONSerialization.JSONObjectWithData(self.receivedData!, options: []) as? NSDictionary
105+
if let features = response?["features"] as? NSArray {
106+
var results: [MBPlacemark] = []
107+
for feature in features {
108+
if let feature = feature as? NSDictionary,
109+
let placemark = MBPlacemark(featureJSON: feature) {
112110
results.append(placemark)
113111
}
114112
}
113+
self.completionHandler?(results, nil)
114+
} else {
115+
self.completionHandler?([], nil)
115116
}
116-
self.completionHandler?(results, nil)
117-
} else {
118-
self.completionHandler?([], nil)
117+
} catch {
118+
self.completionHandler?(nil, NSError(domain: MBGeocoderErrorDomain,
119+
code: MBGeocoderErrorCode.ParseError.rawValue,
120+
userInfo: [ NSLocalizedDescriptionKey: "Unable to parse results" ]))
119121
}
120122
}
121123

@@ -129,8 +131,8 @@ public class MBPlacemark: NSObject, NSCopying, NSSecureCoding {
129131

130132
private var featureJSON: NSDictionary?
131133

132-
required public init(coder aDecoder: NSCoder) {
133-
featureJSON = aDecoder.decodeObjectOfClass(NSDictionary.self, forKey: "featureJSON") as? NSDictionary
134+
required public init?(coder aDecoder: NSCoder) {
135+
featureJSON = aDecoder.decodeObjectOfClass(NSDictionary.self, forKey: "featureJSON") as NSDictionary!
134136
}
135137

136138
public override init() {
@@ -143,17 +145,10 @@ public class MBPlacemark: NSObject, NSCopying, NSSecureCoding {
143145
}
144146

145147
internal convenience init?(featureJSON: NSDictionary) {
146-
var valid = false
147-
if let geometry = featureJSON["geometry"] as? NSDictionary {
148-
if geometry["type"] as? String == "Point" {
149-
if geometry["coordinates"] as? NSArray != nil {
150-
if featureJSON["place_name"] as? String != nil {
151-
valid = true
152-
}
153-
}
154-
}
155-
}
156-
if (valid) {
148+
if let geometry = featureJSON["geometry"] as? NSDictionary,
149+
type = geometry["type"] as? String where type == "Point",
150+
let _ = geometry["coordinates"] as? NSArray,
151+
let _ = featureJSON["place_name"] as? String {
157152
self.init()
158153
self.featureJSON = featureJSON
159154
} else {

0 commit comments

Comments
 (0)