core-data - 将用户生成的图像保存到核心数据时出现运行时错误(Swift5)
问题描述
我正在尝试将用户生成的图像保存到核心数据。如果我对图像进行编程,则代码可以正常工作。但是当用户选择图像时,我收到错误Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value at jx = drawPlace.image!. 我只想解决这个问题,代码就可以工作了。仅将包装保存到核心数据中没有任何问题。
var jx = UIImage()
var drawPlace = UIImageView()
func save() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Item", in: managedContext)!
let item = NSManagedObject(entity: entity, insertInto: managedContext)
jx = drawPlace.image!
if let data = jx.pngData() {
item.setValue(data, forKey: "image")
}
解决方案
推荐阅读
- javascript - D3.js V4 Zoom 在 Angular 中不起作用
- c# - GMap MouseOver 似乎不起作用
- c# - 如何填写 DataTable 的第一列?
- windows-vista - 应用程序的系统范围配置文件的标准位置?
- amazon-dynamodb - 使用无服务器框架部署无服务器项目时如何运行 DynamoDB 脚本?
- google-api - 试驾 Speech-to-Text 异步操作 - 无结果
- asp.net-core - 启动完成后在哪里运行代码
- c# - Android - 广播中的共享首选项
- python - 代码在 Python 2 中有效,但在 Python3 中无效 TypeError: a bytes-like object is required, not 'str'
- angular - 带有打字稿的Angular版本6 Raygun设置问题