2016-08-16 73 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)") 
    }