IBOutlet 和 IBAction 的名称冲突?

Conflicting names of IBOutlet and IBAction?

我有以下从 UICollectionViewCell class 到故事板上单元格中的按钮的引用。点击按钮不触发'setFavorite'.

class StationCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var favorite: UIButton!

    @IBAction func setFavorite(sender: AnyObject) {
       println("Button clicked")
    }
}

但是当我将动作函数或出口变量的名称更改为其他名称(例如更改为 'setAsFavorite' 或 'favoriteButton')时,它起作用了。

Swift 的 xCode 是否有一些 'magic' 阻止我同时拥有名为 'favorite' 的插座和名为 'setFavorite' 的操作

魔法属于NSObject。名为 favourite 的 属性 有两个访问器方法:favourite,returns 属性 值,以及 setFavourite:,它设置值。使用上面的代码,您打破了这个假设,这就是更改方法名称有效的原因。