diff --git a/ObjectMapper/Core/Map.swift b/ObjectMapper/Core/Map.swift index 9349858e..fd852e8d 100644 --- a/ObjectMapper/Core/Map.swift +++ b/ObjectMapper/Core/Map.swift @@ -118,12 +118,10 @@ private func valueFor(keyPathComponents: ArraySlice, 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 } } @@ -139,22 +137,24 @@ private func valueFor(keyPathComponents: ArraySlice, 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 } \ No newline at end of file diff --git a/ObjectMapperTests/NestedArrayTests.swift b/ObjectMapperTests/NestedArrayTests.swift index 6e4e5376..7b5a1cd8 100644 --- a/ObjectMapperTests/NestedArrayTests.swift +++ b/ObjectMapperTests/NestedArrayTests.swift @@ -57,6 +57,19 @@ 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() + + let mappedObject: NestedArray! = mapper.map(JSON) + XCTAssertNotNil(mappedObject) + + XCTAssertEqual(mappedObject.nestedObject!.value, value) + XCTAssertEqual(mappedObject.nestedObjectValue, value) + } } class NestedArray: Mappable { @@ -64,6 +77,10 @@ class NestedArray: Mappable { var value_0: Int? var value_1: Int? + var nestedObject: NestedObject? + + var nestedObjectValue: Int? + required init?(_ map: Map){ } @@ -71,5 +88,18 @@ class NestedArray: Mappable { 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"] } } \ No newline at end of file