Swift 1.2 redeclares Objective-C method

前端 未结 3 1611
青春惊慌失措
青春惊慌失措 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 11:58

    This is cause by the change stated in Xcode 6.3beta release notes:

    Swift now detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime. (18391046, 18383574) For example, the following conflict between the Objective-C setter for “property” in a class and the method “setProperty” in its extension is now diagnosed:

     class A : NSObject {
         var property: String = "Hello" // note: Objective-C method 'setProperty:’
                                        // previously declared by setter for
                                        // 'property’ here
     }
     extension A {
         func setProperty(str: String) { } // error: method ‘setProperty’
                                           // redeclares Objective-C method
                                           //'setProperty:’
     }
    

    To fix this you need to make all you method signatures unique (as Objective-C does not provide method overload)

    Or don't inherit from NSObject if you need Swift only class.

提交回复
热议问题