2016-08-16 71 views
0

我在从核心数据中提取我的对象时遇到问题。对象是这样的:从核心数据中将自定义对象提取到数组中

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

我的实体被称为实体,并有两个属性“tekst”和“SLIKA”,无论是String类型。我保存FUNC是这样的:

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

} 

我已经通过射线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 
} 
+0

你试试这个? 'let fetchedResults =尝试managedContext.executeFetchRequest(fetchRequest)as! [实体]' – Santosh

+0

嘿,是的,我有,然后它在它下面的线上说“不能指定类型[实体]类型的值键入[cilj]。 – user3739902

+0

您正在使用'Entity'进行保存并用'Entity'进行提取,然后为什么数组是'[cilj]'?只要尝试打印'fetchedResults'并检查你所得到的结果是什么?删除'as![Entity]'或'as![cilj]'并尝试。 – Santosh

回答

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