2016-11-07 90 views
7

我需要在我的用戶模型中實現一個協議,它需要有一些特殊的屬性。但我想避免它們被存儲/保存在Realm數據庫中。境界,避免存儲一些屬性

我沒有在文檔中看到是否有關鍵字。避免保存一些屬性有一個竅門嗎?

public final class User: Object, Mappable, AvatarImageViewDataSource { 

dynamic var id: Int = 0 
dynamic var desc: String? = nil 
dynamic var email: String? = nil 
dynamic var firstName: String? = nil 
dynamic var lastName: String? = nil 

... 

public var myPropertyIDontWantToSave: String? = nil // I don't want this to be stored 

回答

14

查看關於Ignored properties的RealmSwift文檔。該部分包含一些示例代碼:

class Person: Object { 
    dynamic var tmpID = 0 
    var name: String { // read-only properties are automatically ignored 
    return "\(firstName) \(lastName)" 
    } 
    dynamic var firstName = "" 
    dynamic var lastName = "" 

    override static func ignoredProperties() -> [String] { 
    return ["tmpID"] 
    } 
}