Swift 1.2 重新声明 Objective-C 方法

标签 swift properties redeclaration

我刚刚从 swift 1.1 更新到 swift 1.2 并得到编译器错误:

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

这里是一些代码:

var vacation : Vacation?  
func setVacation(_vacation : Vacation)
{...}

但我需要调用setVacation

有什么解决办法的建议吗?

最佳答案

这是由 Xcode 6.3beta 发行说明中所述的更改引起的:

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 不提供方法重载)

或者如果您只需要 Swift 类,则不要继承 NSObject

关于Swift 1.2 重新声明 Objective-C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28500486/

相关文章:

c# 线程安全属性,没有锁的 getter 怎么样?

Python:如何将多个参数传递给属性 getter?

c# - 我如何传入 DateTime 的参数,使其对应于 C# 中所需的形式参数

javascript - 重新声明 var console

ios - 为什么默认情况下符合 Swift 协议(protocol)的值被视为值类型?

ios - iOS10点击时UISearchBar位置下移

php - PHP 有时会忽略函数重新声明吗?

c++使用* lec = fopen()修复语法调试;

swift - 在 SpriteKit Swift 中移动相机

ios - 如何使用 init :patternImage 获取具有背景颜色设置的 View 的背景图像