-3

How can i properly change the following into a NSInteger:

countDown.text = [NSString stringWithFormat:@"%i", (int)(currentTime-startTime)];

Countdown is a SKLabelnode

user3593148
  • 505
  • 1
  • 3
  • 14

2 Answers2

0

There's no real benefit to casting to an NSInteger (which is just a typedef for long on 64-bit architectures and int on 32-bit architectures) , but you can do that by changing your code into the following:

countDown.text = [NSString stringWithFormat:@"%ld", (NSInteger)(currentTime-startTime)];
mattt
  • 19,544
  • 7
  • 73
  • 84
-1

You could use NSNumberFormatter

For example:

NSString *string = @"12";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSInteger i = [[formatter numberFromString:string] integerValue];
Binary Pulsar
  • 1,209
  • 10
  • 17
  • 1
    Creating an `NSFormatter` instance each time an integer is to be formatted is enormously expensive. Make sure to cache an instance for repeated use if you go this route. – mattt Oct 13 '14 at 20:22
  • Yeah, like that idea better – Binary Pulsar Oct 13 '14 at 20:23
  • @pulsar what do i put in place of 12? – user3593148 Oct 13 '14 at 20:31
  • You would put `countDown.text` where `@"12"` is but consider what @mattt is saying. If you're going to be doing this a lot then make a property to hold an `NSInteger` and set it when you set your `countDown.text`. – Binary Pulsar Oct 13 '14 at 20:33