在 core-data 中保存 unix 时间戳并将其转换为负整数

发布于 2024-12-25 02:42:09 字数 730 浏览 0 评论 0原文

我正在尝试使用以下代码将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2025-01-01 02:42:09

如果将该字段设置为字符串然后将其保存到其中会怎样:

NSString *saveValue = [NSString stringWithFormat:@"%d",
[[structionDictionary objectForKey:@"timestamp"] intValue]];

What if you make the field a string and then save this into it:

NSString *saveValue = [NSString stringWithFormat:@"%d",
[[structureDictionary objectForKey:@"timestamp"] intValue]];

自控 2025-01-01 02:42:09

是的,为了扩展 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文