-1

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 !

Girish
  • 4,692
  • 4
  • 35
  • 55
user2057209
  • 345
  • 1
  • 6
  • 19

1 Answers1

3

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

  • If changing the arguments is the only solution that i have, so i'll do. – user2057209 Jul 08 '13 at 00:04
  • @user2057209 Note that then **obviously** you'll have to change how you log it as well. –  Jul 08 '13 at 00:04
  • 2
    @user2057209 Please google that. You have to make some effort yourself too. –  Jul 08 '13 at 00:08
  • I did. That's why i said that i've found that. See "Because i read that NSTimeInterval is a double"... – user2057209 Jul 08 '13 at 00:09
  • @user2057209 `NSNumber *foo = @(1.5); NSLog(@"%@", foo);` since it's an object. –  Jul 08 '13 at 00:11
  • @user2057209: Of course it's not the only solution. But until you explain (per rmaddy's comment) why you are not calling it directly, all the other solutions will kinda be pointless. – newacct Jul 08 '13 at 09:19