2016-07-18 122 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