最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift出错:Method with Objective-C selector conflicts with method from superclass with the same Objective-C selector

Swift crifan 986浏览 0评论

Method with Objective-C selector conflicts with method from superclass with the same Objective-C selector

相关代码:

class ContactItem:NSObject, NSCoding {
    var type:ContactType
    // person/group/topic id string
    var id:String
    // person name/group title/topic title
    var name:String
    var headerBkgColor:UIColor
    //30×30, person/group/topic header image
    var headerImage:UIImage
    //41×41, for conversation/personal info/… use, larger size image
    var headerImageLarge:UIImage
   
    //used for SelectPersonViewController
    var selected:Bool
    func update(newContactItem:ContactItem){
        self.type              = newContactItem.type
        self.id                = newContactItem.id
        self.name              = newContactItem.name
        self.headerBkgColor    = newContactItem.headerBkgColor
        self.headerImage        = newContactItem.headerImage
        self.headerImageLarge  = newContactItem.headerImageLarge
        self.selected          = newContactItem.selected
    }
}
class PersonItem: ContactItem {
    var personType:PersonType
    var phoneNumber:String
    var email:String
    var position:String
。。。
   
    func update(newPersonItem:PersonItem){
        super.update(newPersonItem)
        self.personType    = newPersonItem.personType
        self.phoneNumber    = newPersonItem.phoneNumber
        self.email          = newPersonItem.email
        self.position      = newPersonItem.position
    }
}

但是去给子类加上override,也还是不行:

    override func update(newPersonItem:PersonItem){
        super.update(newPersonItem)
        self.personType    = newPersonItem.personType
        self.phoneNumber    = newPersonItem.phoneNumber
        self.email          = newPersonItem.email
        self.position      = newPersonItem.position
    }

Method does not override any method from its superclass

搜:

swift Method with Objective-C selector conflicts with method from superclass with the same Objective-C selector

swift – Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector – Stack Overflow

由于(为了支持NSCoding而)继承了NSObject,导致了其自动变成ObjC(而不是Swift,其本身支持重载的同名函数)

所以:

如果只是去改变函数名字,比如

父类为update

子类为updateInfo

倒是可以解决问题,但是不是我要的:

希望函数名都一样。。

还是private试试:

class ContactItem:NSObject, NSCoding {
。。。
    private func update(newContactItem:ContactItem){
        self.type              = newContactItem.type
        self.id                = newContactItem.id
        self.name              = newContactItem.name
        self.headerBkgColor    = newContactItem.headerBkgColor
        self.headerImage        = newContactItem.headerImage
        self.headerImageLarge  = newContactItem.headerImageLarge
        self.selected          = newContactItem.selected
    }
}
class PersonItem: ContactItem {
    private func update(newPersonItem:PersonItem){
        super.update(newPersonItem)
        self.personType    = newPersonItem.personType
        self.phoneNumber    = newPersonItem.phoneNumber
        self.email          = newPersonItem.email
        self.position      = newPersonItem.position
    }
}

果然可以了,没有错误了。

[总结]

如果父类是Object-C的话

比如我此处的ContactItem由于为了支持NSCoding而继承了NSObject, NSCoding,从而导致ContactItem从Swift变成了Object-C类型了。

则就是:

子类没发使用和父类同名的函数(即使两个同名函数的参数不同)

解决办法是:

把父类和子类的同名函数都加上private

-》使得每个函数都是当前类的私有函数

-》子类就不知道父类有对应函数,子类就可以实现自己的同名的函数了。

但是却导致:

外部无法调用该函数了:

所以还要想其他办法

去加上@nonobjc试试:

然后就可以了:

class ContactItem:NSObject, NSCoding {
。。。
    @nonobjc
    func update(newContactItem:ContactItem){
        self.type              = newContactItem.type
        self.id                = newContactItem.id
        self.name              = newContactItem.name
        self.headerBkgColor    = newContactItem.headerBkgColor
        self.headerImage        = newContactItem.headerImage
        self.headerImageLarge  = newContactItem.headerImageLarge
        self.selected          = newContactItem.selected
    }
}
class PersonItem: ContactItem {
 。。。
    @nonobjc
    func update(newPersonItem:PersonItem){
        super.update(newPersonItem)
        self.personType    = newPersonItem.personType
        self.phoneNumber    = newPersonItem.phoneNumber
        self.email          = newPersonItem.email
        self.position      = newPersonItem.position
    }
}

外部也可以调用此函数了:

    func updatePersonInfo(oldPersonItem:PersonItem, newPersonItem: PersonItem) {
        //copy all new info
        oldPersonItem.update(newPersonItem)
       
        SingletonContactVC().saveContactItemToCache(oldPersonItem)
    }

转载请注明:在路上 » [已解决]swift出错:Method with Objective-C selector conflicts with method from superclass with the same Objective-C selector

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.122 seconds, using 18.87MB memory