Search code examples
objective-ccocoapointerspropertieskey-value-coding

How to pass a float * array as a property?


I recently changed the type of one of my properties from @property (readonly) NSImage *someData to @property (readonly) float *someData. The data is a large array of floats. Before the change, the NSImage was created from that float data.

Since then, whenever I called [self didChangeValueForKey:@"someData"] in my code, I received the following error:

[MyController 0x109161710> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key someData.

All other properties continued to work just fine. Now one solution to this is the following piece of code:

- (id)valueForUndefinedKey:(NSString *)key {
    if ([key isEqualToString:@"someData"]) {
        return someData; // throws a warning because (float*) is not id
    } else {
        return [super valueForUndefinedKey:key];
    }
}

This works for now, but throws a warning about an incompatible pointer type returning 'float *' from a function with result type 'id'. So it can not be a final solution.

So the way I understand this is that you can not have a property of pointer type. Is that correct?
Also, I really want to pass that pointer through a property. Is there some way to wrap a pointer in some data structure that would make this work?

Edit: The float array can be a few 100 Mb in size, so I don't want to copy it around if I can help it. I suppose copying it into an NSArray would help, but that would definitely be bad for performance.


Solution

  • As per @BastiBechtold's comment below: Using an NSValue to point to the array is the best way to encapsulate the array of floats in an NSObject without copying it.