0

In Java, I would pass bytes around as byte[] which also knows it's own length. is there an equivalent array in Objective C?

I'm not sure, but NSArray seems too heavy weight for something like that as it can hold any type.

3 Answers 3

2

I think the closest thing is NSMutableData and its mutableBytes property. That'll give you a C style array with a length count attached. The only slight complexity is that while you can read and modify entries by mutableBytes, you need to use appendBytes:length: to add extra bytes onto the end should you want the buffer to grow.

0
0

I think Tommy has the correct answer, but it's worth noting that you can also use the primitive C data types (i.e., char* in this case). When things get very low level -- as might be the case when you're talking about individual bytes -- avoiding some of the cleverer stuff may perform better.

2
  • I would agree; recently I needed to keep track of a large array of BOOL values representing pixel locations, and found that having a two dimensional array of bytes was significantly faster than using NSArray or NSDictionary for the same purpose.
    – sapi
    Jul 4, 2013 at 1:07
  • And you can wrap them in NSValue
    – uchuugaka
    Jul 4, 2013 at 4:45
0

I also think @Tommy has the correct answer, but it's also worth considering that the built in storage classes are heavily optimized. Unless you are confident that performance will be an issue, it may be easier to use NSMutableArray and only move away after you've shown that you're having performance issues. If you need to store primitive values, take a look at CFMutableArray.

1
  • 3
    I don't think NSMutableArray is suitable for storing arrays of byte values. Each byte would have to be wrapped in an NSValue in order to go into the array. NSData or NSMutableData is the right thing here.
    – JeremyP
    Nov 22, 2010 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.