user141302
user141302

Reputation:

UIColor result in NSString problem?

NSString *st = [[UIColor greenColor] description]; it gives wrong output. i want to get the Result , st must be @"greenColor" as a NSString any help please?

Upvotes: 0

Views: 402

Answers (1)

philsquared
philsquared

Reputation: 22493

greenColor is just the name of a class method on UIColor. Once the UIColor is constructed it doesn't know it's a greenColor - it just knows that it has colour values that happen to make green.

So I'd suggest one of two things.

  1. Create a category on UIColor that intercepts description and compares the colour values against the set of colour constructors and return the appropriate string.
  2. Subclass UIColor and store a colour name string. Supply a description method that just returns that string.

I suspect (2) is simpler but requires that you are able to use your subclassed version instead of UIColor.

Upvotes: 3

Related Questions