首页 > 解决方案 > 指定“UIAlertActionStyle.Default”与“.default”之类的属性之间的区别?

问题描述

我是 Swift 的初学者,不知道如何用谷歌搜索。我知道当您在 Swift 中指定属性时,您可以执行以下任一操作:

UIAlertAction(title: "OK", style: UIAlertActionStyle.Default)

或者

UIAlertAction(title: "OK", style: .default)

有没有办法引用指定属性的每种方式,并且在行为上有什么不同吗?这是 Swift 的事情还是 iOS 特有的事情?文档链接也会有所帮助。提前致谢。

标签: swift

解决方案


首先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
}

推荐阅读