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

时间:2016-08-16 19:41:49

标签: ios swift fetch core

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

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {

            finish();
            overridePendingTransition(R.transition.right_in, R.transition.right_out);


        }
    //Hide keyboard when button was clicked.
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return super.onOptionsItemSelected(item);
    }

我的实体名为Entity,有两个属性“tekst”和“slika”,两者都是String类型。我的保存功能是:

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

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

我的获取功能是:

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

}

我一直在阅读Ray Wenderlich关于Core Data,Apple的文档,几个YT视频的文章,但我似乎仍然遗漏了一些东西。我非常感谢任何帮助。谢谢!

编辑 - 这是我的cellForRowAtIndexPath

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

}

2 个答案:

答案 0 :(得分:1)

您正在做正确但只是缺少类型转换到您的模型。请尝试以下:

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

答案 1 :(得分:0)

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

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