将字典保存到核心数据 [英] Saving a Dictionary to Core Data

查看:42
本文介绍了将字典保存到核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序解析播客RSS提要.我使用2个实体:播客(用于保存与播客相关的数据)和剧集(情节数据,例如摘要等).解析供稿后,我将情节列表存储在名为"episodesToDisplay"的数组中.当用户订阅播客时,我想将该阵列保存的数据保存在Core Data中.这是我的代码,在下面带注释的行上引发错误:

My app parses podcast RSS feeds. I use 2 entities: Podcasts (to hold podcast-related data) and Episodes (Episodes data like summaries etc). After parsing a feed, I store the list of episodes in an Array called "episodesToDisplay". When a user subscribes to a podcast, I want to save the data held by that array in Core Data. Here is my code which throws an error on the annotated line below:

class Podcasts: UITableViewController {
var currentPodcast: Podcasts!

    override func viewDidLoad() {
    super.viewDidLoad()
let podcastsEntity = NSEntityDescription.entityForName("Podcasts", inManagedObjectContext: self.managedContext)
    let podcastsFetch = NSFetchRequest(entityName: "Podcasts")
    var error: NSError?

    let result = self.managedContext.executeFetchRequest(podcastsFetch, error: &error) as [Podcasts]?
    if let resu = result {
        println("res is \(resu.count)")
        self.currentPodcast = resu[0] as Podcasts
    } else {
        println("did not work")
    }
}

    @IBAction func subscribe(sender: AnyObject) {
        for dict: AnyObject in episodesToDisplay {
    let episodesEntity = NSEntityDescription.entityForName("Episodes", inManagedObjectContext: self.managedContext)
    let episodesToSave = Episodes(entity: episodesEntity!, insertIntoManagedObjectContext: self.managedContext)
    var episodes = currentPodcast.episode.mutableCopy() as NSMutableOrderedSet
    let btDict = dict as NSDictionary <---------------- Crash
        episodesToSave.title = btDict["title"] as String
        episodesToSave.summary = btDict["summary"] as String
        episodesToSave.link = btDict["link"] as String
        episodes.addObject(episodesToSave)
        currentPodcast.episode = episodes.copy() as NSOrderedSet

    }



    // Save
    var error:NSError?
    if !self.managedContext.save(&error) {
        println("could not save \(error)")
    }
}

请问有什么想法吗?

推荐答案

该错误表明您的数组不包含 NSDictionary 对象-这就是为什么在尝试访问时会得到动态强制转换异常的原因元素 as NSDictionary .

The error indicates that your array doesn't contain NSDictionary objects - that is why you get dynamic cast exception when you try and access an element as an NSDictionary.

从您的评论看来,您的数组似乎实际上包含 MWFeedItem 对象,因此您所需要做的就是更改代码以使用该对象类型,然后可以访问的属性.MWFeedItem -

From your comment it seems that your array actually contains MWFeedItem objects, so all you need to do is change your code to use that object type and then you can access the properties of the MWFeedItem -

@IBAction func subscribe(sender: AnyObject) {
        for item: MWFeedItem in episodesToDisplay {
    let episodesEntity = NSEntityDescription.entityForName("Episodes", inManagedObjectContext: self.managedContext)
    let episodesToSave = Episodes(entity: episodesEntity!, insertIntoManagedObjectContext: self.managedContext)
    var episodes = currentPodcast.episode.mutableCopy() as NSMutableOrderedSet
        episodesToSave.title = item.title
        episodesToSave.summary = item.summary
        episodesToSave.link = item.link
        episodes.addObject(episodesToSave)
        currentPodcast.episode = episodes.copy() as NSOrderedSet

    }

这篇关于将字典保存到核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆