Cocoa sample code: Storing a float array in a keyed archive
/*
IMPORTANT NOTE: THIS CODE IS NOT SAFE ACROSS INTEL/PPC PROCCESSORS. I’LL BE POSTING AN ALTERNATIVE OPTION SOON.
Some samplecode that shows how to store a C array of floats (or any other kind with a little change to this code) in a keyed archive for Cocoa. I struggled quite a bit to get it right. I noticed there wasn’t many information about this to be found on the internet. I hope this’ll help someone out. :-)
*/
-(id)init {
if (self = [super init]) {
arrayOfFloats = (float *) malloc(1*sizeof(float));
numberOfPoints = 1;
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder {
if ([coder allowsKeyedCoding]) { // Assuming 10.2 is quite safe!!
[coder encodeInt:numberOfPoints forKey:@"numberOfPoints"];
[coder encodeBytes:(void *)arrayOfFloats length:numberOfPoints*sizeof(float) forKey:@"arrayOfFloats"];
}
return;
}
- (id)initWithCoder:(NSCoder *)coder {
if ([coder allowsKeyedCoding]) {
numberOfPoints = [coder decodeIntForKey:@"numberOfPoints"];
const uint8_t *temporary = NULL; // Pointer to a temporary buffer returned by the decoder
unsigned int length; // Will hold the length of the returned bytes, not used here, as we separately encoded the length of the array
arrayOfFloats = (float *) malloc(1*sizeof(float)); // since the accessor uses realloc, we need to malloc masses at least one time
temporary = [coder decodeBytesForKey:@"arrayOfFloats" returnedLength:&length];
[self setArrayOfFloats:temporary withCount:numberOfPoints];
}
return self;
}
-(void)setArrayOfFloats:(float *)inArray withCount:(int)inValue {
numberOfPoints = inValue;
arrayOfFloats = (float *) realloc(arrayOfFloats, numberOfPoints*sizeof(float));
memcpy(arrayOfFloats, inArray, numberOfPoints*sizeof(float));
}
IMPORTANT NOTE: THIS CODE IS NOT SAFE ACROSS INTEL/PPC PROCCESSORS. I’LL BE POSTING AN ALTERNATIVE OPTION SOON.
Some samplecode that shows how to store a C array of floats (or any other kind with a little change to this code) in a keyed archive for Cocoa. I struggled quite a bit to get it right. I noticed there wasn’t many information about this to be found on the internet. I hope this’ll help someone out. :-)
*/
-(id)init {
if (self = [super init]) {
arrayOfFloats = (float *) malloc(1*sizeof(float));
numberOfPoints = 1;
}
return self;
}
-(void)encodeWithCoder:(NSCoder *)coder {
if ([coder allowsKeyedCoding]) { // Assuming 10.2 is quite safe!!
[coder encodeInt:numberOfPoints forKey:@"numberOfPoints"];
[coder encodeBytes:(void *)arrayOfFloats length:numberOfPoints*sizeof(float) forKey:@"arrayOfFloats"];
}
return;
}
- (id)initWithCoder:(NSCoder *)coder {
if ([coder allowsKeyedCoding]) {
numberOfPoints = [coder decodeIntForKey:@"numberOfPoints"];
const uint8_t *temporary = NULL; // Pointer to a temporary buffer returned by the decoder
unsigned int length; // Will hold the length of the returned bytes, not used here, as we separately encoded the length of the array
arrayOfFloats = (float *) malloc(1*sizeof(float)); // since the accessor uses realloc, we need to malloc masses at least one time
temporary = [coder decodeBytesForKey:@"arrayOfFloats" returnedLength:&length];
[self setArrayOfFloats:temporary withCount:numberOfPoints];
}
return self;
}
-(void)setArrayOfFloats:(float *)inArray withCount:(int)inValue {
numberOfPoints = inValue;
arrayOfFloats = (float *) realloc(arrayOfFloats, numberOfPoints*sizeof(float));
memcpy(arrayOfFloats, inArray, numberOfPoints*sizeof(float));
}