How to initialize NSData to store MAX_SIZE_BUFFER bytes?

I just realized that I lost a 30-minute search in the Xcode NSData Class, how to do it in objc (sorry, I explain it in C as it is in the only language that 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 writing code this way, but did not know how to run MAX_SIZE_BUFFER and set all bytes to 0 in an intelligent way:

 #define MAX_SIZE_BUFFER 500 NSData *ptr ptr = [[[NSData] alloc] init]; // impossible to specify MAX_SIZE_BUFFER in the allocation. 

So I told myself, let me use a class method, for example:

 + data + dataWithBytes:length: + dataWithBytesNoCopy:length: + dataWithBytesNoCopy:length:freeWhenDone: + dataWithContentsOfFile: + dataWithContentsOfFile:options:error: + dataWithContentsOfMappedFile: + dataWithContentsOfURL: + dataWithContentsOfURL:options:error: + dataWithData: 

but none of them allows you to execute the alloc and blank init command.

For example: + dataWithBytes: length: you want to create an alternative C buffer and set it as a parameter.

Should I consider myself an idiot or as a bad objc programmer?

Seriously, do you have a smart and easy method?

Apple92

+4
source share
1 answer

NSData is unchanged, so all initializers require buffer contents. You want NSMutableData initWithLength:

http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSMutableData_Class/Reference/NSMutableData.html

+6
source

All Articles