核心数据没有保存对象(Swift)

时间:2016-06-13 15:32:04

标签: ios swift core-data ios9

我正在尝试从数组中保存核心数据对象,并在检索它时,我得到的对象没有更新的值。

我相信在保存时我做错了什么。

这是代码:

func getAllHomeSectionsData(entity entity : NSString, data : NSArray) -> NSMutableArray {

        let context: NSManagedObjectContext = DKCoreDataManager.mainManagedObjectContext()

        data.enumerateObjectsUsingBlock({ object, index, stop in

            let homeSectionObject : NSDictionary  = object as! NSDictionary

            let homeSections  = HomeSections.insertInManagedObjectContext(managedObjectContext:context , entity: entity) as! HomeSections

            // Saving Section ID
            let homeSectionID = homeSectionObject.objectForKey("id" )?.integerValue
            homeSections.id = NSNumber(integer : homeSectionID!)


            // Saving Section Type
            let homeSectionType = homeSectionObject.objectForKey("type" )?.integerValue
            homeSections.type = NSNumber(integer : homeSectionType!)

            // Saving URL
            homeSections.url = homeSectionObject.objectForKey("url") as! NSString as String

        })

        do {
            try context.save()
        } catch {
            fatalError("Failure to save context: \(error)")
        }

        let array : NSMutableArray = DKCoreDataManager.getObjectsForEntity(entityName: entity, sortAscending: true, managedObjectContext:DKCoreDataManager.mainManagedObjectContext())

        return array
    }

有人可以建议我如何解决这个问题吗?

0 个答案:

没有答案