首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我运行相同的代码时,为什么我的代码有不同的打印

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

Stack Overflow用户
提问于 2019-01-23 16:16:42
回答 1查看 33关注 0票数 0

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

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

代码语言:javascript
复制
@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"];
}

当我跑很多次的时候,我有不同的指纹。有两个例子:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

我希望输出是唯一的,但当我再次运行它时,它会显示两个打印。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-23 23:03:30

您的代码不是比较旧值和新值,而是比较旧值和新值对象的地址,这些值对象可以是任何内容。

你需要这段代码

代码语言:javascript
复制
if ([oldC integerValue] < [newC integerValue])

读到NSNumber..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54331472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档