我正在尝试切换属性'isLiked'的BOOLEAN值,如下面的代码所示
//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);
}
}
当我打印出'self.answer.isLiked'的值时,我看到返回的值始终为'0'。我怎样才能切换值?
答案 0 :(得分:3)
我猜self.answer
实际上是零。这将导致self.answer.isLiked = !self.answer.isLiked
什么都不做,并且会导致NSLog实际记录整数值nil,即0。
答案 1 :(得分:2)
我怀疑你的标题看起来有点像:
@property (nonatomic, getter=isLiked) BOOL liked;
在这种情况下,您的按钮代码必须是:
self.answer.liked = !self.answer.isLiked;
NSLog(@"answer is: %i",self.answer.isLiked);
请注意,布尔值的setter会删除'is'前缀,并将第一个字母设为小写。