我试图将我的Swift 1代码转换为Swift 2.1.1代码。 所以我想添加一个fetchRequest。在Swift中获取请求2.1.1
在夫特1 I这样做:
if let results = context.executeFetchRequest(fetchRequest, error:&error),
let managedObject = results.first as? NSManagedObject {
context.deleteObject(managedObject)
}
let saveError: NSError?
context.save(nil)
和
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]?
if let results = fetchedResults {
people = results
}
else {
print("Could not fetch \(error), \(error!.userInfo)")
}
夫特2.1(第二请求 - >不工作):
do {
let fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
// success if it gets here
if let results = fetchedResults {
people = results
}
} catch let error as NSError {
// failed so print error
print("Error: \(error.localizedDescription)")
}
错误管线( if let results... & let fetchedResults...
):
Initializer for conditional binding must have Optional type, not '[AnyObject]'
Call can throw, but is not marked with 'try' and the error is not handled
错误的行(let fetchedResults...
):
Call can throw, but is not marked with 'try' and the error is not handled
Cannot downcast from '[AnyObject]' to a more optional type '[NSManagedObject]?'
能否请你帮我翻译成斯威夫特2.1.1吗? 感谢您的帮助!
寻求帮助时会有所帮助,以显示你在哪里遇到问题。这段代码样本给了你什么错误信息,你认为这些意思是什么?你已经尝试了什么?否则很难知道什么可以帮助你,这可以看作是为你工作的请求。 – Jonah
我用更多的信息更新了我的问题。 – 123