diff --git a/ObjectMapper/Core/FromJSON.swift b/ObjectMapper/Core/FromJSON.swift index 3c4ecb24..5e47fde8 100755 --- a/ObjectMapper/Core/FromJSON.swift +++ b/ObjectMapper/Core/FromJSON.swift @@ -74,13 +74,13 @@ class FromJSON { func object(inout field: N, object: AnyObject?) { if let value = object as? [String : AnyObject] { - field = Mapper().map(value, toType: N.self) + field = Mapper().map(value) } } func object(inout field: N?, object: AnyObject?) { if let value = object as? [String : AnyObject] { - field = Mapper().map(value, toType: N.self) + field = Mapper().map(value) } } @@ -104,14 +104,14 @@ class FromJSON { // parses a JSON array into an array of objects of type private func parseObjectArray(object: AnyObject?) -> Array{ - let mapper = Mapper() + let mapper = Mapper() var parsedObjects = Array() if let array = object as [AnyObject]? { for object in array { let objectJSON = object as [String : AnyObject] - var parsedObj = mapper.map(objectJSON, toType: N.self) + var parsedObj = mapper.map(objectJSON) parsedObjects.append(parsedObj) } } @@ -141,14 +141,14 @@ class FromJSON { // parses a JSON Dictionary into an Dictionay of objects of type private func parseObjectDictionary(object: AnyObject?) -> Dictionary{ - let mapper = Mapper() + let mapper = Mapper() var parsedObjectsDictionary = Dictionary() if let dictionary = object as Dictionary? { for (key, object) in dictionary { let objectJSON = object as [String : AnyObject] - var parsedObj = mapper.map(objectJSON, toType: N.self) + var parsedObj = mapper.map(objectJSON) parsedObjectsDictionary[key] = parsedObj } } diff --git a/ObjectMapper/Core/Mapper.swift b/ObjectMapper/Core/Mapper.swift index 45317707..e482c04f 100755 --- a/ObjectMapper/Core/Mapper.swift +++ b/ObjectMapper/Core/Mapper.swift @@ -9,7 +9,7 @@ import Foundation public protocol Mappable { - mutating func map(mapper: Mapper) + mutating func map(mapper: Mapper) init() } @@ -18,7 +18,7 @@ enum MappingType { case toJSON } -public class Mapper { +public class Mapper { var JSONDictionary: [String : AnyObject] = [:] var currentValue: AnyObject? var currentKey: String? @@ -43,7 +43,7 @@ public class Mapper { } // map a JSON string onto an existing object - public func map(string JSON: String, var toObject object: N) -> N! { + public func map(string JSON: String, var toObject object: N) -> N! { var json = parseJSONDictionary(JSON) if let json = json { mappingType = .fromJSON @@ -56,23 +56,23 @@ public class Mapper { } // map a JSON string to an object Type that conforms to Mappable - public func map(string JSONString: String, toType type: N.Type) -> N! { + public func map(string JSONString: String) -> N! { var json = parseJSONDictionary(JSONString) if let json = json { - return map(json, toType: type) + return map(json) } return nil } // maps a JSON dictionary to an object that conforms to Mappable - public func map(JSON: [String : AnyObject], toType type: N.Type) -> N! { + public func map(JSON: [String : AnyObject]) -> N! { var object = N() return map(JSON, toObject: object) } // maps a JSON dictionary to an existing object that conforms to Mappable. // Usefull for those pesky objects that have crappy designated initializers like NSManagedObject - public func map(JSON: [String : AnyObject], var toObject object: N) -> N! { + public func map(JSON: [String : AnyObject], var toObject object: N) -> N! { mappingType = .fromJSON self.JSONDictionary = JSON object.map(self) @@ -80,23 +80,23 @@ public class Mapper { } // maps a JSON array to an object that conforms to Mappable - public func mapArray(string JSONString: String, toType type: N.Type) -> [N]! { + public func mapArray(string JSONString: String) -> [N]! { var jsonArray = parseJSONArray(JSONString) if let jsonArray = jsonArray { - return mapArray(jsonArray, toType: type) + return mapArray(jsonArray) } else { // failed to parse JSON into array form // try to parse it into a dictionary and then wrap it in an array var jsonDict = parseJSONDictionary(JSONString) if let jsonDict = jsonDict { - return mapArray([jsonDict], toType: type) + return mapArray([jsonDict]) } } return nil } // maps a JSON dictionary to an object that conforms to Mappable - public func mapArray(JSON: [[String : AnyObject]], toType type: N.Type) -> [N] { + public func mapArray(JSON: [[String : AnyObject]]) -> [N] { mappingType = .fromJSON var objects: Array = [] @@ -113,7 +113,7 @@ public class Mapper { } // maps an Object to a JSON dictionary - public func toJSON(var object: N) -> [String : AnyObject] { + public func toJSON(var object: N) -> [String : AnyObject] { mappingType = .toJSON self.JSONDictionary = [String : AnyObject]() @@ -123,7 +123,7 @@ public class Mapper { } // maps an array of Objects to an array of JSON dictionaries [[String : AnyObject]] - public func toJSONArray(array: [N]) -> [[String : AnyObject]] { + public func toJSONArray(array: [N]) -> [[String : AnyObject]] { mappingType = .toJSON var JSONArray = [[String : AnyObject]]() @@ -138,7 +138,7 @@ public class Mapper { } // maps an Object to a JSON string - public func toJSONString(object: N, prettyPrint: Bool) -> String! { + public func toJSONString(object: N, prettyPrint: Bool) -> String! { let JSONDict = toJSON(object) var err: NSError? diff --git a/ObjectMapper/Core/Operators.swift b/ObjectMapper/Core/Operators.swift index c426b5c9..72370983 100755 --- a/ObjectMapper/Core/Operators.swift +++ b/ObjectMapper/Core/Operators.swift @@ -13,7 +13,7 @@ import Foundation //infix operator <= {} // MARK: basic type -public func <=(inout left: T, right: Mapper) { +public func <=(inout left: T, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().baseType(&left, object: right.currentValue) } else { @@ -22,7 +22,7 @@ public func <=(inout left: T, right: Mapper) { } // Optional basic type -public func <=(inout left: T?, right: Mapper) { +public func <=(inout left: T?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().optionalBaseType(&left, object: right.currentValue) } else { @@ -31,7 +31,7 @@ public func <=(inout left: T?, right: Mapper) { } // Basic type with Transform -public func <=(inout left: T, right: (Mapper, MapperTransform)) { +public func <=(inout left: T, right: (Mapper, MapperTransform)) { if right.0.mappingType == MappingType.fromJSON { var value: T? = right.1.transformFromJSON(right.0.currentValue) //println("FromJSON \(value)"); @@ -44,7 +44,7 @@ public func <=(inout left: T, right: (Mapper, MapperTransform)) { } // Optional basic type with Transform -public func <=(inout left: T?, right: (Mapper, MapperTransform)) { +public func <=(inout left: T?, right: (Mapper, MapperTransform)) { if right.0.mappingType == MappingType.fromJSON { var value: T? = right.1.transformFromJSON(right.0.currentValue) //println("FromJSON \(value)"); @@ -57,7 +57,7 @@ public func <=(inout left: T?, right: (Mapper, MapperTransform)) { } // MARK:- T: Mappable -public func <=(inout left: T, right: Mapper) { +public func <=(inout left: T, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().object(&left, object: right.currentValue) } else { @@ -66,7 +66,7 @@ public func <=(inout left: T, right: Mapper) { } // Optional object conforming to Mappable -public func <=(inout left: T?, right: Mapper) { +public func <=(inout left: T?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().object(&left, object: right.currentValue) } else { @@ -75,7 +75,7 @@ public func <=(inout left: T?, right: Mapper) { } // MARK:- Dictionary -public func <=(inout left: Dictionary, right: Mapper) { +public func <=(inout left: Dictionary, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().objectDictionary(&left, object: right.currentValue) } else { @@ -84,7 +84,7 @@ public func <=(inout left: Dictionary, right: Mapper) { } // Optional Dictionary -public func <=(inout left: Dictionary?, right: Mapper) { +public func <=(inout left: Dictionary?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().optionalObjectDictionary(&left, object: right.currentValue) } else { @@ -93,7 +93,7 @@ public func <=(inout left: Dictionary?, right: Mapper) { } // MARK: Dictionary -public func <=(inout left: Dictionary, right: Mapper) { +public func <=(inout left: Dictionary, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().baseType(&left, object: right.currentValue) } else { @@ -102,7 +102,7 @@ public func <=(inout left: Dictionary, right: Mapper) { } // Optional dictionary -public func <=(inout left: Dictionary?, right: Mapper) { +public func <=(inout left: Dictionary?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().optionalBaseType(&left, object: right.currentValue) } else { @@ -111,7 +111,7 @@ public func <=(inout left: Dictionary?, right: Mapper) { } // MARK:- Array -public func <=(inout left: Array, right: Mapper) { +public func <=(inout left: Array, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().objectArray(&left, object: right.currentValue) } else { @@ -120,7 +120,7 @@ public func <=(inout left: Array, right: Mapper) { } // Optional array of objects conforming to Mappable -public func <=(inout left: Array?, right: Mapper) { +public func <=(inout left: Array?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().optionalObjectArray(&left, object: right.currentValue) } else { @@ -129,7 +129,7 @@ public func <=(inout left: Array?, right: Mapper) { } // MARK: Array -public func <=(inout left: Array, right: Mapper) { +public func <=(inout left: Array, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().baseType(&left, object: right.currentValue) } else { @@ -138,7 +138,7 @@ public func <=(inout left: Array, right: Mapper) { } // Optional array of String type -public func <=(inout left: Array?, right: Mapper) { +public func <=(inout left: Array?, right: Mapper) { if right.mappingType == MappingType.fromJSON { FromJSON().optionalBaseType(&left, object: right.currentValue) } else { diff --git a/ObjectMapper/Core/ToJSON.swift b/ObjectMapper/Core/ToJSON.swift index 752f1f46..f3623e16 100644 --- a/ObjectMapper/Core/ToJSON.swift +++ b/ObjectMapper/Core/ToJSON.swift @@ -88,7 +88,7 @@ class ToJSON { } func object(field: N, key: String, inout dictionary: [String : AnyObject]) { - let mapper = Mapper() + let mapper = Mapper() dictionary[key] = NSDictionary(dictionary: mapper.toJSON(field)) } @@ -103,7 +103,7 @@ class ToJSON { var JSONObjects = NSMutableArray() for object in field { - let mapper = Mapper() + let mapper = Mapper() JSONObjects.addObject(mapper.toJSON(object)) } @@ -122,7 +122,7 @@ class ToJSON { var JSONObjects = NSMutableDictionary() for (k, object) in field { - let mapper = Mapper() + let mapper = Mapper() JSONObjects.setObject(mapper.toJSON(object), forKey: k) } diff --git a/ObjectMapperTests/BasicTypesTests.swift b/ObjectMapperTests/BasicTypesTests.swift index 09169135..5f8893d6 100644 --- a/ObjectMapperTests/BasicTypesTests.swift +++ b/ObjectMapperTests/BasicTypesTests.swift @@ -12,6 +12,8 @@ import ObjectMapper class BasicTypesTests: XCTestCase { + let mapper = Mapper() + override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. @@ -32,7 +34,7 @@ class BasicTypesTests: XCTestCase { object.boolOptional = value let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { @@ -50,7 +52,7 @@ class BasicTypesTests: XCTestCase { object.intOptional = value let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { @@ -68,7 +70,7 @@ class BasicTypesTests: XCTestCase { object.doubleOptional = value let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { @@ -86,7 +88,7 @@ class BasicTypesTests: XCTestCase { object.floatOptional = value let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { @@ -104,7 +106,7 @@ class BasicTypesTests: XCTestCase { object.stringOptional = value let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { @@ -124,7 +126,7 @@ class BasicTypesTests: XCTestCase { object.arrayBoolOptional = [value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { var firstObject = mappedObject.arrayBool[0] @@ -142,7 +144,7 @@ class BasicTypesTests: XCTestCase { object.arrayIntOptional = [value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { var firstObject = mappedObject.arrayInt[0] @@ -160,7 +162,7 @@ class BasicTypesTests: XCTestCase { object.arrayDoubleOptional = [value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { var firstObject = mappedObject.arrayDouble[0] @@ -178,7 +180,7 @@ class BasicTypesTests: XCTestCase { object.arrayFloatOptional = [value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { var firstObject = mappedObject.arrayFloat[0] @@ -196,7 +198,7 @@ class BasicTypesTests: XCTestCase { object.arrayStringOptional = [value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { var firstObject = mappedObject.arrayString[0] @@ -217,7 +219,7 @@ class BasicTypesTests: XCTestCase { object.dictBoolOptional = [key:value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { if let val = mappedObject.dictBoolOptional?[key] { @@ -237,7 +239,7 @@ class BasicTypesTests: XCTestCase { object.dictIntOptional = [key:value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { if let val = mappedObject.dictIntOptional?[key] { @@ -257,7 +259,7 @@ class BasicTypesTests: XCTestCase { object.dictDoubleOptional = [key:value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { if let val = mappedObject.dictDoubleOptional?[key] { @@ -277,7 +279,7 @@ class BasicTypesTests: XCTestCase { object.dictFloatOptional = [key:value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { if let val = mappedObject.dictFloatOptional?[key] { @@ -297,7 +299,7 @@ class BasicTypesTests: XCTestCase { object.dictStringOptional = [key:value] let JSON = Mapper().toJSONString(object, prettyPrint: true) - var mappedObject = Mapper().map(string: JSON, toType: BasicTypes.self) + var mappedObject = mapper.map(string: JSON) if let mappedObject = mappedObject { if let val = mappedObject.dictStringOptional?[key] { @@ -347,7 +349,7 @@ class BasicTypes: Mappable { required init() { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { bool <= mapper["bool"] boolOptional <= mapper["boolOpt"] int <= mapper["int"] diff --git a/ObjectMapperTests/ObjectMapperTests.swift b/ObjectMapperTests/ObjectMapperTests.swift index ca41c19b..7e8464b5 100644 --- a/ObjectMapperTests/ObjectMapperTests.swift +++ b/ObjectMapperTests/ObjectMapperTests.swift @@ -11,6 +11,8 @@ import XCTest import ObjectMapper class ObjectMapperTests: XCTestCase { + + let userMapper = Mapper() override func setUp() { super.setUp() @@ -44,7 +46,7 @@ class ObjectMapperTests: XCTestCase { let userJSONString = "{\"username\":\"\(username)\",\"identifier\":\"\(identifier)\",\"photoCount\":\(photoCount),\"age\":\(age),\"drinker\":\(drinker),\"smoker\":\(smoker), \"arr\":[ \"bla\", true, 42 ], \"dict\":{ \"key1\" : \"value1\", \"key2\" : false, \"key3\" : 142 }, \"arrOpt\":[ \"bla\", true, 42 ], \"dictOpt\":{ \"key1\" : \"value1\", \"key2\" : false, \"key3\" : 142 }, \"birthday\": 1398956159, \"birthdayOpt\": 1398956159, \"y2k\" : \"2000-01-01T00:00:00Z\", \"y2kOpt\" : \"2000-01-01T00:00:00Z\", \"weight\": \(weight), \"float\": \(float), \"friend\": \(subUserJSON), \"friendDictionary\":{ \"bestFriend\": \(subUserJSON)}}" - let user = Mapper().map(string: userJSONString, toType: User.self) + let user = userMapper.map(string: userJSONString) XCTAssertEqual(username, user.username, "Username should be the same") XCTAssertEqual(identifier, user.identifier!, "Identifier should be the same") @@ -138,7 +140,7 @@ class ObjectMapperTests: XCTestCase { let userJSONString = "{\"username\":\"bob\", \"height\": {\"value\": \(heightInCM), \"text\": \"6 feet tall\"} }" - let user = Mapper().map(string: userJSONString, toType: User.self) + let user = userMapper.map(string: userJSONString) XCTAssertEqual(user.heightInCM!, heightInCM, "Username should be the same") } @@ -146,7 +148,7 @@ class ObjectMapperTests: XCTestCase { func testNullObject() { let userJSONString = "{\"username\":\"bob\"}" - let user = Mapper().map(string: userJSONString, toType: User.self) + let user = userMapper.map(string: userJSONString) XCTAssert(user.heightInCM == nil, "Username should be the same") } @@ -168,7 +170,7 @@ class ObjectMapperTests: XCTestCase { let jsonString = Mapper().toJSONString(user, prettyPrint: true) println(jsonString) - var parsedUser = Mapper().map(string: jsonString, toType: User.self) + var parsedUser = userMapper.map(string: jsonString) XCTAssertEqual(user.identifier!, parsedUser.identifier!, "Identifier should be the same") @@ -185,7 +187,7 @@ class ObjectMapperTests: XCTestCase { func testUnknownPropertiesIgnored() { let userJSONString = "{\"username\":\"bob\",\"identifier\":\"bob1987\", \"foo\" : \"bar\", \"fooArr\" : [ 1, 2, 3], \"fooObj\" : { \"baz\" : \"qux\" } }" - let user = Mapper().map(string: userJSONString, toType: User.self) + let user = userMapper.map(string: userJSONString) XCTAssert(user != nil, "User should not be nil") } @@ -193,7 +195,7 @@ class ObjectMapperTests: XCTestCase { func testInvalidJsonResultsInNilObject() { let userJSONString = "{\"username\":\"bob\",\"identifier\":\"bob1987\"" // missing ending brace - let user = Mapper().map(string: userJSONString, toType: User.self) + let user = userMapper.map(string: userJSONString) XCTAssert(user == nil, "User should be nil due to invalid JSON") } @@ -204,7 +206,7 @@ class ObjectMapperTests: XCTestCase { let arrayJSONString = "[{\"name\": \"\(name1)\", \"UUID\": \"3C074D4B-FC8C-4CA2-82A9-6E9367BBC875\", \"major\": 541, \"minor\": 123},{ \"name\": \"\(name2)\", \"UUID\": \"3C074D4B-FC8C-4CA2-82A9-6E9367BBC876\", \"major\": 54321,\"minor\": 13 }]" - let studentArray = Mapper().mapArray(string: arrayJSONString, toType: Student.self) + let studentArray = Mapper().mapArray(string: arrayJSONString) if let students = studentArray { XCTAssert(students.count == 2, "There should be 2 students in array") @@ -222,7 +224,7 @@ class ObjectMapperTests: XCTestCase { let arrayJSONString = "{\"name\": \"\(name1)\", \"UUID\": \"3C074D4B-FC8C-4CA2-82A9-6E9367BBC875\", \"major\": 541, \"minor\": 123}" - let studentArray = Mapper().mapArray(string: arrayJSONString, toType: Student.self) + let studentArray = Mapper().mapArray(string: arrayJSONString) if let students = studentArray { XCTAssert(students.count == 1, "There should be 2 students in array") @@ -239,7 +241,7 @@ class ObjectMapperTests: XCTestCase { let JSON = "{ \"tasks\": [{\"taskId\":103,\"percentage\":\(percentage1)},{\"taskId\":108,\"percentage\":\(percentage2)}] }" - let plan = Mapper().map(string: JSON, toType: Plan.self) + let plan = Mapper().map(string: JSON) if let tasks = plan.tasks { let task1 = tasks[0] @@ -256,7 +258,7 @@ class ObjectMapperTests: XCTestCase { let code: Int = 22 let JSON = "{\"result\":{\"code\":\(code)}}" - let response = Mapper().map(string: JSON, toType: Response.self) + let response = Mapper>().map(string: JSON) if let status = response.result?.status { XCTAssertEqual(status, code, "Code was not mapped correctly") @@ -301,16 +303,14 @@ class ObjectMapperTests: XCTestCase { } func testISO8601DateTransformWithInvalidInput() { - let mapper = Mapper() - var JSON: [String: AnyObject] = ["y2kOpt": ""] - let user1 = mapper.map(JSON, toType: User.self) + let user1 = userMapper.map(JSON) XCTAssert(user1 != nil, "ISO8601DateTransform must not crash for empty string") XCTAssert(user1.y2kOpt == nil, "ISO8601DateTransform should return nil for empty string") JSON["y2kOpt"] = "incorrect format" - let user2 = mapper.map(JSON, toType: User.self) + let user2 = userMapper.map(JSON) XCTAssert(user2 != nil, "ISO8601DateTransform must not crash for incorrect format") XCTAssert(user2.y2kOpt == nil, "ISO8601DateTransform should return nil for incorrect format") @@ -323,7 +323,7 @@ class Response: Mappable { required init() { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { result <= mapper["result"] } } @@ -334,7 +334,7 @@ class Status: Mappable { required init() { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { status <= mapper["code"] } } @@ -346,7 +346,7 @@ class Plan: Mappable { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { tasks <= mapper["tasks"] } } @@ -359,7 +359,7 @@ class Task: Mappable { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { taskId <= mapper["taskId"] percentage <= mapper["percentage"] } @@ -376,7 +376,7 @@ struct Student: Mappable { } - mutating func map(mapper: Mapper) { + mutating func map(mapper: Mapper) { name <= mapper["name"] UUID <= mapper["UUID"] major <= mapper["major"] @@ -414,7 +414,7 @@ class User: Mappable { } - func map(mapper: Mapper) { + func map(mapper: Mapper) { username <= mapper["username"] identifier <= mapper["identifier"] photoCount <= mapper["photoCount"] diff --git a/README.md b/README.md index 979c4ae8..50dbd553 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ class User: Mappable { required init(){} // Mappable - func map(mapper: Mapper) { + func map(mapper: Mapper) { username <= mapper["username"] age <= mapper["age"] weight <= mapper["weight"] @@ -43,7 +43,7 @@ struct Temperature: Mappable { init(){} - mutating func map(mapper: Mapper) { + mutating func map(mapper: Mapper) { celcius <= mapper["celcius"] fahrenheit <= mapper["fahrenheit"] } @@ -54,7 +54,7 @@ Once your class implements Mappable, the Mapper class handles everything else fo Convert a JSON string to a model object: ```swift -let user = Mapper().map(string: JSONString, toType: User.self) +let user = Mapper().map(string: JSONString) ``` Convert a model object to a JSON string: @@ -106,7 +106,7 @@ ObjectMapper supports dot notation within keys for easy mapping of nested object ``` You can access the nested objects as follows: ``` -func map(mapper: Mapper){ +func map(mapper: Mapper){ distance <= mapper["distance.value"] } ```