gpt4 book ai didi

ios - Swift 2/Xcode 7.0 - 条件绑定(bind)的初始值设定项必须具有可选类型而不是 'nsmanagedobjectcontext'

转载 作者:可可西里 更新时间:2023-11-01 00:52:25 25 4
gpt4 key购买 nike

刚刚从 Xcode 6.4 更新到 Xcode 7.0。在我的项目中,我现在遇到了一个错误,我整晚都在尝试解决这个错误,但没有成功。

错误信息是:条件绑定(bind)的初始化器必须有可选类型而不是'nsmanagedobjectcontext'

错误出现在 if let managedObjectContext = self.managedObjectContext { 在下面的代码行中两次

func preloadData () {
// Retrieve data from the source file
if let contentsOfURL = NSBundle.mainBundle().URLForResource("listofdata", withExtension: "csv") {

// Remove all the items before preloading
removeData()

var error:NSError?
if let items = parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding, error: &error) {
// Preload the items
if let managedObjectContext = self.managedObjectContext {
for item in items {
let listOfItem = NSEntityDescription.insertNewObjectForEntityForName("ListOfItem", inManagedObjectContext: managedObjectContext) as! ListOfItem
listOfItem.name = item.name
listOfItem.address = item.address
listOfItem.phone = item.phone

if managedObjectContext.save(&error) != true {
print("insert error: \(error!.localizedDescription)")
}
}
}
}
}
}

func removeData () {
// Remove the existing items
if let managedObjectContext = self.managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "ListOfItem")
var e: NSError?
let listOfItems = managedObjectContext.executeFetchRequest(fetchRequest, error: &e) as! [ListOfItem]

if e != nil {
print("Failed to retrieve record: \(e!.localizedDescription)")

} else {

for listOfItem in listOfItems {
managedObjectContext.deleteObject(listOfItem)
}
}
}
}

感谢所有帮助!谢谢!

更新:

更新后的代码看起来像这样,但是在第一个函数 preloadData 中仍然有这两个错误:

  1. 调用中缺少参数“error”的参数
  2. 条件绑定(bind)的初始化器必须是 Optional 类型,而不是 'NSManagedObjectContext'

    func preloadData () {
    // Retrieve data from the source file
    if let contentsOfURL = NSBundle.mainBundle().URLForResource("listofdata", withExtension: "csv") {

    // Remove all the menu items before preloading
    removeData()

    do {
    let items = try parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding)
    // Preload the items
    if let managedObjectContext = self.managedObjectContext {
    for item in items {
    let listOfItem = NSEntityDescription.insertNewObjectForEntityForName("ListOfItem", inManagedObjectContext: managedObjectContext) as! ListOfItem
    listOfItem.name = item.name
    listOfItem.address = item.address
    listOfItem.phone = item.phone

    if managedObjectContext.save() != true {
    print("insert error: \(error.localizedDescription)")
    }
    }
    }
    } catch let error as NSError {
    print("insert error: \(error.localizedDescription)")
    }
    }

这个函数显示没有错误

func removeData () {
// Remove the existing items
let fetchRequest = NSFetchRequest(entityName: "ListOfItem")

do {
let listOfItems = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [ListOfItem]
for listOfItem in listOfItems {
self.managedObjectContext.deleteObject(listOfItem)
}
}
catch let error as NSError {
print("Failed to retrieve record: \(error.localizedDescription)")
}

有什么帮助吗?谢谢!

最佳答案

在 Swift 2 中,Core Data 模板将 AppDelegate 中的属性 managedObjectContext 实现为非可选属性。可能更新程序相应地更改了实现。

好处是不再需要可选绑定(bind),但是您必须考虑新的错误处理,例如

func removeData () {
let fetchRequest = NSFetchRequest(entityName: "ListOfItem")

do {
let listOfItems = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [ListOfItem]
for listOfItem in listOfItems {
self.managedObjectContext.deleteObject(listOfItem)
}
}
catch let error as NSError {
print("Failed to retrieve record: \(error.localizedDescription)")
}
}

关于ios - Swift 2/Xcode 7.0 - 条件绑定(bind)的初始值设定项必须具有可选类型而不是 'nsmanagedobjectcontext',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com