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 7574383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:25:23+00:00 2026-05-30T16:25:23+00:00

There are different approaches of converting NSString to NSData . NSString *req_string = @I

  • 0

There are different approaches of converting NSString to NSData .

NSString *req_string = @"I am a NSString.";
NSData *req_data = [NSData dataWithBytes:[req_string UTF8String] length:[req_string length]];
// or
NSData *req_data_alt = [req_string dataUsingEncoding:NSUTF8StringEncoding];

Any difference between these 2 approaches?

  • 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-05-30T16:25:25+00:00Added an answer on May 30, 2026 at 4:25 pm

    The latter (NSData * data = [string dataUsingEncoding:NSUTF8StringEncoding]) is the option I would recommend.

    Why?

    Some people will say efficiency. In this case, using the string instance method to create a NSData object requires only a single obj-c message dispatch to Apple’s code, which is highly optimized. In the other case (creating a new NSData object using the class method) would require 2 message dispatches to your string object and 1 message dispatch to the NSData class object.

    However, the runtimes are unlikely to vastly differ, and even if they do, the cost of the encoding is going to be based on the length of the string, and not what methods you use to create the NSData object.

    I would argue the real reason you would want to use the instance method on NSString is semantics and clarity.

    Let’s consider an pseudo-english translation of these options:

    • [string DataUsingEncoding:NSUTF8StringEncoding]: Hey, string, I want you to give me a NSData copy of yourself using UTF8 encoding. Ok, thanks, put that right over there – no, not on the rug.
    • [NSData dataWithBytes:[req_string UTF8String] length:[req_string length]]: String! Give me all your UTF8 bytes. yeah, oh I need your length, too. Sec. NSData, get over here, I need you to pick up this stuff string is leaving on my doorstep and turn it into a data objec- hey, string, wait, gently! Don’t break anything”

    Which seems clearer to you?

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

Sidebar

Related Questions

I'm sure there are different approaches to this problem, and I can think of
is there any different in Connection string between SQL server Authentication and Windows Authentication
In javascript, is there any different between these two: // call MyFunction normal way
Although I realise there are different approaches to securing upload process, I'm still confused
Is there any difference between these tow pieces of code & which approach is
Are there different limitations as to how many connections (sockets) that can be created
In x86 GNU Assembler there are different suffixes for memory related operations. E.g.: movb,
Is this fine or is there a different way? <a href=Http://<%=Html.Encode(Model.Website)%>><%=Html.Encode(Model.Website??)%></a>
Greetings, Is there a different between using Ajax and jquery to do input valuation
I have a collection of MDI forms, where there are different objects (Form1,Form2 etc).

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.