Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 2-CoreData-NSManagedObjectContext无法调用';保存';具有类型为';(是否有错误?';_Ios_Core Data_Uiwebview - Fatal编程技术网

Ios Swift 2-CoreData-NSManagedObjectContext无法调用';保存';具有类型为';(是否有错误?';

Ios Swift 2-CoreData-NSManagedObjectContext无法调用';保存';具有类型为';(是否有错误?';,ios,core-data,uiwebview,Ios,Core Data,Uiwebview,我正试图遵循此,以便在我的应用程序中保存UIWebView的本地缓存 我不得不将几行代码转换为swift 2,但在添加到NSManagedObjectContext并保存NSError类型的参数时,我找不到解决问题的方法 从这张照片你可以更好地理解我的意思 我怎样才能解决这个问题? 我使用的代码是: func saveCachedResponse () { print("Saving cached response") // 1 let delegate = UIAp

我正试图遵循此,以便在我的应用程序中保存UIWebView的本地缓存

我不得不将几行代码转换为swift 2,但在添加到NSManagedObjectContext并保存NSError类型的参数时,我找不到解决问题的方法

从这张照片你可以更好地理解我的意思

我怎样才能解决这个问题? 我使用的代码是:

func saveCachedResponse () {
    print("Saving cached response")

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

    // 2
    let cachedResponse = NSEntityDescription.insertNewObjectForEntityForName("CachedURLResponse", inManagedObjectContext: context) as NSManagedObject

    cachedResponse.setValue(self.mutableData, forKey: "data")
    cachedResponse.setValue(self.request.URL!.absoluteString, forKey: "url")
    cachedResponse.setValue(NSDate(), forKey: "timestamp")
    cachedResponse.setValue(self.response.MIMEType, forKey: "mimeType")
    cachedResponse.setValue(self.response.textEncodingName, forKey: "encoding")

    // 3
    var error: NSError?
    let success = context.save(&error)
    if !success {
        print("Could not cache the response")
    }
}
在Swift 2.0中 要保存上下文,您必须使用
do{try…}
catch{…}
您的错误

// Save the context.
    do {
        try context.save()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //print("Unresolved error \(error), \(error.userInfo)")
    }
编辑 我在Xcode 7中使用了类似的代码,并且没有出现致命错误

do {
    let success = try context.save()
} catch {

}

我会成功的。该错误与Xcode 7无关

表面错误

句柄
打印(“无法缓存响应”)
未在其他答案中列出:

do {
    try context.save()
} catch let error {
    print("Could not cache the response \(error)")
}
避免数据库损坏,对所有MOC访问使用执行块

由于我喜欢代码示例具有一定的健壮性,这里有一个更详尽的解决方案:

func saveCachedResponse (context: NSManagedObjectContext) {
    context.performBlockAndWait({ () -> Void in

        let cachedResponse = NSEntityDescription.insertNewObjectForEntityForName("CachedURLResponse", inManagedObjectContext: context) as NSManagedObject
        cachedResponse.setValue(self.mutableData, forKey: "data")
        // etc.

        do {
            try context.save()
        } catch let error {
            print("Could not cache the response \(error)")
        }
    })
}

谢谢Antonio,我已更改为执行{try…}但是,我收到错误:致命错误:在展开可选值try:let context:NSManagedObjectContext=delegate.managedobjectcontext时意外发现nil。谢谢Antonio,仍然存在相同的问题。。然而,我解决了这个问题,用Xcode 6.4编译了我的项目。。。似乎Xcode 7 beta还没有准备好,yetI在我的回答中发布了一个编辑,试图解决您的错误。您如何将错误转发给saveCachedResponse的调用者?我在save()行得到“从抛出到非抛出的无效转换”。虽然我可以在这里回答这个问题,但评论并不是要问问题。请看,请详细说明你的答案
func saveCachedResponse (context: NSManagedObjectContext) {
    context.performBlockAndWait({ () -> Void in

        let cachedResponse = NSEntityDescription.insertNewObjectForEntityForName("CachedURLResponse", inManagedObjectContext: context) as NSManagedObject
        cachedResponse.setValue(self.mutableData, forKey: "data")
        // etc.

        do {
            try context.save()
        } catch let error {
            print("Could not cache the response \(error)")
        }
    })
}
do {
    try managedObjectContext.save()
} catch let error as NSError
{
    NSLog("Unresolved error \(error), \(error.userInfo)")
}