2016-06-24 32 views
2

私はスウィフトで複合主キーを持つレルムを使用するため、この偉大な解決策が見つかりました:https://github.com/realm/realm-cocoa/issues/1192複合キー

public final class Card: Object { 
    public dynamic var id = 0 { 
     didSet { 
      compoundKey = compoundKeyValue() 
     } 
    } 
    public dynamic var type = "" { 
     didSet { 
      compoundKey = compoundKeyValue() 
     } 
    } 
    public dynamic lazy var compoundKey: String = self.compoundKeyValue() 
    public override static func primaryKey() -> String? { 
     return "compoundKey" 
    } 

    private func compoundKeyValue() -> String { 
     return "\(id)-\(type)" 
    } 
} 

をしかし、私はレルムは怠け者プロパティをサポートしていないことを発見し、私の場合、私はこれを受け取りますエラー:

exception NSException * name: "RLMException" - reason: "Lazy managed property 'compoundKey' is not allowed on a Realm Swift object class. Either add the property to the ignored properties list or make it non-lazy." 0x00007f8a05108060

怠惰な性質のない複合キーを使用することはできますか?

答えて

6

あなたが見つけた解決策は、古くなっています。私はそこに書き留めておきます。 lazy修飾子を削除して、compoundKeyを空の文字列に初期化することをおすすめします。