Question

Trying to create a property, so I can set CGfloat array values on an instance of a class. Having a hard time figuring out how to do it. The CGFloat needs to have "size" [2] variable and numeric components like below:

CGFloat locations[2] = {1.0, 0.0};

Here is how it looks in the class itself (this works), before I created the property in an attempt to set these values from an instance (this creates a gradient BTW via drawRect in a UIView sunclass):

CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef myColorspace=CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {1.0, 0.0};
CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0,  0.5, 0.25, 1.0, 1.0 };
CGGradientRef myGradient = CGGradientCreateWithColorComponents(myColorspace, components,locations, num_locations);
CGContextDrawLinearGradient (context, myGradient, myStartPoint, myEndPoint, 0);

I tried creating a property like below in the .h

@property (nonatomic, assign) CGFloat locations;

and in the .m

@synthesize locations;

But I cant figure out how to properly set the values for the [size] and the components from the instance. Also I get errors when I set up the property: ERROR: "Passing 'CGFloat' (aka 'float') to parameter of incompatible type 'const CGFloat *' (aka 'const float *'); take the address with &"

you can get the project here is you feel like taking a look tinyurl.com/cmgy482 Any help much appreciated.

Was it helpful?

Solution 2

From another answer found here: "C arrays are not one of the supported data types for properties". I think that was my issue. The solution I used, is to make the property an NSMutableArray and to convert the values to a CGFloat.

.h

@property (nonatomic, retain) NSMutableArray *locations;

.m

CGFloat locationsFloat[[locations count]];

    for (int i = 0; i < [locations count]; i++) {
        NSNumber *number = [locations objectAtIndex:i];
        locationsFloat[i] = [number floatValue];        
    } 

viewController

- (void)viewDidLoad
{
    NSMutableArray *arrayPoints = [[NSMutableArray alloc]init];    
    [arrayPoints addObject:[NSNumber numberWithFloat:1.0]];
    [arrayPoints addObject:[NSNumber numberWithFloat:0.0]];

    [myInstance setLocations:arrayPoints];

...

OTHER TIPS

Ideally you would be able to just declare your property like this:

@property (nonatomic) CGFloat locations[2];

Alas, clang does not allow you to create a property with an array type.

One alternative is to use a pointer type instead. You probably want to write a comment to explain how many elements it points to:

#define kMyClassLocationCount 2

// This is an array of kMyClassLocationCount elements.  The setter copies
// the new elements into existing storage.
@property (nonatomic) CGFloat *locations;

To implement it, you need to create the instance variable and define the getter and setter:

@implementation MyClass {
    CGFloat _locations[kMyClassLocationCount];
}

- (CGFloat *)locations {
    return _locations;
}

- (void)setLocations:(CGFloat *)locations {
    memcpy(_locations, locations, kMyClassLocationCount * sizeof *_locations);
}

Note that you don't need @synthesize, because you have explicitly defined everything that @synthesize might generate.

When you use an instance of the class, you can read an individual location like this:

CGFloat location1 = myInstance.locations[1];

and you can set an individual location like this:

myInstance.locations[0] = 0.5;

and you can set all of the locations at once like this:

CGFloat newLocations[2] = { 7, 11 };
myInstance.locations = newLocations;

i think you had better code like this:

NSArray * locations = [[NSArray alloc ] initWithObjects:[NSNumber numberWithFloat:1.0f],[NSNumber numberWithFloat:0.0f],nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top