0001 // PropertyDictionary.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 PropertyDictionary<T
PropertyDictionary.swift:119 extension PropertyDictionary: CustomStringConvertible {PropertyDictionary.swift:127 extension PropertyDictionary: CustomDebugStringConvertible {PropertyDictionary.swift:135 extension PropertyDictionary: CollectionType {PropertyDictionary.swift:159 extension PropertyDictionary: Hashable {PropertyDictionary.swift:167 extension PropertyDictionary: Equatable {}PropertyDictionary.swift:169 public func ==<T: Equatable>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool {PropertyDictionary.swift:169 public func ==<T: Equatable>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool {PropertyDictionary.swift:173 public func ==<T>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool {PropertyDictionary.swift:173 public func ==<T>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool {: JSONTransformable>: PropertyDescription { 0026 public typealias PropertyType
PropertyDictionary.swift:26 public typealias PropertyType = T= T 0027 0028 /// Backing store for property data 0029 public var values
PropertyDictionary.swift:29 public var values: [String : PropertyType] = [:]PropertyDictionary.swift:32 public var postProcess: (([String : PropertyType]?) -> Void)?PropertyDictionary.swift:39 return "\(PropertyType.self)"PropertyDictionary.swift:45 public var keys: LazyMapCollection<[String : PropertyType], String> {PropertyDictionary.swift:52 public init(key: String, defaultValues: [String : PropertyType] = [:], required: Bool = false, postProcess: (([String : PropertyType]?) -> Void)? = nil) {PropertyDictionary.swift:52 public init(key: String, defaultValues: [String : PropertyType] = [:], required: Bool = false, postProcess: (([String : PropertyType]?) -> Void)? = nil) {PropertyDictionary.swift:71 if let property = PropertyType.fromJSON(object.1) as? PropertyType {PropertyDictionary.swift:71 if let property = PropertyType.fromJSON(object.1) as? PropertyType {PropertyDictionary.swift:110 if let value = value as? PropertyType {PropertyDictionary.swift:136 public func generate() -> DictionaryGenerator<String, PropertyType> {PropertyDictionary.swift:140 public var startIndex: DictionaryIndex<String, PropertyType> {PropertyDictionary.swift:144 public var endIndex: DictionaryIndex<String, PropertyType> {: [String : PropertyType] = [:] 0030 0031 /// Post-processing closure. 0032 public var postProcess
PropertyDictionary.swift:46 return values.keysPropertyDictionary.swift:54 self.values = defaultValuesPropertyDictionary.swift:69 values.removeAll(keepCapacity: false)PropertyDictionary.swift:72 values[object.0] = propertyPropertyDictionary.swift:76 return !values.isEmptyPropertyDictionary.swift:82 for (key, value) in values {PropertyDictionary.swift:90 postProcess?(values)PropertyDictionary.swift:98 for (key, value) in values {PropertyDictionary.swift:108 values.removeAll(keepCapacity: false)PropertyDictionary.swift:111 values[key] = valuePropertyDictionary.swift:121 return "PropertyDictionary<\(type)> (key: \(key), count: \(values.count), required: \(required))"PropertyDictionary.swift:137 return values.generate()PropertyDictionary.swift:141 return values.startIndexPropertyDictionary.swift:145 return values.endIndexPropertyDictionary.swift:149 return values[position]PropertyDictionary.swift:153 return values[key]PropertyDictionary.swift:170 return lhs.key == rhs.key && lhs.values == rhs.valuesPropertyDictionary.swift:170 return lhs.key == rhs.key && lhs.values == rhs.values: (([String : PropertyType]?) -> Void)? 0033 0034 /// JSON parameter key 0035 public var key
PropertyDictionary.swift:56 self.postProcess = postProcessPropertyDictionary.swift:90 postProcess?(values): String 0036 0037 /// Type information 0038 public var type
PropertyDictionary.swift:53 self.key = keyPropertyDictionary.swift:64 let keyPaths = key.componentsSeparatedByString(".")PropertyDictionary.swift:103 coder.encodeObject(objectDictionary, forKey: key)PropertyDictionary.swift:107 let decodedObjects = decoder.decodeObjectForKey(key) as? [String : AnyObject]PropertyDictionary.swift:121 return "PropertyDictionary<\(type)> (key: \(key), count: \(values.count), required: \(required))"PropertyDictionary.swift:161 return key.hashValuePropertyDictionary.swift:170 return lhs.key == rhs.key && lhs.values == rhs.valuesPropertyDictionary.swift:170 return lhs.key == rhs.key && lhs.values == rhs.valuesPropertyDictionary.swift:174 return lhs.key == rhs.keyPropertyDictionary.swift:174 return lhs.key == rhs.key: String { 0039 return "\(PropertyType.self)" 0040 } 0041 0042 /// Specify whether value is required 0043 public var required
PropertyDictionary.swift:121 return "PropertyDictionary<\(type)> (key: \(key), count: \(values.count), required: \(required))"= false 0044 0045 public var keys: LazyMapCollection<[String : PropertyType], String> { 0046 return values.keys 0047 } 0048 0049 // MARK: Initialization 0050 0051 /// Initialize with JSON property key and default value 0052 public init(key: String, defaultValues: [String : PropertyType] = [:], required: Bool = false, postProcess: (([String : 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 let keyPaths = key.componentsSeparatedByString(".") 0065 for key in keyPaths { 0066 jsonValue = jsonValue[key] 0067 } 0068 0069 values.removeAll(keepCapacity: false) 0070 for object in jsonValue.dictionary ?? [:] { 0071 if let property = PropertyType.fromJSON(object.1) as? PropertyType { 0072 values[object.0] = property 0073 } 0074 } 0075 0076 return !values.isEmpty 0077 } 0078 0079 /// Convert object to JSON 0080 public func toJSON() -> AnyObject? { 0081 var jsonDictionary: [String : AnyObject] = [:] 0082 for (key, value) in values { 0083 jsonDictionary[key] = value.toJSON() 0084 } 0085 return jsonDictionary 0086 } 0087 0088 /// Perform initialization post-processing 0089 public func initPostProcess() { 0090 postProcess?(values) 0091 } 0092 0093 // MARK: Coding 0094 0095 /// Encode 0096 public func encode(coder: NSCoder) { 0097 var objectDictionary: [String : AnyObject] = [:] 0098 for (key, value) in values { 0099 if let object: AnyObject = value as? AnyObject { 0100 objectDictionary[key] = object 0101 } 0102 } 0103 coder.encodeObject(objectDictionary, forKey: key) 0104 } 0105 0106 public func decode(decoder: NSCoder) { 0107 let decodedObjects = decoder.decodeObjectForKey(key) as? [String : AnyObject] 0108 values.removeAll(keepCapacity: false) 0109 for (key, value) in decodedObjects ?? [:] { 0110 if let value = value as? PropertyType { 0111 values[key] = value 0112 } 0113 } 0114 } 0115 } 0116 0117 // MARK:- CustomStringConvertible 0118 0119 extension PropertyDictionary: CustomStringConvertible { 0120 public var description
PropertyDictionary.swift:55 self.required = requiredPropertyDictionary.swift:121 return "PropertyDictionary<\(type)> (key: \(key), count: \(values.count), required: \(required))": String { 0121 return "PropertyDictionary<\(type)> (key: \(key), count: \(values.count), required: \(required))" 0122 } 0123 } 0124 0125 // MARK:- CustomDebugStringConvertible 0126 0127 extension PropertyDictionary: CustomDebugStringConvertible { 0128 public var debugDescription: String { 0129 return description 0130 } 0131 } 0132 0133 // MARK:- CollectionType 0134 0135 extension PropertyDictionary: CollectionType { 0136 public func generate() -> DictionaryGenerator<String, PropertyType> { 0137 return values.generate() 0138 } 0139 0140 public var startIndex: DictionaryIndex<String, PropertyType> { 0141 return values.startIndex 0142 } 0143 0144 public var endIndex: DictionaryIndex<String, PropertyType> { 0145 return values.endIndex 0146 } 0147 0148 public subscript(position: DictionaryIndex<String, PropertyType>) -> (String, PropertyType) { 0149 return values[position] 0150 } 0151 0152 public subscript(key: String) -> PropertyType? { 0153 return values[key] 0154 } 0155 } 0156 0157 // MARK:- Hashable 0158 0159 extension PropertyDictionary: Hashable { 0160 public var hashValue: Int { 0161 return key.hashValue 0162 } 0163 } 0164 0165 // MARK:- Equatable 0166 0167 extension PropertyDictionary: Equatable {} 0168 0169 public func ==<T: Equatable>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool { 0170 return lhs.key == rhs.key && lhs.values == rhs.values 0171 } 0172 0173 public func ==<T>(lhs: PropertyDictionary<T>, rhs: PropertyDictionary<T>) -> Bool { 0174 return lhs.key == rhs.key 0175 } 0176
PropertyDictionary.swift:129 return description