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

Why my code have different print when I run the same code

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

我的 class 声明、实现和 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

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

您的代码没有比较新旧值;它正在比较新旧值 objects 的地址,这几乎可以是任何东西。

您需要此代码

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

并继续阅读 NSNumber...