如何使用Core Data Integer 64和Swift Int64? [英] How to use Core Data Integer 64 with Swift Int64?
问题描述
我有一个CoreData属性的实体,我想要存储大于 Int32.max
和 UInt32.max
。该值用作索引,因此查找性能很重要。所以我选择使用 Integer 64
作为CoreData中的数据类型。
现在我正在努力如何在我的实体实例上存储Int64。另请参阅我尝试的以下不同方法。
使用 NSNumber
:
import Foundation
import CoreData
class Node:NSManagedObject {
@NSManaged var id:NSNumber
}
node.id = Int64(1)
> 'int64'不能转换为'NSNumber'
使用 NSInteger
:
import Foundation
import CoreData
class Node:NSManagedObject {
@NSManaged var id:NSInteger
}
node.id = Int64(1)
> 'Int64'不能转换为'NSInteger'
使用 Int64
:
import Foundation
import CoreData
class Node:NSManagedObject {
@NSManaged var id:Int64
}
node.id = Int64(1)
> EXC_BAD_ACCESS(code = 1,address = ...)
如何定义/
您可以将Integer 64属性定义为 NSNumber
/ code>在托管对象子类中:
@NSManaged var id:NSNumber
设置值:
let值:Int64 = 20000000000000000
node.id = NSNumber(longLong:value)
检索值:
让值:Int64 = node.id.longLongValue
请注意, long long
是32位和64位架构上的64位整数。
将属性定义为
@NSManaged var id:Int64
// ...
node.id = Int64(...)
也应该工作,因为Core Data支持原始
数据类型的标量访问器方法。分配值时, EXC_BAD_ACCESS
异常对我来说是
,就像Swift编译器或运行时中的一个错误。这里报告了一个布尔属性
的类似问题
其中 NSNumber
属性被报告工作,但标量 Bool
属性
导致相同的异常。
I have a CoreData attribute on an entity on which I want to store integer values larger than Int32.max
and UInt32.max
. The value is used as an index, so lookup performance matters. So I've opted to use Integer 64
as datatype in CoreData.
Now I'm struggling on how to store an Int64 on my entity instance. See also the following different approaches I've tried.
Use NSNumber
:
import Foundation
import CoreData
class Node : NSManagedObject {
@NSManaged var id : NSNumber
}
node.id = Int64(1)
> 'Int64' is not convertible to 'NSNumber'
Use NSInteger
:
import Foundation
import CoreData
class Node : NSManagedObject {
@NSManaged var id : NSInteger
}
node.id = Int64(1)
> 'Int64' is not convertible to 'NSInteger'
Use Int64
:
import Foundation
import CoreData
class Node : NSManagedObject {
@NSManaged var id : Int64
}
node.id = Int64(1)
> EXC_BAD_ACCESS (code=1, address=...)
How should the attribute be defined / assigned in order to use 64 bit integers?
You can define the "Integer 64" attribute as NSNumber
in the managed object subclass:
@NSManaged var id : NSNumber
Setting a value:
let value:Int64 = 20000000000000000
node.id = NSNumber(longLong: value)
Retrieving the value:
let value:Int64 = node.id.longLongValue
Note that long long
is a 64-bit integer on both the 32-bit and the 64-bit architecture.
Defining the property as
@NSManaged var id : Int64
// ...
node.id = Int64(...)
should also work, because Core Data supports scalar accessor methods for primitive
data types. The EXC_BAD_ACCESS
exception when assigning a value looks to me
like a bug in the Swift compiler or runtime. A similar problem for a Boolean property
is reported here
where a NSNumber
property is reported to work, but the scalar Bool
property
causes the same exception.
这篇关于如何使用Core Data Integer 64和Swift Int64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!