简体   繁体   English

如何在Swift中附加CoreData对象?

[英]How Do I append CoreData Object with Swift?

I'd like to add myplaceworks from server to coredata Here is my coredata object 我想将myplaceworks从服务器添加到coredata,这是我的coredata对象

private var user: [User]?
private var myPlaceOfWorks: [MyPlaceOfWorks]?
private var context = sharedManagedObjectContext()

and I tried this workplace of parameter is already made class. 我试过这个参数工作场所已经上课了。

this class controls array in json 此类控制json中的数组

func addInfo(workplace: [Sub_JsonModel_MyPlaceOfWorks], rtnvalue: String) ->
(User?, MyPlaceOfWorks?) {

    guard let newUser: User = 
NSEntityDescription.insertNewObjectForEntityForName("User",  
inManagedObjectContext: self.context) as? User else{
        return (nil, nil)

    }

    guard let newWorkPlace: MyPlaceOfWorks = 
NSEntityDescription.insertNewObjectForEntityForName("MyPlaceOfWorks", 
inManagedObjectContext: self.context) as? MyPlaceOfWorks else {
        return (nil, nil)
    }

    newUser.rtnvalue = rtnvalue

    for one in workplace {

        newWorkPlace.activeyn = one.ActiveYN
        newWorkPlace.basic = one.Basic
        newWorkPlace.beacon = one.Beacon
        newWorkPlace.cpiseq = one.CPISEQ
        newWorkPlace.cpmpoweq = one.CPMPOWSEQ
        newWorkPlace.companyname = one.CompanyName
        newWorkPlace.etime = one.ETime
        newWorkPlace.gps_latitude = one.GPS_latitude
        newWorkPlace.gps_longitude = one.GPS_longitude
        newWorkPlace.placename = one.PlaceName
        newWorkPlace.stime = one.STime
        newWorkPlace.wifi = one.Wifi
        self.myPlaceOfWorks?.append(newWorkPlace)
        print("newWorkPlace \(newWorkPlace)")
        print("myPlaceOfWorks \(myPlaceOfWorks)")
    }

    self.saveContext { 
        //action
        print("newUser:: \(newUser)")

    }
    return (newUser, newWorkPlace)
}

private func saveContext(completion: (() -> Void)?) {
    do {
        try self.context.save()
        if let handler = completion {
            NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                handler()
            })
        }
    } catch let error {
        print("can not save context \(error)")
    }
}

I can get rtnvalue but, I can't get myPlaceOfWorks array object! 我可以获取rtnvalue,但是我无法获取myPlaceOfWorks数组对象! It is just nil How can I try this? 只是零,我该如何尝试?

You've created only one object of MyPlaceOfWorks class and then rewriting properties of this object in the loop. 您仅创建了MyPlaceOfWorks类的一个对象,然后在循环中重写了该对象的属性。 Instead you need to create new instance on each loop iteration. 相反,您需要在每次循环迭代中创建新实例。

for one in workplace {
    if let newWorkPlace: MyPlaceOfWorks = NSEntityDescription.insertNewObjectForEntityForName("MyPlaceOfWorks", inManagedObjectContext: self.context) as? MyPlaceOfWorks {
        newWorkPlace.activeyn = one.ActiveYN
        newWorkPlace.basic = one.Basic
        newWorkPlace.beacon = one.Beacon
        newWorkPlace.cpiseq = one.CPISEQ
        newWorkPlace.cpmpoweq = one.CPMPOWSEQ
        newWorkPlace.companyname = one.CompanyName
        newWorkPlace.etime = one.ETime
        newWorkPlace.gps_latitude = one.GPS_latitude
        newWorkPlace.gps_longitude = one.GPS_longitude
        newWorkPlace.placename = one.PlaceName
        newWorkPlace.stime = one.STime
        newWorkPlace.wifi = one.Wifi
        self.myPlaceOfWorks?.append(newWorkPlace)
    }
}
self.saveContext { 
    //action
    print("newUser:: \(newUser)")

}

And as zcui93 has mentioned in comments you need to instantiate your myPlaceOfWorks variable: 正如zcui93在评论中提到的那样,您需要实例化myPlaceOfWorks变量:

private var myPlaceOfWorks = [MyPlaceOfWorks]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM