문제

I'm working on an app, in which I would call a method, and pass in parameter an NSTimeInterval. My method is declared like this :

-(void)MethodwithTime:(NSTimeInterval)t
{
    NSLog(@"Test, T = %f", t);
    ...
}

I call it from a UIViewControllerlike this :

[myObject performSelector:@selector(MethodwithTime:) withObject:[NSNumber numberWithDouble:t]];

Because I read that NSTimeInterval is a double. But the NSLog test gives me some zeros... Test, T = 0.000000

How could I fix it?

Thanks !

도움이 되었습니까?

해결책

NSTimeInterval is not an object (it's a typedef for some floating-point number type, but you could have known that if you had read its documentation). Hence when you are trying to interpret a pointer (which Objective-C objects are represented by) as a floating-point primitive, you'll get unexpected results. How about changing the type of the argument of MethodwithTime: to NSNumber *?

(Oh, and method names start with a lowercase letter and use camelCaps, so MethodwithName: is not idiomatic, MethodWithName: isn't good either, methodWithName: is.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top