从核心数据中获取自定义对象到数组中

fetching a custom object from core data into an array

我无法从核心数据中获取我的对象。对象看起来像这样:

class cilj: NSObject {
var imeCilja: String
var slikaCilja: String }

我的实体名为实体,有两个属性 "tekst" 和 "slika",都是字符串类型。我的保存功能是这样的:

func saveImage(goalName:String, imageName:String) {

  let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    let entityDescription =  NSEntityDescription.entityForName("Entity", inManagedObjectContext:managedContext)

    let thingToSaveToCD = NSManagedObject(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)

    thingToSaveToCD.setValue(globalGoalTitle, forKey: "tekst")
    thingToSaveToCD.setValue(globalGoalImagePath, forKey: "slika")

    do {
        try managedContext.save()
        print("managed to save to core data")
        //5
       // listaObjekata.append(cilj5) as! [cilj]
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    }

}

我使用 alertController 来选取文本,并使用 imagePicker 来选取图像,然后将其存储在文档中并获取路径。我将这两个存储在上面代码中可见的全局变量中。

我的获取函数是:

func coreDataFetch(){

    //core data fetch

    //1
    let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    //2
    let fetchRequest = NSFetchRequest(entityName: "Entity")


    do {
        let fetchedResults =  try managedContext.executeFetchRequest(fetchRequest) as! [cilj]
        listaObjekata = fetchedResults


    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }

}

我已经阅读了 ray wenderlich 关于 Core Data 的文章、Apple 的文档和几个 YT 视频,但我似乎仍然遗漏了一些东西。我将不胜感激任何帮助。谢谢!

编辑 - 这是我的 cellForRowAtIndexPath

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


    let cell = tableView.dequeueReusableCellWithIdentifier("myCell") as! cellController
    let ciljZaPrikaz = listaObjekata[indexPath.item]

    cell.labelText.text = ciljZaPrikaz.imeCilja ?? "no text found"

    let path = getDocumentsDirectory().stringByAppendingPathComponent(ciljZaPrikaz.slikaCilja)
    cell.imageToShow.image = UIImage(contentsOfFile: path)

    return cell
}

您做的是正确的,只是缺少对模型的类型转换。尝试以下:

    let fetchedResults =  try managedContext.executeFetchRequest(fetchRequest)
    if let results = fetchResults where results.count > 0 {
        let entityModel = results[0] as? Entity
        let tekst = entityModel.tekst
    }

我还想补充一下我如何从核心数据遍历 return 对象:

 do {
        let fetchedResults =  try managedContext.executeFetchRequest(fetchRequest)
         listaObjekata.removeAll()
        for i in 0...(fetchedResults.count-1) {
             let enityModel = fetchedResults[i] as? Entity

                let thisToArray = cilj(imeCilja: (enityModel?.tekst!)!, slikaCilja: (enityModel?.slika!)!)
                listaObjekata.append(thisToArray)

        }

    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }