开发者

Objective C: Problem in switching BOOLEAN value of property after button press

I am trying to switch the BOOLEAN value of the property 'isLiked' as seen in the code below

//BOOL isLiked is defined in the header file as property of answer class
- (void)buttonPressed
{
    NSLog(@"button pressed");

    if ([btnType isEqualToString:@"like"]) 
    {
       self.answer.isLiked = !self.answer.isLiked;  
       NSLog(@"answer is: %开发者_运维问答i",self.answer.isLiked);
   }

}

When I print out the value of 'self.answer.isLiked' I see that the value returned is always '0'. How can I switch the values??


I'm guessing self.answer is, in fact, nil. That will cause self.answer.isLiked = !self.answer.isLiked to do nothing at all, and will cause your NSLog to actually be logging the integer value of nil, which is 0.


I suspect your header looks kinda like:

@property (nonatomic, getter=isLiked) BOOL liked;

In which case, your button press code needs to be:

self.answer.liked = !self.answer.isLiked;
NSLog(@"answer is: %i",self.answer.isLiked);

Note the setter for your boolean drops the 'is' prefix, and makes the first letter lowercase.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜