Swift: Failed to pass value of type NSManagedObject_ to 'dataModel.Entity'

I don’t know what I should explain or not, feel free to ask me for more code or explanations if necessary .. I am trying to use CoreData to store data received from an HTTP POST request and then print it to a UITableView.

I successfully receive data from JSON and send it to the database. The problem is that I am trying to send data from a database to a UITableView.

This is my first time with basic data, so to understand how it works, I followed this guide, which I adapted to my situation: https://www.youtube.com/watch?v=UniafUWsvLg

This is the object I'm working in:

import Foundation
import CoreData

class Task: NSManagedObject {

    @NSManaged var summary: String
    @NSManaged var status: String
    @NSManaged var responsable: String
    @NSManaged var id: String
    @NSManaged var detail: String
    @NSManaged var date: String
    @NSManaged var context: String

}

This is part of the code that prepared the work on CoreData, I have some comments on it:

//Preparing variables used to get and send datas from DB
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var nTask: Task? = nil
var frc : NSFetchedResultsController = NSFetchedResultsController()

func getFetchedResultsController() -> NSFetchedResultsController{
    frc = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: context!, sectionNameKeyPath: nil, cacheName: nil)
    return frc
}
func taskFetchRequest() -> NSFetchRequest {
    //On which Entity are we working?
    let fetchRequest = NSFetchRequest(entityName: "Task")
    //Which attribute get the Order by. There summary as Ascending
    let sortDescriptor = NSSortDescriptor(key: "summary", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}

, viewDidLoad getFetchedResultsController :

override func viewDidLoad() {
    super.viewDidLoad()

    frc = getFetchedResultsController()
    frc.delegate = self
    frc.performFetch(nil)
}

:

//Link creation to SQLite DB
        let context = self.context
        let ent = NSEntityDescription.entityForName("Task", inManagedObjectContext: context!)
        let nTask = Task(entity: ent!, insertIntoManagedObjectContext: context)

nTask , JSON, :

for dict in json2 {
    var apps = [String]()


    if let summary = dict["summary"] as? String{
        nTask.summary = summary
    }


    if let description = dict["description"] as? String{
        nTask.detail = description
    }


    if let context = dict["context"] as? String{
        nTask.context = context
    }


    if let due = dict["due"] as? String {
        nTask.date = due
    }

    if let status = dict["status"] as? String{
        nTask.status = status
    }

    if let responsible = dict["responsible"] as? String{
        nTask.responsable = responsible
    }

    if let id = dict["id"] as? String{
        nTask.id = id
    }

}
context?.save(nil)
println(nTask)
self.tableView.reloadData()

TableView, cellForRowAtIndexPath numberOfRowsInSection, :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("customTableViewCell") as! UITableViewCell
    let task = frc.objectAtIndexPath(indexPath) as! Task
    cell.textLabel?.text = task.summary
    var detail = task.detail
    var context = task.context
    var due = task.date
    var status = task.status
    var responsible = task.responsable

    cell.detailTextLabel?.text = "Contexte: \(context), Detail: \(detail), Status: \(status), Ending date: \(due)"

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let numberOfRowsInSection = frc.sections?[section].numberOfObjects
    return numberOfRowsInSection!

}

let task = frc.objectAtIndexPath(indexPath) as! Task cellForRowAtIndexPath.

: Could not cast value of type 'NSManagedObject_Task_' (0x79ebd190) to 'TaskManager.Task' (0xa1f08).

. , ...

, , , , .

, , .

.

Edit:

, , , . , ... @objc(Task) Task, DataModel Task, , NSManagedObjectModel let modelURL = NSBundle.mainBundle().URLForResource("TaskManager", withExtension: "momd")!, url let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("TaskManager.sqlite") AppDelegate..

.

+4
2

Try:

let task = frc.objectAtIndexPath(indexPath) as NSManagedObject

, "cellForRowAtIndexPath", "FOR":

for dict in json2 {
...
if let summary = json2["summary"] as? String{
    nTask.summary = summary
}
...

"summary" of "dict", "json2"

+1

, @objc(NameOfClass) . !

0

All Articles