user2747220
user2747220

Reputation: 893

Saving a Dictionary to 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)")
    }
}

Any ideas please?

Upvotes: 0

Views: 3387

Answers (1)

Paulw11
Paulw11

Reputation: 114846

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.

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

    }

Upvotes: 1

Related Questions