在代码中使用通用Int-在Core Data iOS中使用32位还是64位Int? [英] Using generic Int in code - Use 32 or 64 bit Int in Core Data iOS?

查看:146
本文介绍了在代码中使用通用Int-在Core Data iOS中使用32位还是64位Int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迅速编写一个应用程序,该应用程序通过苹果的核心数据保存其数据。在我的代码中,所有整数都被声明为 Int,因为这更加灵活,并且编译器将这些int修改为运行代码的设备。

I am writing an app in swift that saves its data through apple's core data. In my code, all integers are just declared as "Int", because that is more flexible and the compiler adapts those ints to the device the code runs on.

但是,当我想使用核心数据保存这些 Int时,我必须选择32位或64位Integer。我希望我的应用程序尽可能与iPhone 5-6s兼容,因此犹豫是否要使用32bit(我读到Apple由于性能更好而在6s中移至32bit)。

However, when I want to save these "Int"s using core data, I have to chose either 32 or 64 bit Integers. I would want my app to be compatible with iphone 5-6s if possible and am therefore hesitant to go for the 32bit(I read Apple moved to 32bit in 6s because of better performance).

有什么变通办法可以使这部分代码灵活?如果我选择32位,那么如果代码在64位设备上运行会怎样?

Any workarounds to keep this part of the code flexible? If I select 32bit, what will happen if the code is run on a 64bit device?

预先感谢。

推荐答案

默认Int:


  • 在32位设备上= Int32

  • 在64位设备上= Int64(是的,它只是一个Int64在我的iPhone6S上进行了测试)

但是 Int32和Int64都可以在32位设备上运行。 (但是Int64在32位设备上进行计算需要更长的时间)

But both Int32 and Int64 will work on an 32 Bit device. (But Int64 takes longer to calculate on 32 Bit devices)

如果您的数字小于或等于±2.147.483.647
$ b $,我建议您使用Int32 b公式:(2 ^(Bit-1)-1)
甚至是Int16(如果小于或等于±32.767

(实际上,负值可以比正值大1)值:Int32的范围-2.147.483.648 ... 2.147.483.647)

I recommend you using Int32 if your number is smaller or equal to ±2.147.483.647
Formula: (2^(Bit - 1) - 1) Or even Int16 if smaller or equal to ±32.767
(Actually the negative Value can be 1 greater than the positiv value: Range of Int32 -2.147.483.648 ... 2.147.483.647)

如果您在coreData中使用Int32,请确保不要超出此数字并将Int 强制转换为?保存时为Int32 (为?,因为理论上它可以是一个更大的数字)

将Int32加载到Int时总是成功(使用: as!Int

If you use Int32 in coreData just make sure that you don't exceed this number and cast the Int as? Int32 when saving (as? because it theoretically can be a higher number)
When loading Int32 to Int always succeeds (use: as! Int)

如果您在coreData中使用Int64,只需将Int 强制转换为!保存时为Int64 (即使在32位设备上也总是会成功,但可能会稍慢,但是如果您不保存/加载它,那么您就不会有此功能)问题)

但是在加载时要小心,将Int64转换为Int可能会失败,因为从理论上讲,Int64可以存储的数字更大,而32位设备上的Int可以存储更大的数字(使用 as? Int ,以防止可能的崩溃)

If you use Int64 in coreData just cast the Int as! Int64 when saving (This will always succeed even on 32-Bit devices, but might be a slightly slower but if you don't save/load it to ofter you shouldn't have any problems)
But be careful when loading, the cast form Int64 to Int might fail because again Int64 could theoretically have a greater number stored that an Int on 32-Bit devices can store (use as? Int to prevent possible crashes)

这篇关于在代码中使用通用Int-在Core Data iOS中使用32位还是64位Int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆