I have just realized I lost 30 minutes searching in Xcode NSData Class reference how to do this in objc (sorry I explain this in C as this in the only language which comes without thinking too much):
#define MAX_SIZE_BUFFER 500
byte *ptr;
ptr = malloc(MAX_SIZE_BUFFER * sizeof(byte));
memset(ptr, 0, MAX_SIZE_BUFFER);
I started to code like this but never found out how to init MAX_SIZE_BUFFER and set all bytes to 0 in a smart way:
#define MAX_SIZE_BUFFER 500
NSData *ptr
ptr = [[[NSData] alloc] init]; // impossible to specify MAX_SIZE_BUFFER in the allocation.
Thus I told myself开发者_开发知识库, let's use a class method like:
+ data
+ dataWithBytes:length:
+ dataWithBytesNoCopy:length:
+ dataWithBytesNoCopy:length:freeWhenDone:
+ dataWithContentsOfFile:
+ dataWithContentsOfFile:options:error:
+ dataWithContentsOfMappedFile:
+ dataWithContentsOfURL:
+ dataWithContentsOfURL:options:error:
+ dataWithData:
but none of them enables to do the alloc and blank init symply.
For example: + dataWithBytes:length: requires that an alternate C buffer be created and given as a parameter to the method.
Should I consider myself like an idiot or like an objc bad programmer ?
Seriously, do you have a smart and simple method ?
Apple92
NSData is immutable, so that's why all the initializers require the contents of the buffer. What you want is NSMutableData's initWithLength:
http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSMutableData_Class/Reference/NSMutableData.html
精彩评论