2017-04-19 14 views
0

프로젝트를 Swift 3으로 마이그레이션 중이며 RealmSwift (2.6.1) 및 Genome (3.2.0)에서 몇 가지 문제가 발생했습니다. RealmSwift 이외에 영역을 가져올 것을 요구, 그러나RealmSwift에 영역도 필요합니다.

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) 
} 

, 내 클래스가 초기화 될 때 대신 RLMRealm를 사용하려고 : 나는이 inits 필요하다고 엑스 코드의 영역에 대한 오류를 받고 있어요 왕국. 경고는 '필수'이니셜 라이저 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에서는 Realim과 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

일반적으로 속성과 같이 클래스 _에서 초기화해야하는 경우에만 필요한 초기화 프로그램을 재정의해야합니다. – NRitH

답변

1

당신은 다음과 같은 convenience required init(node:in:) throws을 정의 init(realm:schema:)init(realm:schema:)를 오버라이드 (override) 할 필요가 없습니다.

class BaseModel : RealmSwift.Object, MappableBase, Identifiable { 

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

    ... 

}