2015-10-20 30 views
0

我運行我的舊代碼在xcode 7 「var fetchedObjects:[AnyObject]?=嘗試context.executeFetchRequest(fetchRequest)」有錯誤與「從這裏拋出的錯誤沒有處理「如何解決它?從這裏拋出的錯誤不是在迅速處理2 xcode 7

@IBAction func SaveImage(sender: AnyObject) { 

    let app = UIApplication.sharedApplication().delegate as! AppDelegate 
    let context = app.managedObjectContext 

    var error:NSError? 

    //聲明數據的請求 
    var fetchRequest:NSFetchRequest = NSFetchRequest() 
    fetchRequest.fetchLimit = 10 //限定查詢結果的數量 
    fetchRequest.fetchOffset = 0 //查詢的偏移量 

    //聲明一個實體結構 
    var entity:NSEntityDescription? = NSEntityDescription.entityForName("User", 
     inManagedObjectContext: context) 
    //設置數據請求的實體結構 
    fetchRequest.entity = entity 

    //設置查詢條件 
    let predicate = NSPredicate(format: "id= '1' ", "") 
    fetchRequest.predicate = predicate 

    //查詢操作 
    var fetchedObjects:[AnyObject]? = try context.executeFetchRequest(fetchRequest) 

    //遍歷查詢的結果 
    for info:Content in fetchedObjects as! [Content]{ 
     println("id=\(info.id)") 
     println("username=\(info.username)") 
     println("password=\(info.password)") 
    } 
} 
+1

看看在這個問題的答案:http://stackoverflow.com/questions/32997955/error-call-can-throw-但沒有標記出來,而且這個問題與Xcode,核心數據或iOS有什麼關係?32998154#32998154 –

+0

這個問題與Xcode,核心數據或iOS有什麼關係?我試圖編輯刪除這些標籤,但後來迫使我編輯內容,所以我放棄了。 –

+0

@Jody Hagins我想保存數據在coredata.but swift2.0有改變 –

回答

0

我解決它,使用此代碼

do { 
    let fetchedObjects = try context.executeFetchRequest(fetchRequest) 

    //遍歷查詢的結果 
    for info:Content in fetchedObjects as! [Content]{ 
     print("id=\(info.contentName)") 
     info.photo_1 = UIImagePNGRepresentation(imageHolder.image!) 
     //item!.photo_1 = UIImagePNGRepresentation(imageHolder.image!) 
     } 

     try self.moc.save() 
    } catch { 
     print(error) 
    }