这是我的对象
{
"id": 1,
"discriminator": "product"
}
我的代码:
@objcMembers
class RLBCategory: Base {
enum CategoryType: String {
case product
case event
case workshop
case industry
}
// MARK: - Properties
dynamic var name: String?
dynamic var discriminator: String?
override func mapping(map: Map) {
super.mapping(map: map)
name <- map["name"]
discriminator <- map["discriminator"]
}
}
var json = ["id": 1, "name": "Sport", "discriminator": "product"] as [String: Any]
var mapper = Map(mappingType: .fromJSON, JSON: json)
var category = RLBCategory(map: mapper)
XCTAssertEqual(category?.id, 1)
XCTAssertEqual(category?.name, "Sport")
XCTAssertEqual(category?.discriminator, RLBCategory.CategoryType.product.rawValue)
json = ["id": 1, "name": "Football", "discriminator": "product"] as [String: Any]
mapper = Map(mappingType: .fromJSON, JSON: json)
category = RLBCategory(map: mapper)
XCTAssertEqual(category?.id, 1)
XCTAssertEqual(category?.name, "Football")
XCTAssertEqual(category?.discriminator, RLBCategory.CategoryType.product.rawValue)
json = ["id": 1, "discriminator": "product"] as [String: Any]
mapper = Map(mappingType: .fromJSON, JSON: json)
category = RLBCategory(map: mapper)
XCTAssertEqual(category?.id, 1)
XCTAssertEqual(category?.name, "Football")
XCTAssertEqual(category?.discriminator,
RLBCategory.CategoryType.product.rawValue)
我希望所有单元测试断言都为true
。更新的对象名称不应修改,因为它不是JSON。
XCTAssertEqual(category?.name, "Football")
之所以为false
,因为更新的对象名现在为nil。
如何将缺少Json属性的Json对象更新为默认值?