Objective C KVO,NSLog New + Old值

时间:2015-12-21 21:28:27

标签: objective-c nsstring nsdictionary key-value-observing kvocontroller

我有:

$user = factory(User::class)->create();
$this->actingAs($user);
$request = new MyRequest();
$request->setContainer($this->app);
$attributes = ['aa' => 'asd'];
$request->initialize([], $attributes);
$this->app->instance('request', $request);
$authorized = $request->authorize();
$this->assertEquals(true, $authorized);

- (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(Player *)object
                         change:(NSDictionary<NSString *,id> *)change
                        context:(void *)context
{
    switch (object.Status)
    {
        case 0:
            NSLog(@"%ld Play" ,object.Status);
            break;
        case 1:
            NSLog(@"%ld Pause", object.Status);
            break;
        case 2:
            NSLog(@"%ld Stop", object.Status);
            break;
        case 3:
            NSLog(@"%ld RewindF", object.Status);
            break;
        case 4:
            NSLog(@"%ld RewindB", object.Status);
            break;
        default:
            break;
    }
}

我想在屏幕2上看到值: [player addObserver:classA forKeyPath:@"Status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; NewValueKey但我也不知道如何使用NSLog OldValue。 我试过了

OldValueKey

但我收到了错误

  

“NSUInteger too String”

如何在一个NSLog中连接新旧:)?

1 个答案:

答案 0 :(得分:0)

如果您特别想要注销新旧状态,那么您需要输入它们(因为您收到它们为NSNumber

像这样:

  NSLog(@"OLD VALUE: %@ \nNEW VALUE: %@", [[change objectForKey:NSKeyValueChangeOldKey] stringValue], [[change objectForKey:NSKeyValueChangeNewKey] stringValue]);

另请注意,您要使用常量,例如NSKeyValueChangeNewKey。不是选项常量,例如NSKeyValueObservingOptionNew

编辑:@Joost已经纠正了我,-objectForKey返回了NSNumber。因此,要获取要记录的字符串,您需要将该对象发送到-stringValue消息