[英]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
我有一个videoURL像
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. 我通过使用以下代码将其转换为DATA来保存此videoURL。
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. 但我无法在获取时将这些数据转换为url。 Could anyone guide me how to do this task?
谁能指导我如何完成这项任务?
You are saving the loaded video data, not the URL. 您正在保存加载的视频数据,而不是URL。
Set the type of the attribute videoURL
to URI
and the type of the corresponding property to URL
and save self.videoURL
将属性
videoURL
的类型设置为URI
,将相应属性的类型设置为URL
并保存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 顺便说一句,不要使用
NSURL
和NSData
类的NS
类,而要使用URL
和Data
,强烈建议在访问NSManagedObject
属性时使用点表示法而不是KVC (value(forKey:)
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)
}
Sorry to say but this is wrong. 很抱歉,但这是错误的。 why do you want to convert URL to data?
为什么要将URL转换为数据? You just save URL as string then fetch this string and work with the URL
您只需将URL保存为字符串,然后获取此字符串并使用URL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.