0
我遇到了一個問題,它返回Realm中的對象,奇怪的是,如果我打印控制檯對象,如果我啓動它,但是如果我嘗試訪問它的值,它會告訴我它是空的。返回對象域無法訪問
對象的結構如下:
class Favourite : Object {
var character : Character!
}
我創建一個對象,並將其添加到數據庫
let fav = Favourite() fav.character = character FavouriteDao.sharedInstance.addFavourite(characterFavourite: fav)
獲取喜歡的類型
的所有對象func getAllFavourites() - >結果{{0} {0}返回realm.objects(Favourite.s精靈) }
當我拿到項目,並執行打印
Favourite { character = Character { name = Spider-Man; descriptionC = Bitten by a radioactive spider, high school student Peter Parker gained the speed, strength and powers of a spider. Adopting the name Spider-Man, Peter hoped to start a career using his new abilities. Taught that with great power comes great responsibility, Spidey has vowed to use his powers to help people.; thumbnail = Thumbnail { id = 815D93D0-C116-4267-978C-9E47C0074D0D; path = http://i.annihil.us/u/prod/marvel/i/mg/3/50/526548a343e4b; extensionImage = jpg; }; };
如果我試圖訪問它告訴我,這是零
字符元素有人設法理解,因爲如果我打印最喜歡的對象,它會告訴我,有一個字符對象內,但如果我嘗試接受它,它說它不存在?
是的,問題是動態的我以爲如果屬性是對象不需要的動態,好的謝謝。 – Alejandro
'動態'表示變量以'Objective-C'動態方式工作。通過'dynamic',getter和setter被注入,由數據庫提供者進行額外的事情,這裏是Realm。所以你想要在Realm中的所有屬性都應該以'dynamic'開頭,除非Realm明確不要像'List'那樣。如果你不使用'dynamic','var character'只是一個非Realm屬性,根本不會保存到Realm中。 –