Frage

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 !

War es hilfreich?

Lösung

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.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top