0001    // PropertyArray.swift
0002    //
0003    // Copyright (c) 2015 Oven Bits, LLC
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in all
0013    // copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0021    // SOFTWARE.
0022    
0023    import Foundation
0024    
0025    final public class PropertyArray
PropertyArray.swift:100
extension PropertyArray: CustomStringConvertible {
PropertyArray.swift:108
extension PropertyArray: CustomDebugStringConvertible {
PropertyArray.swift:116
extension PropertyArray: CollectionType {
PropertyArray.swift:136
extension PropertyArray: Hashable {
PropertyArray.swift:144
extension PropertyArray: Equatable {}
PropertyArray.swift:146
public func ==<T: Equatable>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool {
PropertyArray.swift:146
public func ==<T: Equatable>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool {
PropertyArray.swift:150
public func ==<T>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool {
PropertyArray.swift:150
public func ==<T>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool {
<T
PropertyArray.swift:26
    public typealias PropertyType = T
: JSONTransformable>: PropertyDescription { 0026 public typealias PropertyType
PropertyArray.swift:29
    public var values: [PropertyType] = []
PropertyArray.swift:32
    public var postProcess: (([PropertyType]) -> Void)?
PropertyArray.swift:39
        return "\(PropertyType.self)"
PropertyArray.swift:45
    public var last: PropertyType? {
PropertyArray.swift:52
    public init(key: String, defaultValues: [PropertyType] = [], required: Bool = false, postProcess: (([PropertyType]) -> Void)? = nil) {
PropertyArray.swift:52
    public init(key: String, defaultValues: [PropertyType] = [], required: Bool = false, postProcess: (([PropertyType]) -> Void)? = nil) {
PropertyArray.swift:69
        values = jsonValue.flatMap { PropertyType.fromJSON($0) as? PropertyType }
PropertyArray.swift:69
        values = jsonValue.flatMap { PropertyType.fromJSON($0) as? PropertyType }
PropertyArray.swift:94
        values = decodedObjects.flatMap { $0 as? PropertyType }
PropertyArray.swift:117
    public func generate() -> IndexingGenerator<[PropertyType]> {
= T 0027 0028 /// Backing store for property data 0029 public var values
PropertyArray.swift:46
        return values.last
PropertyArray.swift:54
        self.values = defaultValues
PropertyArray.swift:69
        values = jsonValue.flatMap { PropertyType.fromJSON($0) as? PropertyType }
PropertyArray.swift:71
        return !values.isEmpty
PropertyArray.swift:76
        return values.map { $0.toJSON() }
PropertyArray.swift:81
        postProcess?(values)
PropertyArray.swift:88
        let objectArray = values.flatMap { $0 as? AnyObject }
PropertyArray.swift:94
        values = decodedObjects.flatMap { $0 as? PropertyType }
PropertyArray.swift:102
        return "PropertyArray<\(type)> (key: \(key), count: \(values.count), required: \(required))"
PropertyArray.swift:118
        return values.generate()
PropertyArray.swift:126
        return values.count
PropertyArray.swift:130
        return values[index]
PropertyArray.swift:147
    return lhs.key == rhs.key && lhs.values == rhs.values
PropertyArray.swift:147
    return lhs.key == rhs.key && lhs.values == rhs.values
: [PropertyType] = [] 0030 0031 /// Post-processing closure. 0032 public var postProcess
PropertyArray.swift:56
        self.postProcess = postProcess
PropertyArray.swift:81
        postProcess?(values)
: (([PropertyType]) -> Void)? 0033 0034 /// JSON parameter key 0035 public var key
PropertyArray.swift:53
        self.key = key
PropertyArray.swift:65
        key.componentsSeparatedByString(".").forEach {
PropertyArray.swift:89
        coder.encodeObject(objectArray, forKey: key)
PropertyArray.swift:93
        let decodedObjects = decoder.decodeObjectForKey(key) as? [AnyObject] ?? []
PropertyArray.swift:102
        return "PropertyArray<\(type)> (key: \(key), count: \(values.count), required: \(required))"
PropertyArray.swift:138
        return key.hashValue
PropertyArray.swift:147
    return lhs.key == rhs.key && lhs.values == rhs.values
PropertyArray.swift:147
    return lhs.key == rhs.key && lhs.values == rhs.values
PropertyArray.swift:151
    return lhs.key == rhs.key
PropertyArray.swift:151
    return lhs.key == rhs.key
: String 0036 0037 /// Type information 0038 public var type
PropertyArray.swift:102
        return "PropertyArray<\(type)> (key: \(key), count: \(values.count), required: \(required))"
: String { 0039 return "\(PropertyType.self)" 0040 } 0041 0042 /// Specify whether value is required 0043 public var required
PropertyArray.swift:55
        self.required = required
PropertyArray.swift:102
        return "PropertyArray<\(type)> (key: \(key), count: \(values.count), required: \(required))"
= false 0044 0045 public var last: PropertyType? { 0046 return values.last 0047 } 0048 0049 // MARK: Initialization 0050 0051 /// Initialize with JSON property key 0052 public init(key: String, defaultValues: [PropertyType] = [], required: Bool = false, postProcess: (([PropertyType]) -> Void)? = nil) { 0053 self.key = key 0054 self.values = defaultValues 0055 self.required = required 0056 self.postProcess = postProcess 0057 } 0058 0059 // MARK: Transform 0060 0061 /// Extract object from JSON and return whether or not the value was extracted 0062 public func fromJSON(json: JSON) -> Bool { 0063 var jsonValue: JSON = json 0064 0065 key.componentsSeparatedByString(".").forEach { 0066 jsonValue = jsonValue[$0] 0067 } 0068 0069 values = jsonValue.flatMap { PropertyType.fromJSON($0) as? PropertyType } 0070 0071 return !values.isEmpty 0072 } 0073 0074 /// Convert object to JSON 0075 public func toJSON() -> AnyObject? { 0076 return values.map { $0.toJSON() } 0077 } 0078 0079 /// Perform initialization post-processing 0080 public func initPostProcess() { 0081 postProcess?(values) 0082 } 0083 0084 // MARK: Coding 0085 0086 /// Encode 0087 public func encode(coder: NSCoder) { 0088 let objectArray = values.flatMap { $0 as? AnyObject } 0089 coder.encodeObject(objectArray, forKey: key) 0090 } 0091 0092 public func decode(decoder: NSCoder) { 0093 let decodedObjects = decoder.decodeObjectForKey(key) as? [AnyObject] ?? [] 0094 values = decodedObjects.flatMap { $0 as? PropertyType } 0095 } 0096 } 0097 0098 // MARK:- CustomStringConvertible 0099 0100 extension PropertyArray: CustomStringConvertible { 0101 public var description
PropertyArray.swift:110
        return description
: String { 0102 return "PropertyArray<\(type)> (key: \(key), count: \(values.count), required: \(required))" 0103 } 0104 } 0105 0106 // MARK:- CustomDebugStringConvertible 0107 0108 extension PropertyArray: CustomDebugStringConvertible { 0109 public var debugDescription: String { 0110 return description 0111 } 0112 } 0113 0114 // MARK:- CollectionType 0115 0116 extension PropertyArray: CollectionType { 0117 public func generate() -> IndexingGenerator<[PropertyType]> { 0118 return values.generate() 0119 } 0120 0121 public var startIndex: Int { 0122 return 0 0123 } 0124 0125 public var endIndex: Int { 0126 return values.count 0127 } 0128 0129 public subscript(index: Int) -> PropertyType { 0130 return values[index] 0131 } 0132 } 0133 0134 // MARK:- Hashable 0135 0136 extension PropertyArray: Hashable { 0137 public var hashValue: Int { 0138 return key.hashValue 0139 } 0140 } 0141 0142 // MARK:- Equatable 0143 0144 extension PropertyArray: Equatable {} 0145 0146 public func ==<T: Equatable>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool { 0147 return lhs.key == rhs.key && lhs.values == rhs.values 0148 } 0149 0150 public func ==<T>(lhs: PropertyArray<T>, rhs: PropertyArray<T>) -> Bool { 0151 return lhs.key == rhs.key 0152 } 0153