2016-12-24 63 views
2

我在下面的格式已经JSON数据ManagedObject可选参数:转换JSON在迅速

{ 
    "AvailToDate": "2016-12-31 00:00:00.0", 
    "CompanyName": "Google", 
    "ShowroomName": "Mobile Phones", 
    "OwnerUserId": "OID1544234", 
    "PartyId": "APL026306123", 
    "Currency": "USD", 
    "ProductCount": 10, 
    "AvailFromDate": "2016-12-20 00:00:00.0", 
    "MaxPrice": 10, 
    "MinPrice": 1, 
    "ShowroomId": 11904, 
    "AccessStatus": "Open" 
    } 

我能上述对象数组转换成字典对象的数组。然后我将字典对象转换为核心数据实体。

这里是我的代码Dictionary对象转换为ManagedObject

let showroom = Showroom(context: bgContext) 

showroom.availableFromDate = (tShowroom[kAvailFromDate] as? String)?.toDate() 
showroom.minPrice = tShowroom[kMinPrice] as? Float 
showroom.showroomID = tShowroom[kShowroomId] as! Int32 
showroom.accessStatus = tShowroom[kAccessStatus] as? String 
showroom.availableToDate = (tShowroom[kAvailToDate] as? String)?.toDate() 
showroom.companyName = tShowroom[kCompanyName] as? String 
showroom.showroomName = tShowroom[kShowroomName] as? String 
showroom.ownerID = tShowroom[kOwnerUserId] as? String 
showroom.partyID = tShowroom[kPartyId] as? String 
showroom.currency = tShowroom[kCurrency] as? String 
showroom.productsCount = tShowroom[kProductCount] as! Int32 
showroom.maxPrice = tShowroom[kMaxPrice] as? Float 

如果某些参数缺少JSON收到,我们如何能够解析的对象。我是否必须将我的ManagedObject中的所有参数设置为可选? 我不想为每个参数使用“if let”或“guard”。什么是实现它的最好方法?

+0

如果您不想使用“if let”和“guard”,那么您可以将每个参数声明为Optional ?.但我通常更喜欢使用“如果让”和“警惕”,它将提供安全且无崩溃的代码。 –

回答

0

你可以简单地“默认值”使用??语法添加到变量

比方说您希望最小价格设定为10.2,如果没有从JSON返回一个值,像这样做:

showroom.minPrice = tShowroom[kMinPrice] as? Float ?? 10.2 

或者字符串:

showroom.accessStatus = tShowroom[kAccessStatus] as? String ?? "Default String" 
+0

但在我的情况下,我没有默认值。 – Satyam

+0

然后为int设置它为0,为字符串将它设置为“” –

+0

仍然选项如日期将失败。如果我们设置了某个日期,它至少会失败一次。布尔也不会工作,因为它只有2个州。它可能不是一个具体的解决方案。 – Satyam

0

我会推荐给每一个参数设置为optionalShowroom模型。这样更安全。另外,您可以为您的模型引入一些转换相关的类,您将在那里执行guard - if let检查,并且您只需处理错误。

类似如下:

import Foundation 
import UIKit 

enum DictionaryParseError: Error { 
    case missingKey 
    case invalidType 
} 

extension Dictionary where Key: ExpressibleByStringLiteral { 

    // You are expecting an optinal string 
    func string(key: Key) -> String? { 
     return self[key] as? String 
    } 

    // You are expecting an optinal float 
    func float(key: Key) -> Float? { 
     return self[key] as? Float 
    } 

    // You are expecting a string 
    func requiredString(key: Key) throws -> String { 
     let string = try self.requiredValue(key) as String 
     return string 
    } 

    // You are expecting a float 
    func requiredFloat(key: Key) throws -> Float { 
     let float = try self.requiredValue(key) as Float 
     return float 
    } 

    private func requiredValue<Type>(_ key: Key) throws -> Type { 
     guard let value = self[key] else { 
      throw DictionaryParseError.missingKey 
     } 
     guard let typedValue = value as? Type else { 
      throw DictionaryParseError.invalidType 
     } 
     return typedValue 
    } 
} 
0

如果你没有为管理对象的属性的一个值,你有两个选择:

  • 不要设置为任意值财产,保留为零。这要求在数据模型中将该属性声明为可选。这里的可选项特别意味着该属性被允许没有价值。
  • 如果您没有要分配的特定值,请设置一些默认值。

您做出的选择完全取决于您计划如何使用数据以及是否存在任何合理的默认值。您不必以同样的方式处理所有财产。