views:

35

answers:

2
UILabel *templabel = [self.wallBoxArray objectAtIndex:i];

for( int i = 0 ; i < [self.wallBoxArray count]; i++)
{
  if(templabel.backgroundColor == [UIColor greenColor])
   {
     NSLog(@"the color isn green");  
   }
}

There are many label's in my array. They all initialized with green color. But i judged that way ,why cant print " the color isn't green.Thank you.

+1  A: 

You are performing a pointer comparision there, so if the color's are both green, but different instances of UIColor, this will fail. And they are because UIView's backgroundColor property is a copy property.

@property(nonatomic, copy) UIColor *backgroundColor

I'm sort of surprised this is that convoluted, but to check for equality, try the following:

CGColorEqualToColor([templabel.backgroundColor CGColor], [[UIColor greenColor] CGColor])

This is checking equality of the color value, not just a pointer comparison. Also remember to do [str compare:otherString] == NSOrderSame when checking strings!

Brian King
Or `[str isEqualToString:otherString]`.
Peter Hosey
+2  A: 

The UIColor class cluster implements -isEqual:, so you could just use

if([templabel.backgroundColor isEqual:[UIColor greenColor]])
  ...
KennyTM
hah, right, forgot about that... *palm->forehead*
Brian King