NSURL 转换为 NSData(Cocoa 错误 256.) [英] NSURL into NSData (Cocoa error 256.)

查看:14
本文介绍了NSURL 转换为 NSData(Cocoa 错误 256.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要序列化我的 NSURL.

I need to serialize my NSURL.

object 是 NSManagedObject 的类型.

object is type of NSManagedObject.

NSURL *objectURIRepresentation = [[object objectID] URIRepresentation];
NSError *error = nil;
NSData *objectIDData = [NSData dataWithContentsOfURL:objectURIRepresentation options:NSDataReadingMapped error:&error];

我收到错误:(Cocoa 错误 256.).有任何想法吗?有人告诉我,使用 dataWithContentsOfURL: 不是个好主意.

I get error: (Cocoa error 256.). Any ideas? Something tells me, using dataWithContentsOfURL: is not good idea.

更新

还有一个问题被错误地当作评论:

One more question which is put as a comment mistakenly:

[NSData dataWithContentsOfURL:uri];[NSKeyedArchiver archivedDataWithRootObject:uri]; 有什么区别?

谢谢.

推荐答案

首先,您的代码不会尝试序列化 NSURL 对象,它会尝试从作为 URL 的 URL 返回的数据中创建数据对象管理对象.

Firstly, your code does not attempt to serialize a NSURL object, it attempts to create a data object out of the data at the URL returned as the URI of a managed object.

其次,这永远行不通.

[NSData dataWithContentsOfURL:] 将尝试读取特定 URL 上的文件.托管对象的 URI 表示一个对象与许多其他对象一起存储在一个持久文件(如 SQLite 数据库)中.

[NSData dataWithContentsOfURL:] will try to read a file at a particular URL. The URI of a managed object represents an object stored in pieces with many others inside a persistent file like a SQLite database.

URI 仅允许托管对象上下文在其自己的存储中识别特定对象.URI 对上下文以外的任何其他事物都是胡言乱语.

The URI only allows a managed object context to identify a particular object in its own store. The URI is gibberish to anything else other than the context.

NSManagedObject 未实现 NSCoder 协议,因此无法序列化托管对象.我不确定你想在这里做什么,但你不能这样做.

NSManagedObject does not implement the NSCoder protocol so managed objects cannot be serialized. I'm not sure what you want to do here but you can't do it this way.

这篇关于NSURL 转换为 NSData(Cocoa 错误 256.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆