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.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87

3 Answers3

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
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.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • 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 04 '13 at 01:07
  • And you can wrap them in NSValue – uchuugaka Jul 04 '13 at 04: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.

kubi
  • 48,104
  • 19
  • 94
  • 118
  • 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 '10 at 16:06