从这里抛出的错误不会在swift 2 xcode 7中处理

时间:2015-10-20 02:03:32

标签: ios xcode swift core-data

我在xcode 7中运行我的旧代码 " var fetchedObjects:[AnyObject]? = try context.executeFetchRequest(fetchRequest)"错误"从这里抛出的错误不被处理"如何解决?

   @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 个答案:

答案 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)
    }