object_getInstanceVariable works for float, int, bool, but not for double?

后端 未结 2 1335
太阳男子
太阳男子 2020-12-28 20:19

I\'ve got object_getInstanceVariable to work as here however it seems to only work for floats, bools and ints not doubles. I do suspect I\'m doing something wro

2条回答
  •  -上瘾入骨i
    2020-12-28 20:53

    What about using valueForKey:?

    NSNumber * value = [self valueForKey:[NSString stringWithUTF8String:ivar_getName(tmpIvar)]];
    NSLog(@"Double value: %f", [value doubleValue];
    

    Note: this requires you to have a "someFloat" method. If you want to use setValue:forKey:, you'll also need the "setSomeFloat:" method. This is easily implemented by declaring the ivar as an @property and synthesizing it.

提交回复
热议问题