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



我有麻烦从core data获取我的对象。对象看起来像这样:

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

我的实体名为entity,有两个属性"tekst"one_answers"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关于核心数据的文章,苹果的文档,几个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
    }

我还想添加如何从核心数据遍历返回对象:

 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)")
    }

相关内容

  • 没有找到相关文章

最新更新