@@ -9,44 +9,70 @@ import Foundation
9
9
10
10
open class GPXParser : NSObject , XMLParserDelegate {
11
11
12
- private var parser : XMLParser
12
+ private let parser : XMLParser
13
13
14
14
// MARK:- Initializers
15
15
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
+ ///
16
21
public init ( withData data: Data ) {
17
22
self . parser = XMLParser ( data: data)
18
23
super. init ( )
19
24
self . parser. delegate = self
20
25
self . parser. parse ( )
21
26
}
22
27
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)
25
35
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 ( )
36
38
}
37
39
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
40
48
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 }
49
64
}
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)
50
76
}
51
77
52
78
// MARK:- GPX Parsing
0 commit comments