I feel silly having to ask this question, but what's the best way to store an array or collection of strings? Without thinking, I originally tried to use a Swift array which crashed the app and I've also tried List<String> but because String is not a Realm type, it does not work. Suggestions?
Just looking for a push in the right direction.
Cheers
Found this: http://stackoverflow.com/a/31730894/5236819
Really wish Realm supported primitive types. :) Thanks anyways
For others finding this, we're tracking it in #1120.
We now support list of primitives. See https://realm.io/blog/realm-cocoa-reaches-3-0/
Thanks
For some reason it doesn't work for me, I'm using ObjectMapper:
Transform
// Transform Array to Realm.List -> Primitives
func arrayToList<T>() -> TransformOf<List<T>, [T]> {
return TransformOf(
fromJSON: { (value: [T]?) -> List<T> in
let result = List<T>()
if let value = value {
result.append(objectsIn: value)
}
return result
},
toJSON: { (value: List<T>?) -> [T] in
var results = [T]()
if let value = value {
results.append(contentsOf: Array(value))
}
return results
})
}
class Foo: Object, Mappable {
var images = List<String>()
public func mapping(map: Map) {
images <- (map["images.original"], arrayToList())
print(images) // <- At this point it has a value
...
}
But when I fetch those objects from Realm, images is always empty, any idea??
Please don't ask questions that are only tangentially related to the initial question on an issue that's been closed for months.
Your problem appears to be due to the fact that you're allocating a new List<T> and assigning that to your property rather than _mutating_ the property. Our documentation recommends that you declare your List<T> properties using let rather than var so the compiler will prevent you from making this mistake.
Never mind, it works (even as var) the problem was on my code (related to how I detach objects from Realm)
what is the last code?
It seems a nice solution...
@barylevy it works, the problem I had was on my own code
Most helpful comment
For some reason it doesn't work for me, I'm using ObjectMapper:
Transform
But when I fetch those objects from Realm,
imagesis always empty, any idea??