Reputation: 829
I've got a task to convert an existing project to Swift4 (from Swift 2.3, don't ask O_o). Managed to fix almost everything except this issue using RealmSwift:
Old code:
class func myFunc() -> List<MyClass> {
let realm = try! Realm()
return List(realm.objects(MyClass).sorted("name", ascending: true))
}
Getting compiler error for the return statement:
Argument passed to call that takes no arguments
When I trying to fix like this, compiler is silent, but the function doesn't do its job:
return List<MyClass>()
So which is then the right way to initialize List with a collection of custom objects? Please, help!
Upvotes: 0
Views: 1378
Reputation: 54706
List
doesn't have an initializer accepting a Results
instance in RealmSwift 3.1.0 (I'm not sure since when though). List
's only initializer doesn't take any input arguments and it creates an empty List
instance.
You can work around this by creating an empty List
using the only initializer, then calling append
to add the elements of the Results
collection to the list.
func myFunc() -> List<MyClass> {
let realm = try! Realm()
let list = List<MyClass>()
list.append(objectsIn: realm.objects(MyClass.self).sorted(byKeyPath: "name", ascending: true))
return list
}
Upvotes: 3