C-A
C-A

Reputation: 699

Calling a method within a method (UIColor). Beginner

I am basically trying to make a method that enables me to get random colors for drawing. I have created a method like this:

-(UIColor*)randomColour
{
    NSArray *colourArray = [NSArray arrayWithObjects:[UIColor redColor],
                                                     [UIColor blueColor],
                                                     [UIColor greenColor],nil];
    UIColor *colour = [colourArray objectAtIndex:rand()%3];

    [colourArray release];
    return colour;
}

and I WANT to call it like this

[[UIColor [self randomColour]] setStroke];

or like this

[[UIColor randomColour] setStroke];

but both fails. What is it that I don't understand?

For the second option I get a warning:

"class method '+randomColour' not found"

I have tried reading up on class methods but can't see why it's cussing about it. Any quick pointers?

Thanks

Upvotes: 0

Views: 350

Answers (3)

Ilanchezhian
Ilanchezhian

Reputation: 17478

It should be

[[self randomColour] setStroke];

Upvotes: 0

DarkDust
DarkDust

Reputation: 92335

If you have defined randomColour in your own class, you simply do:

[[self randomColour] setStroke];

Upvotes: 1

Manlio
Manlio

Reputation: 10865

Try using

+(UIColor*)randomColour

instead of

-(UIColor*)randomColour

Upvotes: 0

Related Questions