0

I am using the code below. I could successfully get the string value. But when it is converted to NSInteger, a minus appears in the front, and the value changes. Am I missing something?

NSInteger bannerStamp = [[eachDict  objectForKey:@"timeStamp"] integerValue];
NSLog(@"%@",[eachDict  objectForKey:@"timeStamp"]);
NSLog(@"%d",bannerStamp);

OUTPUT

2015-01-01 10:44:52.482 SalesApp[24570:60b] 3597478187
2015-01-01 10:44:54.094 SalesApp[24570:60b] -697489109
Pang
  • 9,564
  • 146
  • 81
  • 122
VARUN ISAC
  • 443
  • 3
  • 13

2 Answers2

2

try to convert into long long value

long long bannerStamp = [[eachDict  objectForKey:@"timeStamp"] longLongValue];
NSLog(@"%@",[eachDict  objectForKey:@"timeStamp"]);
NSLog(@"%lld",bannerStamp);

Here is the difference between 32-bit and 64-bit architecture,

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

Apple document link for changes to data types

Pradip Vanparia
  • 1,742
  • 10
  • 22
  • 1
    Thanks to share the apple document link. I got this issue some months ago. I did same like as you explained, Thanks. – ObjC Jan 01 '15 at 06:33
1

That's what I get for going off the top of my head. Check this code and output for a variety of approaches.

    //*******
NSLog(@"For SO question");
NSDictionary *eachDict = @{ @"timeStamp" : @"3597478187" };
NSDecimalNumber *decimalBannerStamp = [NSDecimalNumber decimalNumberWithString:[eachDict objectForKey:@"timeStamp"]];
NSInteger bannerStamp = [[eachDict  objectForKey:@"timeStamp"] integerValue];
uint64_t bannerStampUINT64 = [[eachDict objectForKey:@"timeStamp"] longLongValue];
NSLog(@"%@",[eachDict  objectForKey:@"timeStamp"]);
NSLog(@"NSInteger: %u",bannerStamp);
NSLog(@"uint64_t: %llu",bannerStampUINT64);
NSLog(@"DecimalObject: %@", decimalBannerStamp);
NSLog(@"Decimal unsigned value: %lu", (unsigned long)decimalBannerStamp.unsignedIntegerValue);
NSLog(@"End SO question code");
//*******


2014-12-31 22:14:50.206 PIClient[5112:2296154] For SO question
2014-12-31 22:14:50.212 PIClient[5112:2296154] 3597478187
2014-12-31 22:14:50.213 PIClient[5112:2296154] NSInteger: 2147483647
2014-12-31 22:14:50.213 PIClient[5112:2296154] uint64_t: 3597478187
2014-12-31 22:14:50.214 PIClient[5112:2296154] DecimalObject: 3597478187
2014-12-31 22:14:50.215 PIClient[5112:2296154] Decimal unsigned value: 3597478187
2014-12-31 22:14:50.215 PIClient[5112:2296154] End SO question code
Brad Brighton
  • 2,179
  • 1
  • 13
  • 15
  • Probably better to use uint64_t (which is a unsigned long long), that will be a 64-bit value on both 32-bit and 64-bit systems. – zaph Jan 01 '15 at 05:38
  • If the source data or the resultant use of the converted value warrants the extra measure of safety, I agree. – Brad Brighton Jan 01 '15 at 05:40
  • No way. NSUInteger bannerStamp = [[eachDict objectForKey:@"timeStamp"] unsignedIntegerValue]; shows the same result. – VARUN ISAC Jan 01 '15 at 05:40
  • "No way" what? NSUInteger is giving you a negative number? Note you'll also probably be getting a warning about the format specifier in your log statement - use the unsigned version for that as well or check the bannerStamp value in the debugger. – Brad Brighton Jan 01 '15 at 05:43