致命错误:对类使用未实现的初始化程序'init()'

时间:2016-12-12 00:09:12

标签: swift realm

我在混合项目中使用Realm-Cocoa。有一些令人头疼的事,但现在我真的陷入了困境。我尝试了几件事,但没有任何帮助。 这个类编译没有任何抱怨,但在运行时崩溃:

致命错误:对“MyApp.Model”类使用未实现的初始化程序'init()'

这是班级:

import Foundation
import Realm
import SwiftyJSON


class Model: RLMObject {
    dynamic var contactId: String
    dynamic var userId: String
    dynamic var contactType: String
    dynamic var firstName: String
    dynamic var lastName: String
    dynamic var middleName: String
    dynamic var jobTitle:String
    dynamic var department: String
    dynamic var location: String
    dynamic var serviceName: String
    dynamic var serviceType : String
    dynamic var emails : String
    dynamic var addresses :String
    dynamic var profilePictureSHA1 : String
    dynamic var extNum : String
    dynamic var fqExtNum : String
    dynamic var ipbxId : String
    dynamic var phone : String
    dynamic var fax : String
    dynamic var dispWhenNoext : Bool
    dynamic var dispInContDir : Bool


    override class func primaryKey() -> String? {
        return "contactId"
    }



    init(
        contactId: String,
        userId: String,
        contactType: String, 
        firstName: String, 
        lastName: String, 
        middleName: String, 
        jobTitle: String, 
        department: String, 
        location: String, 
        serviceName: String, 
        serviceType: String, 
        emails: String, 
        addresses: String, 
        profilePictureSHA1: String, 
        dispWhenNoext: Bool, 
        dispInContDir: Bool, 
        extNum: String, 
        fqExtNum: String, 
        ipbxId: String, 
        phone: String, 
        fax: String)


    {
        self.contactId = contactId 
        self.userId = userId
        self.contactType = contactType
        self.firstName = firstName
        self.lastName = lastName
        self.middleName = middleName
        self.jobTitle = jobTitle
        self.department = department
        self.location = location
        self.serviceName = serviceName
        self.serviceType = serviceType
        self.emails = emails
        self.addresses = addresses
        self.profilePictureSHA1 = profilePictureSHA1
        self.dispInContDir = dispInContDir
        self.dispWhenNoext = dispWhenNoext 
        self.extNum = extNum
        self.fqExtNum = fqExtNum
        self.ipbxId = ipbxId
        self.phone = phone
        self.fax = fax

        super.init()
    }


    override static func ignoredProperties() -> [String] {
        return ["contactFlag", "dttmCreated", "nickName", "customField1", "customField2", "customField3", "serviceId", "parentServiceId", "branchId", "hideInAA", "vooEnableFlag", "devLockDown", "tenantId", "serviceEmailId", "siteCustomerId", "branchCode", "accessCode"]
    }
}

1 个答案:

答案 0 :(得分:2)

https://realm.io/docs/swift/latest/#adding-custom-initializers-to-object-subclasses

上查看自定义子类初始化器和Swift内省问题的Realm doco

根据那个doco,你应该把它变成一种方便的方法,而不是指定的初始化方法。无论如何,值得一试。类似的东西:

convenience init(
    contactId: String,
    userId: String,
    contactType: String, 
    firstName: String, 
    lastName: String, 
    middleName: String, 
    jobTitle: String, 
    department: String, 
    location: String, 
    serviceName: String, 
    serviceType: String, 
    emails: String, 
    addresses: String, 
    profilePictureSHA1: String, 
    dispWhenNoext: Bool, 
    dispInContDir: Bool, 
    extNum: String, 
    fqExtNum: String, 
    ipbxId: String, 
    phone: String, 
    fax: String)
{
    self.init()  //  NB:  'self' not 'super'

    self.contactId = contactId 
    self.userId = userId
    self.contactType = contactType
    self.firstName = firstName
    self.lastName = lastName
    self.middleName = middleName
    self.jobTitle = jobTitle
    self.department = department
    self.location = location
    self.serviceName = serviceName
    self.serviceType = serviceType
    self.emails = emails
    self.addresses = addresses
    self.profilePictureSHA1 = profilePictureSHA1
    self.dispInContDir = dispInContDir
    self.dispWhenNoext = dispWhenNoext 
    self.extNum = extNum
    self.fqExtNum = fqExtNum
    self.ipbxId = ipbxId
    self.phone = phone
    self.fax = fax
}