objective-ckey-value-observingkvc

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


I am learning KVC and KVO. In my demo which from a tutorial, it has two different print when I run the same code.

My class declaration, implementation, and 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"];
}

I have different print when I run many times. There are two examples:

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

and

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

I expect the output is only, but it shows two print when I run it again.


Solution

  • Your code is not comparing the old and new values; it's comparing the addresses of the old and new value objects, which can be just about anything.

    You need this code

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

    And read up on NSNumber...