Swift 1.2 redeclares Objective-C method

前端 未结 3 1564
青春惊慌失措
青春惊慌失措 2021-02-20 11:29

I just updated from swift 1.1 to swift 1.2 and get compiler Error:

Method \'setVacation\' redeclares Objective-C method \'setVacation:\'

Here

3条回答
  •  梦谈多话
    2021-02-20 12:08

    As noted by @Kirsteins, Swift now detects conflicting symbols between Swift and Obj-C, and swift symbols that would cause Obj-C grief. In addition to the answer given, you can avoid this in general by specifying a required label for the additional types, thus changing the call signature:

    import Foundation
    
    extension NSObject {
        func foo(d:Double, i:Int) { println("\(d), \(i)") }
        func foo(withInt d:Int, i:Int) { println("\(d), \(i)") }
    }
    
    let no = NSObject()
    no.foo(withInt:1, i: 2)
    

    Beyond that though, and to answer your immediate question, you are trying to apply Obj-C idioms to Swift. What you really want, is to either implement didSet (most likely), or possibly set:

    class WhatIDidLastSummer {
    
        var vacation:Bool = false {
            didSet {
                // do something
            }
        }
    
        var staycation:Bool {
            get { return true }
            set {
                // do something
            }
        }
    
    }
    

提交回复
热议问题