为什么当我运行相同的代码时我的代码具有不同的打印效果

时间:2019-01-23 16:16:43

标签: objective-c key-value-observing kvc

我正在学习KVC和KVO。在本教程的演示中,当我运行相同的代码时,它具有两种不同的打印方式。

我的类的声明,实现和main.m

@interface Character:NSObject

@property (nonatomic, copy) NSString *characterName;
@property NSInteger ownedClowCards;

-(void)hasLostClowCard;
-(void)hasGainedClowCard;

@end

@implementation Character

-(void)hasLostClowCard
{
    NSLog(@"%@ has lost a card! Cards now: %ld", _characterName, _ownedClowCards);
}

-(void)hasGainedClowCard
{
    NSLog(@"%@ has earned a card! Cards now: %ld", _characterName, _ownedClowCards);
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if([keyPath isEqualToString:@"ownedClowCards"])
    {
        NSNumber *oldC = [change objectForKey:NSKeyValueChangeOldKey];
        [oldC integerValue];
        NSNumber *newC = [change objectForKey:NSKeyValueChangeNewKey];
        [newC integerValue];
        NSLog(@" newC = %@ ", newC);
        NSLog(@" oldC = %@", oldC);

        if(oldC < newC)
        {
            NSLog(@" invoke gain method");
            [self hasGainedClowCard];
        }else
        {
            NSLog(@"invoke lost method");
            [self hasLostClowCard];
        }
    }
}
int main() {
    Character *sakura;

    //Created and give the properties some values with KVC...
    sakura = [[Character alloc] init];
    [sakura setValue:@"Sakura Kinomoto" forKey:@"characterName"];
    [sakura setValue:[NSNumber numberWithInt:20] forKey:@"ownedClowCards"];

    //Done! Now we are going to fetch the values using KVC.

    NSString *mainCharacter = [sakura valueForKey:@"characterName"];
    NSNumber *mainCharCards = [sakura valueForKey:@"ownedClowCards"];


    NSLog(@"%@ has %d Clow Cards", mainCharacter, [mainCharCards intValue]);

    //Here begins the KVO section.
    [sakura addObserver:sakura forKeyPath:@"ownedClowCards" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

    [sakura setValue:[NSNumber numberWithInt:2] forKey:@"ownedClowCards"];
    [sakura removeObserver:sakura forKeyPath:@"ownedClowCards"];
}

我多次跑步时有不同的印刷品。有两个示例:

2019-01-24 00:07:12.469612+0800 CardCaptorChars[48275:1711978] Sakura Kinomoto has 20 Clow Cards
2019-01-24 00:07:12.471564+0800 CardCaptorChars[48275:1711978]  newC = 2
2019-01-24 00:07:12.471792+0800 CardCaptorChars[48275:1711978]  oldC = 20
2019-01-24 00:07:12.471820+0800 CardCaptorChars[48275:1711978]  invoke gain method
2019-01-24 00:07:12.471864+0800 CardCaptorChars[48275:1711978] Sakura Kinomoto has earned a card! Cards now: 2
Program ended with exit code: 0

2019-01-24 00:09:13.092788+0800 CardCaptorChars[48279:1712542] Sakura Kinomoto has 20 Clow Cards
2019-01-24 00:09:13.093989+0800 CardCaptorChars[48279:1712542]  newC = 2
2019-01-24 00:09:13.094054+0800 CardCaptorChars[48279:1712542]  oldC = 20
2019-01-24 00:09:13.094093+0800 CardCaptorChars[48279:1712542] invoke lost method
2019-01-24 00:09:13.094161+0800 CardCaptorChars[48279:1712542] Sakura Kinomoto has lost a card! Cards now: 2
Program ended with exit code: 0

我希望输出只是,但是再次运行时它显示两个打印。

1 个答案:

答案 0 :(得分:0)

您的代码未比较旧值和新值;它在比较旧值和新值 objects 的地址,该地址几乎可以是任何东西。

您需要此代码

if ([oldC integerValue] < [newC integerValue])

并继续阅读NSNumber ...