ios - How to save and fetch VideoURL in iOS swift using core data?
问题描述
I have a videoURL like file:///var/mobile/Containers/Data/Application/3C80B7F1-F0AA-4723-B7F5-E7AC5E71CA09/Documents/2018-Nov-12%2014:09:01output.mov
I am saving this videoURL by using below code as converting to DATA.
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Device", in: context)
let newEntity = NSManagedObject(entity: entity!, insertInto: context)
self.videoData = NSData(contentsOf: self.videoURL as URL)! as Data
newEntity.setValue(self.videoData, forKey:"videoURL")
do {
try context.save()
print("saved")
} catch {
print("not saved")
}
but I am unable to converting this data to url while fetching. Could anyone guide me how to do this task?
解决方案
You are saving the loaded video data, not the URL.
Set the type of the attribute videoURL
to URI
and the type of the corresponding property to URL
and save self.videoURL
By the way, don't use NS
classes like NSURL
and NSData
, use URL
and Data
and it's highly recommended to use dot notation rather than KVC (value(forKey:)
when accessing NSManagedObject
properties
let newEntity = NSManagedObject(entity: entity!, insertInto: context) as! Device
self.videoData = Data(contentsOf: self.videoURL)!
newEntity.videoURL = self.videoURL
do {
try context.save()
print("saved")
} catch {
print(error)
}
推荐阅读
- javascript - 用ajax加载一个光滑的幻灯片轮播
- python - 在标准化图中可视化值
- c++ - c++ 从 String 到 Bitboard 的转换和反向优化
- sparql - 使用 SPARQL 查询按 GDP 重新创建 Wikipedia 州列表
- mvvm - DataGrid 中的 RichTextBox 更新 (mvvm)
- python - 将数组的数据类型从 double 更改为 int 的问题
- windows - 如何查看哪些用户正在调用 Windows 服务托管的 WCF Web 服务?
- android - 从 Android 前台服务迁移到 WorkManager
- c - 如何分配一个指向列表的指针并在c中打印出来?
- solidity - 为什么我的 Solidity 构造函数需要两个未在参数中指定的变量?