NSMutableArray *count_id = [NSMutableArray new];
// Can this be done better for an initialization?
for (int i; i < 256; i++) {
[count_id addObject:[NSNumber numberWithInt:0]];
}
NSData *data = [NSData dataWithContentsOfFile:fileName];
for (size_t dgStart = 0; dgStart < data.length; ) {
unsigned char id;
unsigned int size;
[data getBytes:&size range:NSMakeRange(dgStart, 4)];
[data getBytes:&id range:NSMakeRange(dgStart+5, 1)];
//
// Yuck! I just want to increment the number by 1. How do I improve this?
//
[count_id replaceObjectAtIndex:id withObject: [NSNumber numberWithInt:[[count_id objectAtIndex:id] integerValue]+1 ] ];
// Jump to the next packet
dgStart += size+4;
}
Wednesday, August 4, 2010
A cleaner way to have an NSMutableArray of counters?
How do I make this clean. I know how to make this really simple with a C array or a C++ vector, but I'm trying to learn ObjC! Any help greatly appreciated.
Subscribe to:
Post Comments (Atom)
If the number of counters is not going to change, which seems to be the case since you're using i < 256 in your for loop, you'd rather use a normal C-array because an NSMutableArray of NSNumber is overkill.
ReplyDeleteAnd no, there's no way to make that counter work better...
Note, you should use the PSYDataScanner class I gave you to scan your NSData, it's made for that...
Thanks Remy. I've been looking at your scanner. This is more about me learning than at this point.
ReplyDeleteAnd for anyone else reading this, Remy's code is here:
http://gist.github.com/507321