Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7745347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:59:32+00:00 2026-06-01T09:59:32+00:00

i have a NSData object. I want to convert it to a string, pass

  • 0

i have a NSData object.

I want to convert it to a string, pass to a function and then reconvert back to NSData object.

But How?

I tried this method, but NSData value it’s different from the original!

here my code:

  // a generic class
  APClass *c = [[APClass alloc] init];
  c.aNumber = 123;
  c.aString = @"my string";

  // my data obj
  NSMutableData *data = [NSMutableData data];

  // archiver to store class in nsdata
  NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  [encoder encodeObject:[NSNumber numberWithInt:c.aNumber] forKey:@"aNum"];
  [encoder encodeObject:c.aString forKey:@"aStr"];
  [encoder finishEncoding];
  [encoder release];

  [c release];

  NSLog(@"%@", data);
  NSString *d = [NSString stringWithFormat:@"%@", data];

  // ---

  NSString *strFromData = [NSString stringWithFormat:@"%@", d];

  NSData *dataNM = [strFromData dataUsingEncoding:NSUTF8StringEncoding];

  // decoder to retrieve class from nsdata      
  NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataNM];
  int number       = [[decoder decodeObjectForKey:@"aNum"] intValue];
  NSString *string = [decoder decodeObjectForKey:@"aStr"];
  [decoder finishDecoding];
  [decoder release];

  NSLog(@"[Number: %d] -- [String: %@]", number, string);

How can i convert back to original NSData?

data and dataNM are different in size.
Compiler give back this error:

2012-04-02 16:33:28.269 DataTest[18008:f803] -[__NSCFData
objectForKey:]: unrecognized selector sent to instance 0x6b46c80

2012-04-02 16:33:28.270 DataTest[18008:f803] * Terminating app due
to uncaught exception ‘NSInvalidArgumentException’, reason:
‘-[__NSCFData objectForKey:]: unrecognized selector sent to instance
0x6b46c80’

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:59:34+00:00Added an answer on June 1, 2026 at 9:59 am

    Solved.

    Using dataUsingEncoding, value of NSData it’s different.

    To pass data around methods or apps, etc, i’ve used base64 convertion.

    Encode

    NSString *d = 
      [NSString stringWithFormat:@"appdue://obj=%@", 
         [APBase64Converter base64forData:data]];
    

    Decode

    NSData *data = [APBase64Converter base64DataFromString:urlParams];
    

    APBase64Converter Is a lib that encode/decode easily data and strings.

    Working example and APBase64Converter lib can be downloaded from here: http://goo.gl/8YNjS

    Thanks all.

    I "retain" this post for helps people and me next time!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have UTF-8 encoded NSData from windows server and I want to convert it
I have an NSData object of approximately 1000kB in size. Now I want to
I have a NSData of bytes from where I want to subtract a string.
I have no idea how to properly 'return' this JSON object (in JavaScript): function
So I have the following code in a method which I want to set
I have a string of my url.. I want to get data from that
If have an array of char's pulled out of an NSData object with getBytes:range:
I have an object that I want to send to another device via bluetooth.
I have an NSDate object with value for example: 2011-08-26 14:14:51, I want to
I have strings with dates, and want to parse these into NSDate objects. Is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.