I was checking out this question which has this code
- (NSArray *) percentagesRGBArray:(float[]) rgbArray
{
NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
NSNumber *green = [NSNumber numberWithFloat:rgbArray[1] / 255];
NSNumber *blue = [NSNumber numberWithFloat:rgbArray[2] / 255];
NSNumber *alpha = [NSNumber numberWithFloat:rgbArray[3]];
return [NSArray arrayWithObjects:red, green, blue, alpha, nil];
}
and I thought, "that's terrible, what if you have more than three colors?" I know, you don't, but what if you did have count-1 colors and an alpha? Let's say you had [rgbArray count]
(does count even work for a real array?) Using only objective-C, what the normal way that you would return an NSArray of n
objects?
I just tried to work it out but I still don't have the chops to do this in objective-C. Here's my failed attempt:
- (NSArray *) what:(float[]) rgbArray
{
int len = sizeof(rgbArray)/sizeof(float); // made up syntax
NSLog(@"length is wrong dummy %d", len);
NSNumber *retVal[len];
for (int i=0;i<(len-1);i++) {
NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
retVal[i] = red;
[red release];
}
retVal[len-1] = [NSNumber numberWithFloat:rgbArray[len-1]];
return [NSArray arrayWithObjects:retVal count:len];
}