Skip to content

Commit 7da4bb0

Browse files
committed
Merge pull request #15 from dmiedema/Swift2.0
Update for Swift 2.0
2 parents 55f26c0 + 8d7462d commit 7da4bb0

File tree

6 files changed

+79
-69
lines changed

6 files changed

+79
-69
lines changed

MapPointMapper.xcodeproj/project.pbxproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@
195195
547424DC1A1C1CE800A8478C /* Project object */ = {
196196
isa = PBXProject;
197197
attributes = {
198-
LastUpgradeCheck = 0610;
198+
LastSwiftUpdateCheck = 0700;
199+
LastUpgradeCheck = 0700;
199200
ORGANIZATIONNAME = dmiedema;
200201
TargetAttributes = {
201202
547424E31A1C1CE800A8478C = {
@@ -316,6 +317,7 @@
316317
COPY_PHASE_STRIP = NO;
317318
EMBEDDED_CONTENT_CONTAINS_SWIFT = NO;
318319
ENABLE_STRICT_OBJC_MSGSEND = YES;
320+
ENABLE_TESTABILITY = YES;
319321
GCC_C_LANGUAGE_STANDARD = gnu99;
320322
GCC_DYNAMIC_NO_PIC = NO;
321323
GCC_OPTIMIZATION_LEVEL = 0;
@@ -404,6 +406,7 @@
404406
INFOPLIST_FILE = MapPointMapper/Info.plist;
405407
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
406408
MACOSX_DEPLOYMENT_TARGET = 10.10;
409+
PRODUCT_BUNDLE_IDENTIFIER = "dmiedema.$(PRODUCT_NAME:rfc1034identifier)";
407410
PRODUCT_NAME = "$(TARGET_NAME)";
408411
PROVISIONING_PROFILE = "";
409412
RUN_CLANG_STATIC_ANALYZER = YES;
@@ -440,6 +443,7 @@
440443
INFOPLIST_FILE = MapPointMapper/Info.plist;
441444
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
442445
MACOSX_DEPLOYMENT_TARGET = 10.10;
446+
PRODUCT_BUNDLE_IDENTIFIER = "dmiedema.$(PRODUCT_NAME:rfc1034identifier)";
443447
PRODUCT_NAME = "$(TARGET_NAME)";
444448
PROVISIONING_PROFILE = "";
445449
RUN_CLANG_STATIC_ANALYZER = YES;
@@ -454,14 +458,14 @@
454458
FRAMEWORK_SEARCH_PATHS = (
455459
"$(DEVELOPER_FRAMEWORKS_DIR)",
456460
"$(inherited)",
457-
"$(PROJECT_DIR)/Carthage/Build/Mac",
458461
);
459462
GCC_PREPROCESSOR_DEFINITIONS = (
460463
"DEBUG=1",
461464
"$(inherited)",
462465
);
463466
INFOPLIST_FILE = MapPointMapperTests/Info.plist;
464467
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
468+
PRODUCT_BUNDLE_IDENTIFIER = "dmiedema.$(PRODUCT_NAME:rfc1034identifier)";
465469
PRODUCT_NAME = "$(TARGET_NAME)";
466470
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MapPointMapper.app/Contents/MacOS/MapPointMapper";
467471
};
@@ -475,10 +479,10 @@
475479
FRAMEWORK_SEARCH_PATHS = (
476480
"$(DEVELOPER_FRAMEWORKS_DIR)",
477481
"$(inherited)",
478-
"$(PROJECT_DIR)/Carthage/Build/Mac",
479482
);
480483
INFOPLIST_FILE = MapPointMapperTests/Info.plist;
481484
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
485+
PRODUCT_BUNDLE_IDENTIFIER = "dmiedema.$(PRODUCT_NAME:rfc1034identifier)";
482486
PRODUCT_NAME = "$(TARGET_NAME)";
483487
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MapPointMapper.app/Contents/MacOS/MapPointMapper";
484488
};

MapPointMapper/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>CFBundleIconFile</key>
1010
<string></string>
1111
<key>CFBundleIdentifier</key>
12-
<string>dmiedema.$(PRODUCT_NAME:rfc1034identifier)</string>
12+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1313
<key>CFBundleInfoDictionaryVersion</key>
1414
<string>6.0</string>
1515
<key>CFBundleName</key>

MapPointMapper/Parser/Parser.swift

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import MapKit
1212
extension NSString {
1313
/**
1414
Convenience so `isEmpty` can be performed on an `NSString` instance just as if it were a `String`
15+
16+
- returns: `true` if the string is empty, `false` if not
1517
*/
1618
var isEmpty: Bool {
1719
get { return self.length == 0 || self.isEqualToString("") }
@@ -21,7 +23,7 @@ extension String {
2123
/**
2224
Strip all leading and trailing whitespace from a given `String` instance.
2325

24-
:returns: a newly stripped string instance.
26+
- returns: a newly stripped string instance.
2527
*/
2628
func stringByStrippingLeadingAndTrailingWhiteSpace() -> String {
2729
let mutable = self.mutableCopy() as! NSMutableString
@@ -35,12 +37,12 @@ class Parser {
3537
/**
3638
Parse a given string of Lat/Lng values to return a collection of `CLLocationCoordinate2D` arrays.
3739

38-
:note: The preferred way/format of the input string is `Well-Known Text` as the parser supports that for multipolygons and such
40+
- note: The preferred way/format of the input string is `Well-Known Text` as the parser supports that for multipolygons and such
3941

40-
:param: input String to parse
41-
:param: longitudeFirst Only used if it is determined to not be `Well-Known Text` format.
42+
- parameter input: String to parse
43+
- parameter longitudeFirst: Only used if it is determined to not be `Well-Known Text` format.
4244

43-
:returns: An array of `CLLocationCoordinate2D` arrays representing the parsed areas/lines
45+
- returns: An array of `CLLocationCoordinate2D` arrays representing the parsed areas/lines
4446
*/
4547
class func parseString(input: NSString, longitudeFirst: Bool) -> [[CLLocationCoordinate2D]] {
4648
return Parser(longitudeFirst: longitudeFirst).parseInput(input)
@@ -60,23 +62,23 @@ class Parser {
6062
/**
6163
Parse input string into a collection of `CLLocationCoordinate2D` arrays that can be drawn on a map
6264

63-
:note: This method supports (and really works best with/prefers) `Well-Known Text` format
65+
- note: This method supports (and really works best with/prefers) `Well-Known Text` format
6466

65-
:param: input `NSString` to parse
67+
- parameter input: `NSString` to parse
6668

67-
:returns: Collection of `CLLocationCoordinate2D` arrays
69+
- returns: Collection of `CLLocationCoordinate2D` arrays
6870
*/
6971
internal func parseInput(input: NSString) -> [[CLLocationCoordinate2D]] {
7072
var array = [[NSString]]()
7173

72-
let line = input as! String
74+
let line = input as String
7375

7476
if isProbablyGeoString(line) {
7577
self.longitudeFirst = true
7678
var items = [NSString]()
7779

7880
if isMultiItem(line) {
79-
items = stripExtraneousCharacters(line).componentsSeparatedByString("),") as! [NSString]
81+
items = stripExtraneousCharacters(line).componentsSeparatedByString("),")
8082
} else {
8183
items = [stripExtraneousCharacters(line)]
8284
}
@@ -92,18 +94,19 @@ class Parser {
9294
/**
9395
Convert an array of strings into tuple pairs.
9496

95-
:note: the number of values passed in should probably be even, since it creates pairs.
97+
- note: the number of values passed in should probably be even, since it creates pairs.
9698

97-
:param: array of `[NSString]` array to create tuples from
99+
- parameter array: of `[NSString]` array to create tuples from
98100

99-
:returns: array of collections of tuple pairs where the tuples are lat/lng values as `NSString`s
101+
- returns: array of collections of tuple pairs where the tuples are lat/lng values as `NSString`s
100102
*/
101103
internal func convertStringArraysToTuples(array: [[NSString]]) -> [[(NSString, NSString)]] {
102104
var tmpResults = [(NSString, NSString)]()
103105
var results = [[(NSString, NSString)]]()
104106
for arr in array {
105107
for var i = 0; i < arr.count - 1; i += 2 {
106-
tmpResults.append((arr[i], arr[i + 1]))
108+
let elem = (arr[i], arr[i + 1])
109+
tmpResults.append(elem)
107110
}
108111

109112
if tmpResults.count == 1 {
@@ -117,13 +120,13 @@ class Parser {
117120
}
118121

119122
/**
120-
:abstract: Naively format a `Well-Known Text` string into array of string values, where each string is a single value
123+
_abstract_: Naively format a `Well-Known Text` string into array of string values, where each string is a single value
121124

122-
:discussion: This removes any lingering parens from the given string, breaks on `,` then breaks on ` ` while filtering out any empty strings.
125+
_discussion_: This removes any lingering parens from the given string, breaks on `,` then breaks on ` ` while filtering out any empty strings.
123126

124-
:param: input String to format, assumed `Well-Known Text` format
127+
- parameter input: String to format, assumed `Well-Known Text` format
125128

126-
:returns: array of strings where each string is one value from the string with all empty strings filtered out.
129+
- returns: array of strings where each string is one value from the string with all empty strings filtered out.
127130
*/
128131
internal func formatStandardGeoDataString(input: NSString) -> [NSString] {
129132
// Remove Extra ()
@@ -150,18 +153,18 @@ class Parser {
150153

151154
private func splitLine(input: NSString, delimiter: NSString) -> (NSString, NSString) {
152155
let array = input.componentsSeparatedByString(delimiter as String)
153-
return (array.first! as! NSString, array.last! as! NSString)
156+
return (array.first! as NSString, array.last! as NSString)
154157
}
155158

156159
/**
157160
:abstract: Convert a given array of `(String, String)` tuples to array of `CLLocationCoordinate2D` values
158161

159162
:discussion: This attempts to parse the string's double values but does no safety checks if they can be parsed as `double`s.
160163

161-
:param: pairs array of `String` tuples to parse as `Double`s
162-
:param: longitudeFirst boolean flag if the first item in the tuple should be the longitude value
164+
- parameter pairs: array of `String` tuples to parse as `Double`s
165+
- parameter longitudeFirst: boolean flag if the first item in the tuple should be the longitude value
163166

164-
:returns: array of `CLLocationCoordinate2D` values
167+
- returns: array of `CLLocationCoordinate2D` values
165168
*/
166169
internal func convertToCoordinates(pairs: [(NSString, NSString)], longitudeFirst: Bool) -> [CLLocationCoordinate2D] {
167170
var coordinates = [CLLocationCoordinate2D]()
@@ -184,18 +187,25 @@ class Parser {
184187
Removes any text before lat long points as well as two outer sets of parens.
185188

186189
Example:
190+
```
187191
input => "POLYGON(( 15 32 ))"
188192
output => "15 32"
189193

190194
input => "MULTIPOLYGON((( 15 32 )))"
191195
output => "( 15 32 )"
196+
```
197+
198+
- parameter input: NSString to strip extraneous characters from
192199

193-
:param: input NSString to strip extraneous characters from
194-
195-
:returns: stripped string instance
200+
- returns: stripped string instance
196201
*/
197202
internal func stripExtraneousCharacters(input: NSString) -> NSString {
198-
let regex = NSRegularExpression(pattern: "\\w+\\s*\\((.*)\\)", options: .CaseInsensitive, error: nil)
203+
let regex: NSRegularExpression?
204+
do {
205+
regex = try NSRegularExpression(pattern: "\\w+\\s*\\((.*)\\)", options: .CaseInsensitive)
206+
} catch _ {
207+
regex = nil
208+
}
199209
let match: AnyObject? = regex?.matchesInString(input as String, options: .ReportCompletion, range: NSMakeRange(0, input.length)).first
200210
let range = match?.rangeAtIndex(1)
201211

@@ -206,17 +216,17 @@ class Parser {
206216
}
207217

208218
/**
209-
:abstract: Attempt to determine if a given string is in `Well-Known Text` format (GeoString as its referred to internally)
219+
_abstract_: Attempt to determine if a given string is in `Well-Known Text` format (GeoString as its referred to internally)
210220

211-
:discussion: This strips any leading & trailing white space before checking for the existance of word characters at the start of the string.
221+
_discussion_: This strips any leading & trailing white space before checking for the existance of word characters at the start of the string.
212222

213-
:param: input String to attempt determine if is in `Well-Known Text` format
223+
- parameter input: String to attempt determine if is in `Well-Known Text` format
214224

215-
:returns: `true` if it thinks it is, `false` otherwise
225+
- returns: `true` if it thinks it is, `false` otherwise
216226
*/
217227
internal func isProbablyGeoString(input: String) -> Bool {
218228
let stripped = input.stringByStrippingLeadingAndTrailingWhiteSpace()
219-
if let geoString = stripped.rangeOfString("^\\w+", options: .RegularExpressionSearch) {
229+
if stripped.rangeOfString("^\\w+", options: .RegularExpressionSearch) != nil {
220230
return true
221231
}
222232
return false
@@ -225,12 +235,12 @@ class Parser {
225235
/**
226236
Determine if a given string is a `MULTI*` item.
227237

228-
:param: input String to check
238+
- parameter input: String to check
229239

230-
:returns: `true` if the string starts with `MULTI`. `false` otherwise
240+
- returns: `true` if the string starts with `MULTI`. `false` otherwise
231241
*/
232242
internal func isMultiItem(input: String) -> Bool {
233-
if let isPolygon = input.rangeOfString("MULTI", options: .RegularExpressionSearch) {
243+
if input.rangeOfString("MULTI", options: .RegularExpressionSearch) != nil {
234244
return true
235245
}
236246
return false
@@ -239,11 +249,11 @@ class Parser {
239249
/**
240250
Determines if a the collection is space delimited or not
241251

242-
:note: This function should only be passed a single entry or else it will probably have incorrect results
252+
- note: This function should only be passed a single entry or else it will probably have incorrect results
243253

244-
:param: input a single entry from the collection as a string
254+
- parameter input: a single entry from the collection as a string
245255

246-
:returns: `true` if elements are space delimited, `false` otherwise
256+
- returns: `true` if elements are space delimited, `false` otherwise
247257
*/
248258
private func isSpaceDelimited(input: String) -> Bool {
249259
let array = input.componentsSeparatedByString(" ")

MapPointMapper/ViewController.swift

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ class ViewController: NSViewController, MKMapViewDelegate, NSTextFieldDelegate {
9191
}
9292

9393
@IBAction func centerAllLinesPressed(sender: NSButton) {
94-
let polylines = mapview.overlays as! [MKOverlay]
94+
let polylines = mapview.overlays as [MKOverlay]
9595
let boundingMapRect = boundingMapRectForPolylines(polylines)
9696
mapview.setVisibleMapRect(boundingMapRect, edgePadding: NSEdgeInsets(top: 10, left: 10, bottom: 10, right: 10), animated: true)
9797
}
9898

9999
// MARK: MKMapDelegate
100-
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
100+
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
101101
let renderer = MKPolylineRenderer(overlay: overlay)
102102
renderer.alpha = 1.0
103103
renderer.lineWidth = 4.0
104104
renderer.strokeColor = colorWell.color
105105
return renderer
106106
}
107+
107108
// MARK: NSTextFieldDelegate
108109
override func keyUp(theEvent: NSEvent) {
109110
if theEvent.keyCode == 36 { // 36 is the return key apparently
@@ -116,9 +117,9 @@ class ViewController: NSViewController, MKMapViewDelegate, NSTextFieldDelegate {
116117
/**
117118
Create an `MKOverlay` for a given array of `CLLocationCoordinate2D` instances
118119

119-
:param: mapPoints array of `CLLocationCoordinate2D` instances to convert
120+
- parameter mapPoints: array of `CLLocationCoordinate2D` instances to convert
120121

121-
:returns: an MKOverlay created from array of `CLLocationCoordinate2D` instances
122+
- returns: an MKOverlay created from array of `CLLocationCoordinate2D` instances
122123
*/
123124
private func createPolylineForCoordinates(mapPoints: [CLLocationCoordinate2D]) -> MKOverlay {
124125
let coordinates = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(mapPoints.count)
@@ -139,11 +140,11 @@ class ViewController: NSViewController, MKMapViewDelegate, NSTextFieldDelegate {
139140
/**
140141
Get the bounding `MKMapRect` that contains all given `MKOverlay` objects
141142

142-
:warning: If no `MKOverlay` objects are included the resulting `MKMapRect` will be nonsensical and will results in a warning.
143+
- warning: If no `MKOverlay` objects are included the resulting `MKMapRect` will be nonsensical and will results in a warning.
143144

144-
:param: polylines array of `MKOverlay` objects.
145+
- parameter polylines: array of `MKOverlay` objects.
145146

146-
:returns: an `MKMapRect` that contains all the given `MKOverlay` objects
147+
- returns: an `MKMapRect` that contains all the given `MKOverlay` objects
147148
*/
148149
private func boundingMapRectForPolylines(polylines: [MKOverlay]) -> MKMapRect {
149150
var minX = Double.infinity
@@ -171,25 +172,20 @@ class ViewController: NSViewController, MKMapViewDelegate, NSTextFieldDelegate {
171172
/**
172173
Read a given file at a url
173174

174-
:param: passedURL `NSURL` to attempt to read
175+
- parameter passedURL: `NSURL` to attempt to read
175176
*/
176177
private func readFileAtURL(passedURL: NSURL?) {
177-
if let url = passedURL {
178-
if !NSFileManager.defaultManager().isReadableFileAtPath(url.absoluteString!) {
179-
NSAlert(error: NSError(domain: "com.dmiedema.MapPointMapper", code: -42, userInfo: [NSLocalizedDescriptionKey: "File is unreadable at \(url.absoluteString)"]))
180-
}
181-
182-
var error: NSError?
183-
let contents = NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding, error: &error)
184-
185-
if let err = error {
186-
NSAlert(error: err)
187-
return
188-
}
189-
190-
if let content = contents {
191-
renderInput(content)
192-
}
178+
guard let url = passedURL else { return }
179+
180+
if !NSFileManager.defaultManager().isReadableFileAtPath(url.absoluteString) {
181+
NSAlert(error: NSError(domain: "com.dmiedema.MapPointMapper", code: -42, userInfo: [NSLocalizedDescriptionKey: "File is unreadable at \(url.absoluteString)"])).runModal()
182+
}
183+
184+
do {
185+
let contents = try NSString(contentsOfURL: url, encoding: NSUTF8StringEncoding) as String
186+
renderInput(contents)
187+
} catch {
188+
NSAlert(error: error as NSError).runModal()
193189
}
194190
} // end readFileAtURL
195191

@@ -205,7 +201,7 @@ class ViewController: NSViewController, MKMapViewDelegate, NSTextFieldDelegate {
205201
/**
206202
Parse the given input.
207203

208-
:param: input `NSString` to parse and draw on the map. If no string is given this is essentially a noop
204+
- parameter input: `NSString` to parse and draw on the map. If no string is given this is essentially a noop
209205
*/
210206
private func parseInput(input: NSString) {
211207

MapPointMapperTests/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>dmiedema.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

0 commit comments

Comments
 (0)