Skip to content

Commit

Permalink
- Added a missing case
Browse files Browse the repository at this point in the history
- improved code format
- added a test for nested array objects
  • Loading branch information
tristanhimmelman committed Dec 22, 2015
1 parent c085ea1 commit 512b516
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
36 changes: 18 additions & 18 deletions ObjectMapper/Core/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String
} else if let dict = object as? [String : AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
}
else if let dict = object as? [AnyObject] where keyPathComponents.count > 1 {
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
}
else {
return valueFor(tail, dictionary: array)
} else {
return object
}
}
Expand All @@ -139,22 +137,24 @@ private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [AnyObj
return nil
}

//Try to convert keypath to Int as index)
//Try to convert keypath to Int as index
if let keyPath = keyPathComponents.first,
let index = Int(keyPath) {
let index = Int(keyPath) where index >= 0 && index < dictionary.count {

if index >= 0 && index < dictionary.count {
let object = dictionary[index]

if object is NSNull {
return nil
} else if let dict = object as? [AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else {
return object
}
let object = dictionary[index]

if object is NSNull {
return nil
} else if let array = object as? [AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: array)
} else if let dict = object as? [String : AnyObject] where keyPathComponents.count > 1 {
let tail = keyPathComponents.dropFirst()
return valueFor(tail, dictionary: dict)
} else {
return object
}
}

return nil
}
30 changes: 30 additions & 0 deletions ObjectMapperTests/NestedArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,49 @@ class NestedArrayTests: XCTestCase {
XCTAssertEqual(value.value_0, valueFromParsedJSON.value_0)
XCTAssertEqual(value.value_1, valueFromParsedJSON.value_1)
}

func testNestedObjectArray() {
let value = 456
let JSON: [String: AnyObject] = [ "nested": [ ["value": 123], ["value": value] ] ]

let mapper = Mapper<NestedArray>()

let mappedObject: NestedArray! = mapper.map(JSON)
XCTAssertNotNil(mappedObject)

XCTAssertEqual(mappedObject.nestedObject!.value, value)
XCTAssertEqual(mappedObject.nestedObjectValue, value)
}
}

class NestedArray: Mappable {

var value_0: Int?
var value_1: Int?

var nestedObject: NestedObject?

var nestedObjectValue: Int?

required init?(_ map: Map){

}

func mapping(map: Map) {
value_0 <- map["nested.0.value"]
value_1 <- map["nested.1.value"]

nestedObject <- map["nested.1"]
nestedObjectValue <- map["nested.1.value"]
}
}

class NestedObject: Mappable {
var value: Int?

required init?(_ map: Map){}

func mapping(map: Map) {
value <- map["value"]
}
}

0 comments on commit 512b516

Please sign in to comment.