How to declare Results generic
I want to declare it like global variable in UITableViewController and fill within viewDidLoad func
Here's my code
class ContactsController: UITableViewController {
var contacts = Results<Contact>()
override func viewDidLoad() {
super.viewDidLoad()
contacts = Domain.FetchContacts()
}
}
But I get error
Cannot invoke initializer for type 'Results' with no arguments
How can I declare it?
I declared results generic like this
var contacts: Results<Contact>? = nil
You can declare the contacts like this:
var contacts: Results<Contact>!
or
var contacts: Results<Contact>?
var contacts: Results<Contact> = realm.objects(Contact.self).filter("FALSEPREDICATE")
You need to make your declaration as a optional type. Remove () and put ? after variable declaration like var contacts = Results?
来源:https://stackoverflow.com/questions/44475843/swift-how-to-declare-realm-results