0001 import Foundation 0002 0003 /** 0004 The Mappable protocol defines how to create a custom object from a Mapper 0005 0006 Example: 0007 0008 public struct Thing: Mappable { 0009 let string: String 0010 let URL: NSURL? 0011 0012 public init(map: Mapper) throws { 0013 // Attemps to convert the value for the "some_string" key to a String, if it fails 0014 // it throws an error 0015 try string = map.from("some_string") 0016 0017 // Attemps to convert the value for the "base_url" key to an NSURL, if it fails 0018 // it assigns URL to nil 0019 URL = map.optionalFrom("base_url") 0020 } 0021 } 0022 */ 0023 public protocol Mappable{ 0024 /** 0025 Define how your custom object is created from a Mapper object 0026 */ 0027 @warn_unused_result 0028 init
Mappable.swift:51 public extension Mappable {Mapper.swift:147 public func from<T: Mappable>(field: String) throws -> T {Mapper.swift:171 public func from<T: Mappable>(field: String) throws -> [T] {Mapper.swift:190 public func optionalFrom<T: Mappable>(field: String) -> T? {Mapper.swift:207 public func optionalFrom<T: Mappable>(field: String) -> [T]? {Mapper.swift:220 public func optionalFrom<T: Mappable>(fields: [String]) -> T? {Transform+Dictionary.swift:50 public static func toDictionary<T, U where T: Mappable, U: Hashable>(key getKey: T -> U) ->(map: Mapper) throws 0029 0030 /** 0031 Convenience method for creating Mappable objects from NSDictionaries 0032 0033 - parameter JSON: The JSON to create the object from 0034 0035 - returns: The object if it could be created, nil if creating the object threw an error 0036 */ 0037 @warn_unused_result 0038 static func from(JSON: NSDictionary) -> Self? 0039 0040 /** 0041 Convenience method for creating Mappable objects from a NSArray 0042 0043 - parameter JSON: The JSON to create the objects from 0044 0045 - returns: An array of the created objects, or nil if creating threw 0046 */ 0047 @warn_unused_result 0048 static func from(JSON: NSArray) -> [Self]? 0049 } 0050 0051 public extension Mappable { 0052 @warn_unused_result 0053 public static func from(JSON: NSDictionary) -> Self? { 0054 return try? self.init(map: Mapper(JSON: JSON)) 0055 } 0056 0057 @warn_unused_result 0058 public static func from(JSON: NSArray) -> [Self]? { 0059 if let array = JSON as? [NSDictionary] { 0060 return try? array.map { try self.init(map: Mapper(JSON: $0)) } 0061 } 0062 0063 return nil 0064 } 0065 } 0066
Mappable.swift:54 return try? self.init(map: Mapper(JSON: JSON))Mappable.swift:60 return try? array.map { try self.init(map: Mapper(JSON: $0)) }Mapper.swift:149 return try T(map: Mapper(JSON: JSON))Mapper.swift:173 return try JSON.map { try T(map: Mapper(JSON: $0)) }Transform+Dictionary.swift:60 let model = try T(map: Mapper(JSON: object))