Skip to content

Commit af6244c

Browse files
authored
Merge pull request #22 from vincentneo/initOptions
Init replacements & additions
2 parents 27fc6fa + f1bad44 commit af6244c

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

Classes/GPXParser.swift

+49-23
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,70 @@ import Foundation
99

1010
open class GPXParser: NSObject, XMLParserDelegate {
1111

12-
private var parser: XMLParser
12+
private let parser: XMLParser
1313

1414
// MARK:- Initializers
1515

16+
/// for parsing with `Data` type
17+
///
18+
/// - Parameters:
19+
/// - data: The input must be `Data` object containing GPX markup data, and should not be `nil`
20+
///
1621
public init(withData data: Data) {
1722
self.parser = XMLParser(data: data)
1823
super.init()
1924
self.parser.delegate = self
2025
self.parser.parse()
2126
}
2227

23-
public init(withPath path: String) {
24-
self.parser = XMLParser()
28+
/// for parsing with `InputStream` type
29+
///
30+
/// - Parameters:
31+
/// - stream: The input must be a input stream allowing GPX markup data to be parsed synchronously
32+
///
33+
public init(withStream stream: InputStream) {
34+
self.parser = XMLParser(stream: stream)
2535
super.init()
26-
let url = URL(fileURLWithPath: path)
27-
do {
28-
let data = try Data(contentsOf: url)
29-
self.parser = XMLParser(data: data)
30-
self.parser.delegate = self
31-
self.parser.parse()
32-
}
33-
catch {
34-
print(error)
35-
}
36+
self.parser.delegate = self
37+
self.parser.parse()
3638
}
3739

38-
public init(withURL url: URL) {
39-
self.parser = XMLParser()
40+
/// for parsing with `URL` type
41+
///
42+
/// - Parameters:
43+
/// - url: The input must be a `URL`, which should point to a GPX file located at the URL given
44+
///
45+
public init?(withURL url: URL) {
46+
guard let urlParser = XMLParser(contentsOf: url) else { return nil }
47+
self.parser = urlParser
4048
super.init()
41-
do {
42-
let data = try Data(contentsOf: url)
43-
self.parser = XMLParser(data: data)
44-
self.parser.delegate = self
45-
self.parser.parse()
46-
}
47-
catch {
48-
print(error)
49+
self.parser.delegate = self
50+
self.parser.parse()
51+
}
52+
53+
/// for parsing with a string that contains full GPX markup
54+
///
55+
/// - Parameters:
56+
/// - string: The input `String` must contain full GPX markup, which is typically contained in a `.GPX` file
57+
///
58+
public convenience init?(withRawString string: String?) {
59+
if let string = string {
60+
if let data = string.data(using: .utf8) {
61+
self.init(withData: data)
62+
}
63+
else { return nil }
4964
}
65+
else { return nil }
66+
}
67+
68+
/// for parsing with a path to a GPX file
69+
///
70+
/// - Parameters:
71+
/// - path: The input path, with type `String`, must contain a path that points to a GPX file used to facilitate parsing.
72+
///
73+
public convenience init?(withPath path: String) {
74+
guard let url = URL(string: path) else { return nil }
75+
self.init(withURL: url)
5076
}
5177

5278
// MARK:- GPX Parsing

Example/GPXKit/ParseViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ParseViewController: UIViewController, UITableViewDelegate, UITableViewDat
3232

3333
if input != nil {
3434
if let inputURL = URL(string: input!) {
35-
let gpx = GPXParser(withURL: inputURL).parsedData()
35+
guard let gpx = GPXParser(withURL: inputURL)?.parsedData() else { return }
3636
self.tracks = gpx.tracks
3737
self.waypoints = gpx.waypoints
3838
self.tableView.reloadData()

Example/GPXKit/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ViewController: UIViewController {
2323
let url: URL = URL(string: urlString)!
2424

2525
// GPXRoot object that contains all the data parsed from GPXParser.
26-
let gpx = GPXParser(withURL: url).parsedData()
26+
guard let gpx = GPXParser(withURL: url)?.parsedData() else { return }
2727

2828
self.tracks = gpx.tracks
2929
self.waypoints = gpx.waypoints

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ It makes use of `XMLParser` for parsing GPX files, thus making it fully dependen
2020
## How to parse?
2121
Parsing of GPX files is done by initializing `GPXParser`.
2222

23-
There are three ways of initializing `GPXParser`:
23+
There are five ways of initializing `GPXParser`, and these are three main ways of initializing:
2424
### You can initialize with a `URL`:
2525
```Swift
26-
let gpx = GPXParser(withURL: inputURL).parsedData()
26+
guard let gpx = GPXParser(withURL: inputURL)?.parsedData() else { return }
2727
```
2828
### With path:
2929
```Swift
30-
let gpx = GPXParser(withPath: inputPath).parsedData() // String type
30+
guard let gpx = GPXParser(withPath: inputPath)?.parsedData() else { return } // String type
3131
```
32-
### Or with `Data`:
32+
### With `Data`:
3333
```Swift
3434
let gpx = GPXParser(withData: inputData).parsedData()
3535
```
@@ -38,7 +38,7 @@ let gpx = GPXParser(withData: inputData).parsedData()
3838

3939
### Making use of parsed GPX data
4040
```Swift
41-
let gpx = GPXParser(withURL: inputURL).parsedData()
41+
guard let gpx = GPXParser(withURL: inputURL)?.parsedData() else { return // do things here when failed }
4242

4343
// waypoints, tracks, tracksegements, trackpoints are all stored as Array depends on the amount stored in the GPX file.
4444
for waypoint in gpx.waypoints { // for loop example, every waypoint is written
@@ -54,7 +54,7 @@ for waypoint in gpx.waypoints { // for loop example, every waypoint is written
5454

5555
## How to create?
5656

57-
You will first start of with `GPXRoot`.
57+
You will first start off with a `GPXRoot`.
5858

5959
### Initializing `GPXRoot`
6060
```Swift

0 commit comments

Comments
 (0)