字符串转换为大整数值

时间:2015-01-01 05:20:35

标签: ios nsnumber nsinteger

我正在使用以下代码。我可以成功获取字符串值。但是当它转换为NSInteger时,前面会出现一个减号,并且值会发生变化。我错过了什么吗?

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

输出

2015-01-01 10:44:52.482 SalesApp[24570:60b] 3597478187
2015-01-01 10:44:54.094 SalesApp[24570:60b] -697489109

2 个答案:

答案 0 :(得分:2)

尝试转换为long long value

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

这是32位和64位架构之间的区别,

#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

changes to data types

的Apple文档链接

答案 1 :(得分:1)

这就是我从头脑中得到的东西。检查此代码和输出以了解各种方法。

    //*******
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