在 core-data 中保存 unix 时间戳并将其转换为负整数
我正在尝试使用以下代码将 unix 时间戳保存到核心数据:
NSManagedObject *managedPostObject = [NSEntityDescription insertNewObjectForEntityForName:@"CDPost" inManagedObjectContext:moc]; [managementPostObject setValue:[structionDictionary objectForKey:@"timestamp"] forKey:@"timestamp"];
我从远程服务器获取时间戳的格式如下:
“时间戳”:1323118064000
由于某种原因,所有时间戳值都保存为:-2147483648
我尝试过 INTEGER32、INTEGER64、Decimal、Double 以及 string 作为时间戳的数据类型,但没有一个有效。
已解决: 以下对我来说是成功的:
NSString *timeStampStr = [[结构字典 objectForKey:@"timestamp"] stringValue]; [托管Post对象 setValue:timeStampStr forKey:@"timestamp"];
I am trying to save unix timestamp to core data using the following code:
NSManagedObject *managedPostObject = [NSEntityDescription insertNewObjectForEntityForName:@"CDPost" inManagedObjectContext:moc]; [managedPostObject setValue:[structureDictionary objectForKey:@"timestamp"] forKey:@"timestamp"];
The format that I am getting timestamp from remote server is as follows:
"timestamp": 1323118064000
For some reason all the time stamp values gets saved as : -2147483648
I have tried INTEGER32, INTEGER64, Decimal, Double as well as string as the data type for timestamp but none worked.
SOLVED:
Following did the trick for me:
NSString *timeStampStr = [[structureDictionary
objectForKey:@"timestamp"] stringValue]; [managedPostObject
setValue:timeStampStr forKey:@"timestamp"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将该字段设置为字符串然后将其保存到其中会怎样:
What if you make the field a string and then save this into it:
是的,为了扩展 Simon 的评论,请确保您最初这样做不是为了获取值:
NSInteger timestamp = [[dictionary objectForKey:@"timestamp"] intValue]
。当 # 对于空间来说太大时,它看起来像是一个经典的换行案例。
相反,请尝试类似
long long timestamp = [[dictionary objectForKey:@"timestamp"] longLongValue]
的内容。然后将其保存到
INT64
。Yeah, to expand on Simon's comment, make sure you are not doing this to get your value initially:
NSInteger timestamp = [[dictionary objectForKey:@"timestamp"] intValue]
.It looks like a classic case of wrapping when the # is too big for the space.
Instead try something like
long long timestamp = [[dictionary objectForKey:@"timestamp"] longLongValue]
.Then save it to an
INT64
.