xcode - 使用#selector 而不是显式构造选择器

标签 xcode swift

<分区>

在以下代码片段中,Xcode 推荐“使用#selector 而不是显式构造选择器”的原因是什么?

// addButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Add, 
//             target: self, action: #selector(FoldersMaintenanceVC.addButtonPressed))
addButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Add, 
            target: self, action: Selector("addButtonPressed"))

self.navigationItem.leftBarButtonItem = addButton

func addButtonPressed()
{
    myNslogSys2(self, funcName:#function)
}

最佳答案

建议您使用Swift 2.2 new #selector因为它更加类型安全,因为您不能使选择器引用不存在的方法,而使用 Selector(String),您可以引用不存在的方法。

关于xcode - 使用#selector 而不是显式构造选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203746/

相关文章:

objective-c - 将指针分配给另一个指针会崩溃

ios - 实例成员不能用于类型 'ViewController'

ios - 如何以编程方式在 Swift 中创建新的 UIViewController

ios - 在开源解析服务器上实现云代码

swift - UICollectionView 单元格之间的奇怪间隙(Swift)

xcode - 将 Xcode 从 4.3.2 升级到 4.5.2 并安装 Simulator 5.0 或 5.1

ios - Snow Leopard vs. Lion上的iOS Dev:我会失去任何功能吗?

objective-c - Xcode force_load 不适用于模拟器构建

ios - 自动保存在 NSStrings 中

swift - 在 swift iOS 中手动添加 FontAwesome