ios - 核心数据 : error return nil when trying to fetch

标签 ios swift core-data

我正在尝试获取之前保存的数据,但事情没有按预期进行。当我尝试打印结果时,出现以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
2017-02-26 15:32:32.596867 Money Manager[2047:475120] fatal error: unexpectedly found nil while unwrapping an Optional value

我找不到任何对我有用的答案......

这是我尝试获取时的代码:

func getExpensesData() {
    let fetchRequest: NSFetchRequest<Activity> = Activity.fetchRequest()
    do {
        let results = try context.fetch(fetchRequest) as [Activity]
        activities = results
        for activity in activities {
            // I get the error I mentionned just the line below
            print(activity.title!)
        }
    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }
}

其中 Activity 是一个实体。

这也是我保存数据时的代码:

func saveNewContent() {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let entity: NSEntityDescription = NSEntityDescription.entity(forEntityName: "Activity", in: context)!
    let newActivity = NSManagedObject(entity: entity, insertInto: context) as! Activity

    if checkFieldsAreValid() {
        newActivity.isExpense = expenseBenefitSwitch.isOn
        newActivity.isRecurrent = recurrentSwitch.isOn
        newActivity.amount = amountView.text
        newActivity.title = titleView.text
        do {
            try context.save()
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }
    }
}

很抱歉我可能犯了一些错误,这是我在这里发表的第一篇文章。如果您需要其他代码,请告诉我。

提前致谢!!

更新 #1:

运行其他几个测试后,我发现我尝试保存的数据,即 newActivity,在保存之前看起来是空的......

当我在前面放置一个断点

try context.save()

这是我得到的: Structure

更新#2:

经过更深入的搜索,我发现问题似乎与 CoreData 错误有关,因为当我尝试获取对象的某个属性时会发生错误。

当我打印获取的对象之一时,我得到:

<Activity: 0x170284dd0> (entity: Activity; id: 0xd000000000040000 <x-coredata://
68148348-928B-43BE-901C-2073ABFA46EF/Activity/p1> ; data: <fault>)

这是我尝试获取属性时使用的代码(在 UITableViewCell 的自定义子类中使用):

func initialize(withContent activity: Activity) {
    print(activity)
    titleLabel.text = (activity.value(forKey: "title") as! String)
}

最佳答案

好吧,在对我的应用程序进行越来越深入的投资之后,我发现错误不是因为 CoreData 而引发的,而是因为自定义的 UITableViewCell 我忘记了正确初始化......因此,当我尝试这样做时:

textLabel.text = activity.title

它引发错误是因为 UILabel,而不是 Activity...

非常感谢您的帮助,我会记录您的建议!

关于ios - 核心数据 : error return nil when trying to fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42470016/

相关文章:

swift - 仅在 IOS13 的设备上我无法获取 OneSignal 的唯一用户 ID

ios - 核心蓝牙 : didWriteValueFor() is not called after writeValue()

ios - 如何在 CoreData 的 NSLog 中显示详细信息?建议我一个解决方案

ios - 核心数据 : ObjectID Error: "No known class method for selector ' managedObjectContext'

ios - 德尔福 Firemonkey IOS TContextOpenGL.DestroyPixelShader EXC_BAD_ACCESS

ios - 如何创建一个包含 2 个 iOS 项目和一个 iOS 动态框架项目的 Xcode 6 工作区?

iphone - 在 Interface Builder 中创建的 UITableViewCell segue 不会触发

iphone - 如何根据 UILabel 中的数字显示文本

ios - UIView.animateWithDuration 在 UITableviewCell 中不起作用

ios - 核心数据是否支持自定义域对象模型?