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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:07:31+00:00 2026-05-12T20:07:31+00:00

I pretty new to Objective-C (and C itself) and need to consume a NSData

  • 0

I pretty new to Objective-C (and C itself) and need to consume a NSData from a HTTP output. I’ve never really worked with byte arrays or had to worry about little/big endian issues, and have struggled a bit to write the following method to read a NSNumber with a specified length from that NSData.

- (NSNumber *)readNumberWithLength:(NSUInteger)length
{
    Byte k[length];
    [data getBytes:k range:NSMakeRange(offset, length)]; // big endian byte array representing a number
    offset += length;

    NSNumber *number;
    if (length==4) {
        number = [NSNumber numberWithUnsignedInt:CFSwapInt32BigToHost(*(uint32_t *)k)];
    } else if (length==2) {
        number = [NSNumber numberWithUnsignedShort:CFSwapInt16BigToHost(*(uint16_t *)k)];
    } else if (length==1) {
        number = [NSNumber numberWithUnsignedChar:*(uint8_t *)k];
    } else if (length==8) {
        number = [NSNumber numberWithUnsignedLongLong:CFSwapInt64BigToHost(*(uint64_t *)k)];
    } else {
        number = [NSNumber numberWithInt:0];
    }

    return number;
}

I have NSData *data and NSUInteger offset declared as instance variables.

Is this code correct? Is there anything I should worry about? I haven’t tested it on an actual device yet (only on the Simulator) and it seems to be working fine for me. Do you have any comments on it?

Thank you!

  • 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-12T20:07:32+00:00Added an answer on May 12, 2026 at 8:07 pm

    The code looks more or less correct.

    Seems like the hard way.

    I would be surprised — not totally shocked, but surprised — that the data coming back from the HTTP server is really just raw bytes. It would be exceptionally rare that it is and, in the case that it is, the design of the server is almost assuredly wrong. (There are certainly cases where binary-HTTP-response is the right answer, but it is very very rare).

    Unless you are talking about 100s of thousands of values in the data, the overhead implied by HTTP is almost certainly going to outweigh any gains of going binary. Parsing known numeric values from a string isn’t that onerous unless you are truly doing this constantly (which would make using HTTP not particularly attractive in the first place).


    Server design: fair enough. Another valid reason to deal with it is because you can’t change that particular piece of the puzzle.

    I would personally implement the code more defensively:

    • validate that the local data sizes are of the expected sizes compared to the remote / network sizes to prevent surprises (this could be assertions on initialization or in some debug code — I prefer being über defensive in situations like these).

    • use a case statement instead of the if/else if/else if/else sequence. Completely an aesthetic decision, but it would contribute to…

    • … deal with the length being an unexpected value by logging, even if only in debug mode. I would think an unexpected value size would be potentially very bad for overall correctness?

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

Sidebar

Related Questions

I'm pretty new to Objective-C, switching over from C++, so excuse my stupid question.
I'm pretty new to Objective C, and I'm having trouble reading values from a
I'm pretty new to Objective-C and Xcode, so it would be really helpful if
I'm pretty new to objective C started a few days ago... I want to
This is probably a completely stupid question, but i'm pretty new at objective-C and
I'm still pretty new to Objective-C coding (as evidenced by this question) and I
I'm pretty new to Objective-C and i have lots of troubles with memory management
I'm working in the IPhone SDK, and am pretty new to objective-c. Right now
I'm pretty much new to Objective C but I've had some experience in Visual
I'm pretty new to the Objective-C world and I have a long history with

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.