2016-07-18 170 views
1

我是一個新的Xcode開發,我需要一些幫助與境界數據庫 。這些都是我的表Realm.objects(類型)返回空的對象

class Parent: Object { 
    var id :Int = 0 
    var name: String = "" 
} 

class Surah: Parent { 
    dynamic var ayatNum = 0 

    private var fav : Bool { 
     set { self.fav = newValue } 
     get { return self.fav } 
    } 

    init(id: Int , ayat: Int , name: String) { 
     super.init() 
     super.id = id 
     self.ayatNum = ayat 
     super.name = name 
    } 

    override static func primaryKey() -> String? { 
     return "id" 
    } 

    required init(realm: RLMRealm, schema: RLMObjectSchema) { 
     super.init() 
    } 

    required init(value: AnyObject, schema: RLMSchema) { 
     super.init() 
    } 

    required init() { 
     super.init() 
    } 
} 

class Reader: Parent{ 
    private var fav: Bool { 
     set { self.fav = newValue } 
     get { return self.fav } 
    } 

    init(id: Int , name: String) { 
     super.init() 
     self.id = id 
     self.name = name 

    } 

    override static func primaryKey() -> String? { 
     return "id" 
    } 

    required init(realm: RLMRealm, schema: RLMObjectSchema) { 
     super.init() 
    } 

    required init(value: AnyObject, schema: RLMSchema) { 
     super.init() 
    } 

    required init() { 
     super.init() 
    } 
} 

,當我把這些線和存儲結果在一個數組,並打印

realm = try!Realm() 
readers = Array(realm.objects(Reader.self)) 
print(readers) 

,我嘗試這一個也

readers = Array(try!Realm().objects(Reader.self)) 
print(readers) 

它打印S上的空對象

Reader { 
    id = 0; 
    name = ; 
}, Reader { 
    id = 0; 
    name = ; 
}, Reader { 
    id = 0; 
    name = ; 
} 

我搜索一下這個問題在計算器,找到解決方案,它並沒有解決我的問題

Realm.objects() returns empty objects

任何一個可以幫助我!

回答

3

因爲你Parent類屬性不爲dynamic聲明。 Realm中的大多數屬性必須聲明爲dynamic(例外是List和LinkingObjects)。 Realm懶洋洋地爲性能裝載所有值。在運行時所有屬性訪問都被替換爲專用訪問器。所以你應該聲明屬性爲dynamic。像下面這樣:

class Parent :Object{ 
    dynamic var id: Int=0 
    dynamic var name: String="" 
} 

https://realm.io/docs/swift/latest/#cheatsheet

此外,Surah類和Reader類使用convenience初始化將變得更加簡單。如果是這樣,您不需要覆蓋init(realm: RLMRealm, schema: RLMObjectSchema)init(value: AnyObject, schema: RLMSchema)init()

class Surah :Parent { 
    dynamic var ayatNum = 0 

    private var fav : Bool { 
     set { self.fav = newValue } 
     get { return self.fav } 
    } 

    convenience init(id: Int , ayat:Int , name:String) { 
     self.init() 
     self.id = id 
     self.ayatNum = ayat 
     self.name = name 

    } 
} 

class Reader : Parent { 
    private var fav : Bool { 
     set { self.fav = newValue } 
     get { return self.fav } 
    } 

    convenience init(id: Int , name:String) { 
     self.init() 
     self.id = id 
     self.name = name 
    } 

    override static func primaryKey() -> String? { 
     return "id" 
    } 

} 
+0

thaaanks alot super – Inspiration

-1

試試這個從境界

添加GET對象
let realm = try! Realm() 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // retrieve all objects from realm 
    let readers = realm.objects(Reader) 
    print(readers) 
} 
+0

數據已經在數據庫中,我可以在Realm瀏覽器中看到數據,但是當我嘗試獲取並打印它時,它會打印空屬性! – Inspiration

+0

我更新了我的答案,我始終如此使用那種方式 – xmhafiz

+0

它不能解決我的問題! – Inspiration