1

I packed an object (NSObject) to (NSData) and then encoded it with (NSASCIIStringEncoding) for sending it to a SQLite database with this code:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:canvasView.trazoYorch];

//convert NSData object to plain text for sending it to DB
NSData *data2 = [[NSString stringWithFormat:@"%@",data] dataUsingEncoding:NSASCIIStringEncoding];
NSString *dataStr = [[NSString alloc] initWithData:data2 encoding:NSASCIIStringEncoding];

everything works ok, but when I want to do the Inverse process NSString to NSData I got different results, this is my code for inverse process

NSString *FirmaString = [self traerFirmadeBD]; //returns the string content of DB       
NSData   *data2       = [FirmaString dataUsingEncoding:NSASCIIStringEncoding];
FirmaYorch *firmaCompleta = [NSKeyedUnarchiver unarchiveObjectWithData:data2];

Any help solving this I will appreciate

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jesús Ayala
  • 2,743
  • 3
  • 32
  • 48
  • What kind of data is in `canvasView.trazoYorch`? – rmaddy May 16 '13 at 17:34
  • is a NSObject, also implemented in it for being able to use NSKeyedArchiver – Jesús Ayala May 16 '13 at 17:36
  • 1
    Why do you wish to convert the archived object to an `NSString` for storage in the database? Why not store it as a blob? The way you convert `data` to a string is completely inappropriate. Using `stringWithFormat` must not be used for this purpose. – rmaddy May 16 '13 at 17:39
  • I used this method http://stackoverflow.com/questions/4767420/turning-nsdata-back-to-plain-text-nsstring?answertab=votes#tab-top with success, but it only explains how to do this in one way, if you can share the complete implementation for storing my object canvasView.trazoYorch as a blob (inverse and reverse) I will appreciate pal – Jesús Ayala May 16 '13 at 17:45
  • I'm thinking to make a temporary solution using a SQLite DB for a demo, the real one will use WebServices, that's why I was thinking of sending it as a String, still thinking in the proper solution, for your orientation and comments, thanks in advance folks – Jesús Ayala May 16 '13 at 17:57
  • 2
    It is wrong to convert an NSData object to a string using `stringWithFormat:`. The ONLY time this is OK is for printing out the `NSData` object for debugging purposes. A much better approach is to convert the `NSData` object to a string using a base64 encoder. The other question and answer you link to are wrong. The problem is that the `NSData` is converted to a string by calling the `description` method. The output is undocumented. The results could change. It's only for debugging. – rmaddy May 16 '13 at 18:03

0 Answers0