Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURL into NSData (Cocoa error 256.)

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.

like image 951
user500 Avatar asked Feb 24 '11 11:02

user500


3 Answers

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.

like image 107
TechZen Avatar answered Sep 30 '22 12:09

TechZen


As mentioned here Cocoa error 256 core data
error code 256 can occur when an unknown error is occurred in reading the resource or the path has some encoded characters in it.

What it seems to me is you are trying to get the data from NSManagedObject. Hence as @fluchtpunkt suggested you should look for http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html

Now coming to your second question [NSData dataWithContentsOfURL:uri]; returns data for a web url or a local resource in your documents directory. While [NSKeyedArchiver archivedDataWithRootObject:uri]; returns the NSData object containing the encoded form of the object graph whose root object is given.

like image 39
Madhup Singh Yadav Avatar answered Sep 30 '22 14:09

Madhup Singh Yadav


try to add "Http://" before in the link

like image 34
mohammad alabid Avatar answered Sep 30 '22 13:09

mohammad alabid