无法在Swift类之间传递Core Data对象

时间:2017-06-06 18:46:04

标签: core-data swift3

我有一组核心数据实体,我可以成功地从Objective-C VC传递到Swift VC。我已经通过控制台验证了他们正在进入。

现在我需要遍历核心数据对象数组并将它们传递到另一个swift vc。我将数组设置为属性(Customer是CD实体):

var arrCustomers:Array = Customer

在我的循环中,我试图将它们传递给另一个Swift VC:

for thisCustomer in self.arrCustomers {

    let gridCell = storyboard.instantiateViewController(withIdentifier: "bulkAddCell") as! BulkAddCell
    resizeGridCell(vGridCell: gridCell.view, frameY: frameY, frameH:50.0)

    gridCell.thisCustomer = thisCustomer

    self.svData.addSubview(gridCell.view)
}

在BulkAddCell中,我将thisCustomer作为属性:

var thisCustomer: Customer!

我知道BulkAddCell正在初始化,因为我有一些IBOutlets,我可以在控制台中看到它们。但这个顾客总是零。

我正在从Obj-C切换到Swift,所以我确信这是我缺少的基本功能。

1 个答案:

答案 0 :(得分:0)

更改:

var arrCustomers: Array = Customer

var arrCustomers = [Customer]() //init to an empty array of customer objects

//sometime later
//append your arrCustomers with instances of Customer

// before leaving first view controller access the arrCustomers object 
// contents as needed and assign to a corresponding property on the 
// destination view controller. (usually in preparforSegue)