I have a subclass of an UIImageView with some boolean properties as follows: And I cannot set the isOccupied value to YES in my code.
#import <UIKit/UIKit.h>
@interface TargetView : UIImageView
@property (strong, nonatomic) NSString* letter;
@property (assign, nonatomic) BOOL isMatched;
@property (assign) BOOL isOccupied;
-(instancetype) initWithLetter:(NSString*) letter andSideLength:(float)sideLength;
@end
Each of these TargetViews are stored in NSMutableArray* _targets;
And when I execute the following
for (TargetView* tv in _targets) {
if (!tv.isOccupied) {
tv.isOccupied = YES;
//[tv setIsOccupied:YES]; <--- have also tried used this setter
break;
}
}
The targetView is still evaluating as False the next time I run the code. So I am not able to set the isOccupied value to TRUE, true or YES.
I initialise the targetView as follows, and all targetViews are initially set to isOccupied is false
-(instancetype)initWithLetter:(NSString *)letter andSideLength:(float)sideLength{
UIImage* img = [UIImage imageNamed:@"blank.png"];
self = [super initWithImage:img];
if (self != nil) {
self.isMatched = NO;
self.isOccupied = NO;
float scale = sideLength/img.size.width;
self.frame = CGRectMake(0, 0, img.size.width*scale, img.size.width*scale);
_letter = @"";
}
return self;
}
The code that is assigning the boolean value to the targetView is delegate method running in the controller and not within the targetView class itself. Could this be causing the problem? Or is it how I have set up my properties? I've tried assign, atomic, nonatomic, ......
EDIT**
I changed some to the code to use **_isOccupied** instead of **self.isOccupied** in the class initialisation. and now it works. Can anyone explain why this works?