Swift 1.2 重新声明 Objective-C 方法 [英] Swift 1.2 redeclares Objective-C method
问题描述
我刚刚从 swift 1.1 更新到 swift 1.2 并得到编译器错误:
I just updated from swift 1.1 to swift 1.2 and get compiler Error:
Method 'setVacation' redeclares Objective-C method 'setVacation:'
这里有一些代码:
var vacation : Vacation?
func setVacation(_vacation : Vacation)
{...}
但我需要调用 setVacation
有什么建议可以解决这个问题吗?
Is any suggestions how fix this?
推荐答案
这是由 Xcode 6.3beta 发行说明中所述的更改引起的:
This is cause by the change stated in Xcode 6.3beta release notes:
Swift 现在检测重载和覆盖之间的差异Swift 类型系统和通过Objective-C 运行时.(18391046, 18383574) 例如以下类中属性"的 Objective-C setter 和其扩展中的setProperty"方法现已诊断:
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:’
}
要解决此问题,您需要使所有方法签名都是唯一的(因为 Objective-C 不提供方法重载)
To fix this you need to make all you method signatures unique (as Objective-C does not provide method overload)
如果您只需要 Swift 类,请不要从 NSObject
继承.
Or don't inherit from NSObject
if you need Swift only class.
这篇关于Swift 1.2 重新声明 Objective-C 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!