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

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

问题描述

我需要序列化我的NSURL。



对象是NSManagedObject的类型。

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

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



更新 p>

另一个问题被错误地作为注释:



[NSData dataWithContentsOfURL:uri]; [NSKeyedArchiver archivedDataWithRootObject:uri];



谢谢。

解决方案

首先,你的代码不会尝试序列化NSURL对象,它试图创建一个数据对象URL处的数据作为受管对象的URI返回。



其次,这永远不会工作。



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



URI仅允许受管对象上下文在其自己的存储中标识特定对象。 URI对于除上下文之外的任何其他东西都是乱码。



NSManagedObject未实现NSCoder协议,因此无法对受管对象进行序列化。我不知道你想在这里做什么,但你不能这样做。


I need to serialize my NSURL.

object is type of NSManagedObject.

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

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

Update

One more question which is put as a comment mistakenly:

What is the difference between [NSData dataWithContentsOfURL:uri]; and [NSKeyedArchiver archivedDataWithRootObject:uri];?

Thanks.

解决方案

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.

Secondly, that is never going to work.

[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.

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 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 into NSData(Cocoa错误256)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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