Zedd
Zedd

Reputation: 204

@property int value can't change,is really wierd,and i can't find the solution

I'm using a button to invoke the change of a int value declared in @interface.here is my code of interface.

@interface CheckInController ()
@property (nonatomic, assign) int checkInDate;
@end

the code of button's selector

- (void)checkin {
    self.checkInDate++;
    NSLog(@"checkInDate: %d",_checkInDate);
}

whatever times I click the button,the console panel shows like this

2017-08-01 16:46:39.631 HeJing[1888:64607] checkInDate: 0
2017-08-01 16:46:40.057 HeJing[1888:64607] checkInDate: 0
2017-08-01 16:46:40.342 HeJing[1888:64607] checkInDate: 0
2017-08-01 16:46:40.578 HeJing[1888:64607] checkInDate: 0

after that i assign some int value like this

self.checkInDate = 1;
NSLog(@"checkInDate: %d",_checkInDate);

the console panel always show

2017-08-01 17:06:38.182 HeJing[1991:75284] checkInDate: 0
2017-08-01 17:06:39.101 HeJing[1991:75284] checkInDate: 0
2017-08-01 17:06:39.255 HeJing[1991:75284] checkInDate: 0
2017-08-01 17:06:39.401 HeJing[1991:75284] checkInDate: 0

did I do something wrong? the code above all run in the .m file. and my setter method

- (void)setCheckInDate:(int)checkInDate {
    NSString *infoString = [NSString stringWithFormat:@"已连续签到%d天,再坚持%d天就可以积分翻倍哦!",_checkInDate,6 - _checkInDate];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:infoString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.97 green:0.44 blue:0.13 alpha:1.00] range:NSMakeRange(5, 2)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.97 green:0.44 blue:0.13 alpha:1.00] range:NSMakeRange(11, 2)];
    _checkInInfoLabel.attributedText = attributedString;
}

Upvotes: 0

Views: 44

Answers (1)

Larme
Larme

Reputation: 26016

You are using a custom setter by overriding - (void)setCheckInDate:(int)checkInDate.

So when you write self.checkInDate = 1;, it's calling the custom setter. But in that setter you didn't write _checkInDate = checkInDate. Adding this should fix your issue.

Upvotes: 1

Related Questions