How to Convert an Array to List in Realm

How to convert an array to List in Realm?

Note that your problem is not about converting List to array, but rather converting an array to List. You already know how to do the former: Array(someList), but since you are assigning an array (movie.genreIDs ?? []) to a List property (movieRealm.genreIDs), you need to convert an array to a List.

But wait! movieRealm.genreIDs is a let constant, so you can't assign to it anyway. Your real problem is that you haven't declared the genreIDs property correctly.

If you follow the docs on how to declare a property of a List type:

class CachedMovies: Object {
// genreIDs should not be optional
let genreIDs = List<Int>()
}

You'll see that you don't need to convert arrays to List. You can just add the contents of the array to the List, since the list is not nil:

movieRealm.genreIDs.append(objectsIn: move.genreIDs ?? [])

Note that if you actually want an optional list property (a property which can be nil, empty, or have elements), Realm doesn't support that.

Add arrays to Realm with swift 3

As a general rule it's way more efficient to use the one-to-many relationships provided by Realm instead of trying to emulate them by using arrays (Realm's collections are lazy, the objects contained are instantiated only when needed as opposed to plain Swift arrays).

In your case, if I understand correctly what you're trying to do, you want to add [RealmString] Swift arrays to the _backingNickNames list.

Why not use the append(objectsIn:) method of Realm's List class (see here), like this:

// Dog model
class Dog: Object {
dynamic var name = ""
dynamic var owner: Person?
}

// Person model
class Person: Object {
dynamic var name = ""
dynamic var birthdate = NSDate(timeIntervalSince1970: 1)

let dogs = List<Dog>()
}

let jim = Person()

let dog1 = Dog()
let dog2 = Dog()

// here is where the magic happens
jim.dogs.append(objectsIn: [dog1, dog2])

If you want to do the opposite (convert from a List to an Array) just do :

let dogsArray = Array(jim.dogs)

• • • • • • • •

Back to your own posted solution, you could easily refactor the model to accommodate this. Each Sensor object could have several Topic and several Message objects attached.

Just ditch the message and topic computed properties and rename topicV and messageV to topics and messages respectively. Also rename RealmString to Topic and RealmString1 to Message.

Now, you could easily iterate through the, say, topics attached to a sensor like this :

for topic in sensor1.topics { ... }

Or if you want to attach a message to a sensor you could do something like this (don't forget to properly add the newly created object to the DB first):

let message1 = Message()
message1.stringValue = "Some text"

sensor2.messages.append(message1)

So, no need to use intermediary Swift Arrays.

Realm Swift - Convert an array of results into an array of Ints

You can simply use Array(realm.objects(RealmType.self)), which will convert the Results<RealmType> instance into an Array<RealmType>.

However, there are other serious flaws with your code. First of all, neither of the last two lines will compile, since firstly realm.objects() accepts a generic input argument of type Object.Type and Data doesn't inherit from Object. You can't directly store Data objects in Realm, you can only store Data as a property of a Realm Object subclass.

Secondly, myArray[results] is simply wrong, since results is supposed to be of type Results<RealmType>, which is a collection, so using it to index an Array cannot work (especially whose Element type is different).

How to convert Array of Struct to List Realm?

This should just be a matter of adding the new GenreEntity objects to an array and then return the entire array once done.

This should do it

func convertToList(genreArray: [GenreClass]) -> List<GenreEntityRealmModel> {
let genreEntityList = List<GenreEntityRealmModel>()
genreArray.forEach { model in
let genreEntity = GenreEntity()
genreEntity.gamesCount = model.gamesCount
genreEntityList.append(genreEntity)
}
return genreEntityList
}

Converting Realm's ListMyType to normal set or array

You can just initialise an array with a sequence:

let array = Array(myList)

Realm - return array list

The filter operation will keep filter the Mood objects matching your given criteria, so its return value will be of type Results<Mood> which you obviously can't convert in coercion to Array<String>.

It seems that you are actually trying to map the Mood objects matching your filter criteria to their symptom property, which is of type String. To achieve your goals, you need to call map after filtering the Mood objects.

Moreover, your return value is completely flawed, you want to return the array stored in the variable greatMoodActivity and not call your function recursively.

func getGreatMoodActivity() -> [String]{
let greatMoodActivity = realm.objects(Mood.self).filter("mood = 'Great'").map({$0.symptom})
return Array(greatMoodActivity)
}


Related Topics



Leave a reply



Submit