-
Notifications
You must be signed in to change notification settings - Fork 1
/
Plist.swift
334 lines (300 loc) · 10.5 KB
/
Plist.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// Plist.swift
// PlistParsing
//
// Created by Vincent Bernier on 21-08-2015.
// Inspired by https://github.com/monyschuk/PropertyList
// Copyright (c) 2015 Vincent Bernier. Use it as you see fit. No guarantee of support.
//
import Foundation
import Swift
/** Goal : Represent the **node** and **leaf** of a Plist tree */
public struct Plist {
fileprivate var entityType : EntityType
fileprivate enum EntityType {
case string(String)
case number(NSNumber)
case date(Date)
case data(Data)
case array([Plist])
case dictionary([String : Plist])
}
//MARK:- Entity Creation
/** Create a Plist from values that are Plist Convertible or Plist.
- parameter plistObject: Accepted input are Plist compatible classes and Plist structs
- note: this is similar to init?(rawValue), but will also allow Plist, [Plist] and [String : Plist] as input
-->(NOTE TO SELF: is this necessary or usefull... Well... It is when you need to add or remove entity from a .Array)
- warning: Will crash if not convertible
*/
public init(plistObject: Any) {
switch plistObject {
case let string as String:
self.entityType = .string(string)
case let num as NSNumber:
self.entityType = .number(num)
case let date as Date:
self.entityType = .date(date)
case let data as Data:
self.entityType = .data(data)
case let plist as Plist:
self.entityType = plist.entityType
case let array as NSArray:
var pAr : [Plist] = []
for any in array {
let p = Plist(plistObject: any)
pAr.append(p)
}
self.entityType = .array(pAr)
case let array as [Plist]:
self.entityType = .array(array)
case let dictionary as [String : Plist]:
self.entityType = .dictionary(dictionary)
case let dictionary as NSDictionary:
var dic : [String : Plist] = [:]
for (k, value) in dictionary {
if let key = k as? String {
let p = Plist(plistObject: value)
dic[key] = p
} else {
assertionFailure("Key of dictionary must be String")
}
}
self.entityType = .dictionary(dic)
default:
print("value \(plistObject) is not a valid property list type", terminator: "\n")
self.entityType = .string("")
assertionFailure("Use only property list complient class.")
}
}
}
//MARK:- RawRepresentable protocol
/** Convert from and to Cocoa type.
Use to convert from and to Swift and Foundation Plist type (String, NSNumber, NSDate, NSData, NSArray, NSDictionary (Dictionary key must be String)) to a Plist tree. */
extension Plist : RawRepresentable {
/** Designited Creation Method
:discussion: Take an input of Swift and Foundation Plist type (String, NSNumber, NSDate, NSData, NSArray, NSDictionary (Dictionary key must be String)) and output a Plist tree.
*/
public init?(rawValue: Any) {
switch rawValue {
case let string as String:
self.entityType = .string(string)
case let num as NSNumber:
self.entityType = .number(num)
case let date as Date:
self.entityType = .date(date)
case let data as Data:
self.entityType = .data(data)
case let array as NSArray:
var pAr : [Plist] = []
for any in array {
if let p = Plist(rawValue: any) {
pAr.append(p)
} else {
return nil
}
}
self.entityType = .array(pAr)
case let dictionary as NSDictionary:
var dic : [String : Plist] = [:]
for (k, value) in dictionary {
if let key = k as? String, let p = Plist(rawValue: value) {
dic[key] = p
} else {
return nil
}
}
self.entityType = .dictionary(dic)
default:
return nil
}
}
/** Convenience method to unwrap the result of init?(rawValue:)
- warning: Method will crash if an error occure */
public static func newWithRawValue(_ rawValue: Any) -> Plist {
return Plist(rawValue: rawValue)!
}
/** Convert back a Plist to it's Raw representation */
public var rawValue: Any {
//NOTE --> Consider moving rawValue to AnyObject or to a PlistConvertibleProtocol, for which the possible class need to have extension
get {
switch entityType {
case .string(let value):
return value
case .number(let value):
return value
case .date(let value) :
return value
case .data(let value):
return value
case .array(let value):
var output = [Any]()
for p in value {
output.append(p.rawValue)
}
return output
case .dictionary(let value):
var output = [String : Any]()
for (key, val) in value {
output[key] = val.rawValue
}
return output
}
}
}
}
//MARK:- Accessing Entity Values
/** Provide readonly access to **leaf** and **node** value */
public extension Plist {
/** - return: a String if Plist **leaf** is a String, nil otherwise */
public var string : String? {
get {
switch entityType {
case let .string(value):
return value
default:
return nil;
}
}
}
/** - return: a NSNumber if Plist **leaf** is a number (Bool, Float, Int), nil otherwise */
public var number : NSNumber? {
get {
switch entityType {
case let .number(value):
return value
default:
return nil;
}
}
}
//TODO: Add convinience to get Int, Float and Bool
/** - return: a NSDate if Plist **leaf** is a Date, nil otherwise */
public var date : Date? {
get {
switch entityType {
case let .date(value):
return value
default:
return nil;
}
}
}
/** - return: a NSData if Plist **leaf** is a Data, nil otherwise */
public var data : Data? {
get {
switch entityType {
case let .data(value):
return value
default:
return nil;
}
}
}
/** - return: a [Plist] if Plist **node** is an array, nil otherwise */
public var array : [Plist]? {
get {
switch entityType {
case let .array(value):
return value
default:
return nil;
}
}
}
/** - return: a [String:Plist] if Plist **node** is a Dictionary, nil otherwise */
public var dictionary : [String : Plist]? {
get {
switch entityType {
case let .dictionary(value):
return value
default:
return nil;
}
}
}
}
//MARK:- Subscripting Array
/** Array subscript to get and set Plist at index */
public extension Plist {
/** Array subscript
- warning: Numerical subscript on a Plist that is not an array **node** will result in a crash */
subscript(index: Int) -> Plist {
get {
return self.array![index]
}
set(newValue) {
var ar = self.array!
ar[index] = newValue
entityType = .array(ar)
}
}
}
//MARK:- Subsripting Dictionary
/** Dictionary sucscript to get and set Plist at key */
public extension Plist {
/** Dictionary subscript
- warning: String subscript on a Plist that is not a dictionary **node** will result in a crash */
subscript(key: String) -> Plist? {
get {
return self.dictionary![key]
}
set(newValue) {
var dic = self.dictionary!
dic[key] = newValue
entityType = .dictionary(dic)
}
}
}
//MARK:- Equatable
/** Conformance to the Equatable protocol */
extension Plist : Equatable {
/** Equality is based on the stored value for the Plist element
- warning: Float equality remains Float equality
*/
public static func ==(lhs: Plist, rhs: Plist) -> Bool {
let tuple = (lhs.entityType, rhs.entityType)
switch tuple {
case let (.string(left), .string(right)):
return left == right
case let (.number(left), .number(right)):
return left.isEqual(to: right)
case let (.date(left), .date(right)):
return (left == right)
case let (.data(left), .data(right)):
return (left == right)
case let (.array(left), .array(right)):
return left == right
case let (.dictionary(left), .dictionary(right)):
return left == right
default:
return false
}
}
}
//MARK:- 'for in' support for Plist Dictionary and Array
/** Support for Plist use in **for in** loop */
extension Plist: Swift.Sequence {
/** Enable 'for in' support by providing a Generator over SequenceType Plist (Array, and Dictionary)
- note: Calling this method on a non-SequenceType will make 0 iteration in a For in loop */
public func makeIterator() -> AnyIterator<(Swift.String, Plist)> {
switch entityType {
case .array(let value):
var index = 0
var generator = value.makeIterator()
return AnyIterator {
if let next = generator.next() {
let str = "\(index)"
index += 1
return (str, next)
} else {
return nil
}
}
case .dictionary(let value):
var generator = value.makeIterator()
return AnyIterator { return generator.next() }
default:
return AnyIterator { return nil }
}
}
}