核心数据更改64位整数的值

时间:2022-09-06 20:55:44

I'm new to iOS development and have been using core data in the app I'm developing. I have a timestamp value which is stored as a property in my NSManagedObject and this property is declared as an Integer-64 bit in the core data model. It's declared as an NSNumber in the NSManagedObject subclass.

我是iOS开发的新手,并且一直在我正在开发的应用程序中使用核心数据。我有一个时间戳值,它作为属性存储在我的NSManagedObject中,并且此属性在核心数据模型中声明为Integer-64位。它在NSManagedObject子类中声明为NSNumber。

When I run this on my iPhone 6 and iOS 8, the code works fine and the timestamp gets stored as it's supposed to. However, when I run the same code on my iPhone 4 which has iOS 7 on it, the number is changed. I'm unsure why this happens as the core data model is the same and declares the property as an Integer-64.

当我在iPhone 6和iOS 8上运行时,代码工作正常,时间戳按照预期存储。但是,当我在其上安装了iOS 7的iPhone 4上运行相同的代码时,数字会发生变化。我不确定为什么会发生这种情况,因为核心数据模型是相同的,并将属性声明为Integer-64。

When I create this NSManaged object and populate the timestamp property, I use

当我创建这个NSManaged对象并填充timestamp属性时,我使用

[NSNumber numberWithUnsignedInteger: timeStampValue]

I then save the context. But when I fetch the object and read the value, the value changes on my iPhone 4/iOS7. I'm unsure if this is an iPhone 4 or an iOS7 issue as I do not possess another device to test this on. It also works fine on the emulator.

然后我保存上下文。但是当我获取对象并读取值时,我的iPhone 4 / iOS7上的值会发生变化。我不确定这是iPhone 4还是iOS7问题,因为我没有其他设备来测试它。它也可以在模拟器上正常工作。

If anyone could tell me why this is happening and what I could do to fix it, I'd greatly appreciate it.

如果有人能告诉我为什么会这样,以及我能做些什么来解决它,我会非常感激。

If any more information is needed, i'll provide it.

如果需要更多信息,我会提供。

1 个解决方案

#1


0  

To anyone who might have this problem in the future. I have found the solution. It was an issue with the iPhone 4.

对于将来可能遇到此问题的任何人。我找到了解决方案。这是iPhone 4的一个问题。

The problem was not with Core Data or the 64 bit integer setting there. The error arrived due to objective-c's size of long. This is how I retrieved the initial timestamp value from the server and I had stored it as a long. I'd assumed the long to be 64 bits (like in Java).

问题不在于Core Data或64位整数设置。由于objective-c的大小很长,错误到了。这就是我从服务器检索初始时间戳值的方法,并将其存储为long。我假设长到64位(就像在Java中一样)。

On the iPhone 6, a long was indeed 64 bits and the value hence did not change. On the iPhone 4 however, the size of a long was 32 bits and the value was corrupted for the large number. Changing the storing variable to a long long solved this problem.

在iPhone 6上,长的确是64位,因此价值没有变化。然而,在iPhone 4上,long的大小是32位,并且对于大数字,该值被破坏。将存储变量更改为long long解决了这个问题。

#1


0  

To anyone who might have this problem in the future. I have found the solution. It was an issue with the iPhone 4.

对于将来可能遇到此问题的任何人。我找到了解决方案。这是iPhone 4的一个问题。

The problem was not with Core Data or the 64 bit integer setting there. The error arrived due to objective-c's size of long. This is how I retrieved the initial timestamp value from the server and I had stored it as a long. I'd assumed the long to be 64 bits (like in Java).

问题不在于Core Data或64位整数设置。由于objective-c的大小很长,错误到了。这就是我从服务器检索初始时间戳值的方法,并将其存储为long。我假设长到64位(就像在Java中一样)。

On the iPhone 6, a long was indeed 64 bits and the value hence did not change. On the iPhone 4 however, the size of a long was 32 bits and the value was corrupted for the large number. Changing the storing variable to a long long solved this problem.

在iPhone 6上,长的确是64位,因此价值没有变化。然而,在iPhone 4上,long的大小是32位,并且对于大数字,该值被破坏。将存储变量更改为long long解决了这个问题。