如何在Swift中涉及2个对象时重用一堆代码

时间:2018-02-20 19:31:23

标签: ios swift

我多次遇到过这个问题,但最后还是询问如何在Swift和iOS中解决这个问题。我有相同的代码块,它针对2种不同类型的对象运行,但我无法弄清楚,如何将代码应用于强类型的2种对象类型中的任何一种。在其他语言(如PHP)中,我只是将一个通用var myVar = nil放在块的顶部,然后将其设置为这些对象中的一个或另一个,然后将代码挂起,但很快就赢了。让我这样做。

如何在Swift / iOS中处理这种编程情况?

static func entitySetup(jsonObject: JSON, object: MyCoreDataEntityObject?) {
    if let existingObj = object { // existingObj = a MyCoreDataEntityObject
        existingObj.setValue(jsonObject["firstName"].stringValue, forKey: "firstName")
        existingObj.setValue(jsonObject["lastName"].stringValue, forKey: "lastName")
        existingObj.setValue(jsonObject["address"].stringValue, forKey: "address")
        existingObj.setValue(jsonObject["phone"].stringValue, forKey: "phone")
    } else { // newObj = NSManagedObject
        let entity = NSEntityDescription.entity(forEntityName: "MyCoreDataEntityObject", in: self.context)
        let newObj = NSManagedObject(entity: entity!, insertInto: self.context)
        newObj.setValue(jsonObject["firstName"].stringValue, forKey: "firstName")
        newObj.setValue(jsonObject["lastName"].stringValue, forKey: "lastName")
        newObj.setValue(jsonObject["address"].stringValue, forKey: "address")
        newObj.setValue(jsonObject["phone"].stringValue, forKey: "phone")
    }
    do {
        try self.context.save()
    } catch let error as NSError  {
        print("ERROR: Could not save \(error), \(error.userInfo)")
        completionHandler(false)
    }
}

2 个答案:

答案 0 :(得分:3)

由于两个对象都是NSManagedObject,您可以这样做:

let mangedObject: NSManagedObject
if let existingObj = object {
    managedObject = existingObj
} else {
    let entity = NSEntityDescription.entity(forEntityName: "MyCoreDataEntityObject", in: self.context)
    managedObject = NSManagedObject(entity: entity!, insertInto: self.context)
}

managedObject.setValue(jsonObject["firstName"].stringValue, forKey: "firstName")
managedObject.setValue(jsonObject["lastName"].stringValue, forKey: "lastName")
managedObject.setValue(jsonObject["address"].stringValue, forKey: "address")
managedObject.setValue(jsonObject["phone"].stringValue, forKey: "phone")

do {
    try self.context.save()
} catch let error as NSError  {
    print("ERROR: Could not save \(error), \(error.userInfo)")
    completionHandler(false)
}

根据建议,调用setValue的四行可以简化为:

for key in ["firstName", "lastName", "address", "phone"] {
    managedObject.setValue(jsonObject[key].stringValue, forKey: key)
}

答案 1 :(得分:0)

试试这个

static func entitySetup(jsonObject: JSON, object: MyCoreDataEntityObject?) {

    let newObject = object ?? NSManagedObject(entity: NSEntityDescription.entity(forEntityName: "MyCoreDataEntityObject", in: self.context)!, insertInto: self.context)

    for key in ["firstName", "lastName", "address", "phone"] {
        newObject.setValue(jsonObject[key].stringValue, forKey: key)
    }

    do {
        try self.context.save()
    } catch let error as NSError  {
        print("ERROR: Could not save \(error), \(error.userInfo)")
        completionHandler(false)
    }
}