swift - 指定“UIAlertActionStyle.Default”与“.default”之类的属性之间的区别?
问题描述
我是 Swift 的初学者,不知道如何用谷歌搜索。我知道当您在 Swift 中指定属性时,您可以执行以下任一操作:
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)
或者
UIAlertAction(title: "OK", style: .default)
有没有办法引用指定属性的每种方式,并且在行为上有什么不同吗?这是 Swift 的事情还是 iOS 特有的事情?文档链接也会有所帮助。提前致谢。
解决方案
首先UIAlertActionStyle.Default
改为UIAlertAction.Style.default
而且这个是swift特有的,写法没有区别
UIAlertAction (title: "OK", style: UIAlertAction.Style.default)
和
UIAlertAction (title: "OK", style: .default)
这只是编写第二种方法的快捷方式
以枚举为例:
enum MyEnum {
case first, second
}
如果我们想进行如下比较:
let myEnum: MyEnum = .first
if myEnum == .first {
// Action
}
它比以下更具可读性:
let myEnum = MyEnum.first
if myEnum == MyEnum.first {
// Action
}
推荐阅读
- node.js - 服务器不发送对 Sequelize 多对多关系查询的响应
- java - IBM Watson 语音转文本方法返回 okhttp3.internal.http2.StreamResetException:流被重置而不是响应
- assembly - 什么定义了典型的内存布局?
- python - numpy 矩阵中的 Lambda 函数
- python - 如何更改 Jinja 中子模板的背景颜色?
- reactjs - 我在本机反应中不断收到此导航错误
- java - 尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.removeViewAt(int)”
- php - 根据选定的 div 进行下拉选择(使用 jQuery)
- php - 是否有一个功能可以让您知道您来自网站的哪个页面?
- markdown - 使用 jekyll 的“包含”和备注