Question

I am trying to get the color used by UITextView.

For that I wrote,

myTextView.textColor

but this gives me output as

UIDeviceRGBColorSpace 0.439216 0.439216 0.439216 1

Any idea how to get actual RGB combination?


I see...

I need to multiply this by 255 :D :P

When 0.439216*255 is 112 why my color comparing is not giving desired result?

The color I applied to UITextView is as below using constant.

#define placeColor [UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]

When I am comparing as

if (aboutCompany.textColor == placeColor) {
    NSLog(@"place color..");
    aboutCompany.text = @"";
    aboutCompany.textColor = fottColor;
} else {
    NSLog(@"not place color..");
}

I am getting, not place color...

Any reason WHY?

Was it helpful?

Solution

When you compare two NSObject's (or NSObject subclasses, in this case), you don't want to use the == operator. The == operator won't tell you if the two objects have the same value, it will only tell you if you're comparing the same object. You need to use the 'compare' or 'isEqual' methods. (Compare has some additional overhead because it actually returns an enum indicating one of three states, corresponding to <, =, and > respectively.)

'Under the hood', what's happening is that in objective-C, you never directly declare an NSObject, you only ever declare a pointer, which is then filled with the address space the object fills.

{
    NSObject *foo=[[Foo alloc] init];
    NSObject *bar=foo;

    foo==bar;//true
    //If you inspect foo and bar really closely in the debugger, you'll discover they're both just integers pointing to a 'random' memory space.
}

{
    NSNumber *alpha=@(1);
    NSNumber *beta=@(1);
    //Again, if you inspect these very closely, you'll discover they're pointers to memory space, but this time you're pointing to two different spaces in memory, even though you're storing the same data in them.
    alpha==beta;//False, they're two separate objects
    [alpha isEqualTo:beta];//true; their value is identical
    [alpha intValue]==[beta intValue;//Again, true because their value is identical
}

Also, I'd be very careful with trusting the results of equality operators when working with floating point values -- and UIColor's are built on floats. Floats have a nasty habit of not being equal, even when they 'should' be. It has to do with the underlying machine representation of the number, which can have a certain degree of 'noise' in the number. That noise is relatively insignificant most of the time (+/- 1*10^-10 for example), but it becomes a real pain when you want to do equality operations. There are plenty of articles online that cover this better, feel free to look them up, but instead of doing equality, you need to check that the difference between the two numbers is smaller than a given epsilon -- that the difference is less than 10^-5, for example. Exactly how to derive an appropriate epsilon is something I can't help you with, you'll need to do your own research. I just avoid the headache by never using floats if I can help it, and certainly never doing anything that requires an equality operator on them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top