2017-04-19 78 views
0

我正在將一個項目遷移到Swift 3,並遇到了RealmSwift(2.6.1)和Genome(3.2.0)的一些問題。我得到在Xcode是說我需要這些inits的境界錯誤:RealmSwift也要求Realm也

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

required convenience init(value: Any, schema: RLMSchema) { 
    self.init(value: value, schema: schema) 
} 

然而,這需要除進口境界到RealmSwift,當我的類初始化它試圖使用RLMRealm代替領域。警告說'必需的'初始化'init(realm:schema :)'必須由'Object'的子類提供,但是那需要init使用RLMRealm而不是Realm任何建議?

我使用的基因組,以及需要這個初始化,這就是爲什麼境界是要求擺在首位初始化:

required convenience init(node: Node, in context: Context) throws { 
    self.init() 
} 

所以inits看起來像這樣一起:

class BaseModel : RealmSwift.Object, MappableBase, Identifiable { 

required init() { 
    super.init() 
} 

required convenience init(node: Node, in context: Context) throws { 
    self.init() 
} 

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

required convenience init(value: Any, schema: RLMSchema) { 
    self.init(value: value, schema: schema) 
} 

在Swift 2.3(使用Realm和Genome的相應Swift 2.3版本)中,沒有任何這些初始化器都能正常工作,但現在它不起作用。

整個模型:

import RealmSwift 
import Genome 
import Realm 

protocol Identifiable { 
    var identifier: String { get set } 
} 


class BaseModel : RealmSwift.Object, MappableBase, Identifiable { 

required init() { 
    super.init() 
} 

required convenience init(node: Node, in context: Context) throws { 
    try self.init(node: node, in: context) 
} 

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

required convenience init(value: Any, schema: RLMSchema) { 
    self.init(value: value, schema: schema) 
} 

dynamic var identifier = "" 
dynamic var updatedAt: Date? 

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

static func newInstance(_ node: Node, context: Context = EmptyNode) throws -> Self { 
    let map = Map(node: node, in: context) 
    let new = self.init() 
    try new.sequence(map) 
    return new 
} 

func sequence(_ map: Map) throws { 
    switch map.type { 
    case .fromNode: 
     if self.identifier.isEmpty { 
      // only map id if there isn't one, otherwise Realm complains about modified primaryKey 
      try self.identifier <~ map["id"] 
     } 
     updatedAt = Date() 
    case .toNode: 
     if !self.identifier.isEmpty { 
      try self.identifier ~> map["id"] 
     } 
    } 
} 

func objectRepresentation() -> [String : AnyObject] { 
    if let result = try? self.toObject() { 
     return result as? [String : AnyObject] ?? [:] 
    } else { 
     return [:] 
    } 
} 

static func objectInRealm(_ realm: Realm, identifier: String?) -> Self? { 
    if let identifier = identifier { 
     return realm.object(ofType: self, forPrimaryKey: identifier) 
    } else { 
     return nil 
    } 
} 

static func createOrFindObject(inRealm realm: Realm, identifier: String) -> Self { 
    if let foundObject = realm.object(ofType: self, forPrimaryKey: identifier) { 
     return foundObject 
    } else { 
     return realm.create(self, value: ["identifier" : identifier], update: false) 
    } 
} 
} 
+0

您是否碰巧在類中聲明瞭任何非可選的,未初始化的屬性? – NRitH

+0

不,我檢查過,但已經很好的想法。 – charliework

+0

通常情況下,只有當您的class_中的某些東西需要初始化時,需要重寫所需的初始化器,如屬性。 – NRitH

回答

1

你並不需要重寫init(realm:schema:)init(realm:schema:),只是定義convenience required init(node:in:) throws像下面這樣。

class BaseModel : RealmSwift.Object, MappableBase, Identifiable { 

    convenience required init(node: Node, in context: Context) throws { 
     try self.init(node: node, in: context) 
    } 

    ... 

}