“多对多关系”的核心数据崩溃

时间:2016-05-13 07:12:28

标签: ios iphone xcode swift core-data

我无法使用“多对多关系”添加数据

我的代码

let cate2 = self.saveCategories(dict["categories"] as! Array, partyObject: party)//party is NSMAngedObjected Model

let categories = party.mutableSetValueForKey("categories")
                categories.addObjectsFromArray(cate2)
//categories is the many to many relationship between Party & Categories Entity

调试输出

Printing description of cate2:
▿ 1 elements
  ▿ [0] : 1 elements
    - [0] : < apple.Categories: 0x7fbc6ad62dd0> (entity: Categories; id: 0x7fbc6ad62e80 <x-coredata:///Categories/tE393C265-31D1-4E54-92DC-425C9FCB56493> ; data: {
    "category_Name" = 1;
    parties =     (
    ); //parties in the inverse relationship with Party Entity
})

2016-05-13 12:33:49.642 apple[6456:94286] -[_TtCs21_SwiftDeferredNSArray entity]: unrecognized selector sent to instance 0x7fbc6ad68a40
2016-05-13 12:33:49.688 apple[6456:94286] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtCs21_SwiftDeferredNSArray entity]: unrecognized selector sent to instance 0x7fbc6ad68a40

修改

func saveCategories(arr_categories:[AnyObject],partyObject:Party) -> [AnyObject] {


        var categories_model = [Categories]()

        if(!arr_categories.isEmpty){
        for categoryObject in arr_categories {

                let  category = NSEntityDescription.insertNewObjectForEntityForName("Categories",
                                                                                 inManagedObjectContext: appDelegate.managedObjectContext) as! Categories
                category.category_Name = String(categoryObject)
                //category.parties = partyObject
                //category.parties = [partyObject]
                categories_model.append(category)
            }

            return categories_model
    }

        return []

            }

我为saveCategory添加了代码。它将返回Categories nsmangedObject Model的数组。

帮我解决这个问题

谢谢,

0 个答案:

没有答案