11import Foundation
22import 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