OK, I have compiled up the following code at it works as expected.
FloatHolder.h
@interface FloatHolder : NSObject {
int _count;
float* _values;
}
- (id) initWithCount:(int)count;
// possibly look into this for making access shorter
// http://vgable.com/blog/2009/05/15/concise-nsdictionary-and-nsarray-lookup/
- (float)getValueAtIndex:(int)index;
- (void)setValue:(float)value atIndex:(int)index;
@property(readonly) int count;
@property(readonly) float* values; // allows direct unsafe access to the values
@end
FloatHolder.m
#import "FloatHolder.h"
@implementation FloatHolder
@synthesize count = _count;
@synthesize values = _values;
- (id) initWithCount:(int)count {
self = [super init];
if (self != nil) {
_count = count;
_values = malloc(sizeof(float));
}
return self;
}
- (void) dealloc
{
free(_values);
[super dealloc];
}
- (float)getValueAtIndex:(int)index {
if(index<0 || index>=_count) {
@throw [NSException exceptionWithName: @"Exception" reason: @"Index out of bounds" userInfo: nil];
}
return _values[index];
}
- (void)setValue:(float)value atIndex:(int)index {
if(index<0 || index>=_count) {
@throw [NSException exceptionWithName: @"Exception" reason: @"Index out of bounds" userInfo: nil];
}
_values[index] = value;
}
@end
then in your other application code you can do something like the following:
** FloatTestCode.h **
#import <Cocoa/Cocoa.h>
#import "FloatHolder.h"
@interface FloatTestCode : NSObject {
FloatHolder* holder;
}
- (void) doIt:(id)sender;
@end
** FloatTestCode.m **
#import "FloatTestCode.h"
@implementation FloatTestCode
- (id) init
{
self = [super init];
if (self != nil) {
holder = [[[FloatHolder alloc] initWithCount: 10] retain];
}
return self;
}
- (void) dealloc
{
[holder release];
[super dealloc];
}
- (void) doIt:(id)sender {
holder.values[1] = 10;
}