Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to store an array of strings in Realm? #3800

Closed
pigeondev2 opened this issue Jun 24, 2016 · 9 comments
Closed

How to store an array of strings in Realm? #3800

pigeondev2 opened this issue Jun 24, 2016 · 9 comments

Comments

@pigeondev2
Copy link

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

@pigeondev2
Copy link
Author

Found this: http://stackoverflow.com/a/31730894/5236819

Really wish Realm supported primitive types. :) Thanks anyways

@jpsim
Copy link
Contributor

jpsim commented Jun 27, 2016

For others finding this, we're tracking it in #1120.

@bmunkholm
Copy link
Contributor

bmunkholm commented Jan 19, 2018

We now support list of primitives. See https://realm.io/blog/realm-cocoa-reaches-3-0/

@amirpervaiz086
Copy link

Thanks

@robertofrontado
Copy link

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??

@bdash
Copy link
Contributor

bdash commented Jan 23, 2018

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.

@robertofrontado
Copy link

robertofrontado commented Jan 23, 2018

Never mind, it works (even as var) the problem was on my code (related to how I detach objects from Realm)

@barylevy
Copy link

barylevy commented May 7, 2019

what is the last code?
It seems a nice solution...

@robertofrontado
Copy link

@barylevy it works, the problem I had was on my own code

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants