简体   繁体   English

在Realm Swift中声明一个Int数组

[英]Declare an array of Int in Realm Swift

How can I declare an array of integers inside RLMObject ? 如何在RLMObject声明一个整数数组?

Like : 喜欢 :

dynamic var key:[Int]?

Gives the following error : 给出以下错误:

Terminating app due to uncaught exception 'RLMException', reason: ''NSArray' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more information.'

Lists of primitives are not supported yet unfortunately. 遗憾的是,不支持基元列表。 There is issue #1120 to track adding support for that. 问题#1120跟踪添加对此的支持。 You'll find there some ideas how you can workaround that currently. 你会发现一些想法如何解决当前的问题。

The easiest workaround is create a object to hold int values. 最简单的解决方法是创建一个对象来保存int值。 Then the model to have a List of the object. 然后模型有一个对象的List。

class Foo: Object {
    let integerList = List<IntObject>() // Workaround
}

class IntObject: Object {
    dynamic var value = 0
}

The accepted offer is very costly in term of memory. 接受的报价在记忆方面非常昂贵。 You might get a List of very big "n" of objects. 您可能会获得一个非常大的“n”对象列表。

It's not a matter of right and wrong but I think it's good to write here a different workaround. 这不是对与错的问题,但我认为在这里写一个不同的解决方法是件好事。

Another approach: 另一种方法:
I decided to use a single string to represent an Int array. 我决定用一个字符串来表示一个Int数组。

In my Realm class I defined a variable: 在我的Realm类中,我定义了一个变量:

dynamic var arrInt: String? = nil

And use it very easily: 并且非常容易使用它:

let arrToSave = [0, 1, 33, 12232, 394]
<MY_CUSTOM_REALM_CLASS>.arrInt = arrToSave.map { String(describing: $0) }.joined(separator: ",")

And the way back: 回来的路上:

let strFetched = <MY_CUSTOM_REALM_CLASS>.arrInt 
let intArray = strFetched.components(separatedBy: ",").flatMap { Int($0) }

Will be happy to hear your feedback, as I think this approach is better. 很高兴听到您的反馈,因为我认为这种方法更好。

Fortunately arrays of primitive types are now supported in Realm 3.0 and above. 幸运的是,Realm 3.0及更高版本现在支持基本类型的数组。 (Oct 31 2017) (2017年10月31日)

You can now store primitive types or their nullable counterparts (more specifically: booleans, integer and floating-point number types, strings, dates, and data) directly within RLMArrays or Lists. 您现在可以直接在RLMArrays或Lists中存储基本类型或其可空对应项(更具体地说:布尔值,整数和浮点数类型,字符串,日期和数据)。 If you want to define a list of such primitive values you no longer need to define cumbersome single-field wrapper objects. 如果要定义此类原始值的列表,则不再需要定义繁琐的单字段包装器对象。 Instead, you can just store the primitive values themselves! 相反,您可以自己存储原始值!

class MyObject : Object {
    @objc dynamic var myString: String = ""
    let myIntArray = List<Int>()
}

Source: https://realm.io/blog/realm-cocoa-reaches-3-0/ 资料来源: https//realm.io/blog/realm-cocoa-reaches-3-0/

As the error message states, you have to use RLMArray - or rather it's swift equivalent List . 正如错误消息所述,您必须使用RLMArray - 或者更确切地说它是快速等效的List

See: Realm docs 请参阅: Realm docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM