所以我在Realm中有一个非常简单的Book模型
class Book: Object { dynamic var title: String! dynamic var author: String! }
而我正在尝试在帮助程序类中检索所有书籍:
var userBookLibrary = [Book]() let realm = try! Realm() func getBooksFromLocalDatastore() { userBookLibrary = realm.objects(Book) }
这一行:
userBookLibrary = realm.objects(Book)
我疯了还是这不是Realm文档告诉我们要做的?
解决方法
realm.objects()不会返回[Book]但会返回结果< Book> ?.所以你必须改变userBookLibrary的类型:
var userBookLibrary = Results<Book>?