2013-10-02 93 views
0

我想获得UITextView使用的颜色。UIColor比较没有给出想要的结果

对于我写的,

myTextView.textColor 

,但是这给了我作为

UIDeviceRGBColorSpace 0.439216 0.439216 0.439216 1 

任何想法如何得到实际的RGB组合输出?


我看到...

我需要用255乘以这样的:d:P

当0.439216 * 255是112,为什么我的颜色进行比较,是不是给期望的结果?

我应用于UITextView的颜色如下使用常量。

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

当我比较为

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

我得到,不要将颜色...

任何理由?

回答

0

当您比较两个NSObject的(或NSObject子类,在这种情况下),您不想使用==运算符。 ==运算符不会告诉您两个对象是否具有相同的值,它只会告诉您是否在比较相同的对象。您需要使用'比较'或'isEqual'方法。 (比较有一些额外的开销,因为它实际上返回一个枚举,指示三种状态之一,分别对应于<,=和>)。

'在引擎盖下',发生了什么事情,在objective-C中,直接声明一个NSObject,你只会声明一个指针,然后用对象填充的地址空间填充指针。

{ 
    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 *[email protected](1); 
    NSNumber *[email protected](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 
} 

而且,与浮点值的工作时,我会很小心平等信任运营商的结果 - 和的UIColor的是建立在浮动。即使他们'应该',浮游物也有不平等的恶习。它与数字的底层机器表示有关,数字中可能存在一定程度的“噪音”。这种噪音在大多数情况下相对微不足道(例如+/- 1 * 10^-10),但当您想要进行平等操作时,它会变成真正的痛苦。网上有很多关于这方面的文章,可以随时查阅,但不要做平等,你需要检查两个数字之间的差异是否小于给定的epsilon - 差异小于10例如,^ -5。确切地说,如何得到一个合适的epsilon是我无法帮助你的,你需要做你自己的研究。如果我可以帮助的话,我从不使用花车来避免头痛,并且绝对不会做任何需要平等运营者的事情。