0

这会将 UIUserNotificationType 数组分配给 UIUserNotificationType 变量。这不应该工作:

1)

var userNotificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
print(userNotificationTypes.dynamicType) // UIUserNotificationType

这是 [UIUserNotificationType] 的类型

2)

let intermidiate = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
print(intermidiate.dynamicType) // Array<UIUserNotificationType>

尝试分配它按预期失败:

3)

userNotificationTypes = intermidiate // Cannot assign a value of type '[UIUserNotificationType]' to a value of type 'UIUserNotificationType'

尝试分配[UIUserNotificationType]显然UIUserNotificationType是行不通的,那么为什么要1)编译呢?

4

1 回答 1

1

for 的语法[.Alert, .Badge, .Sound]可用于 2 个不同的目的。它可以定义或创建填充的Array<UIUserNotificationType>位掩码OptionTypeSetUIUserNotificationType

结果取决于声明的赋值变量类型。

选项集类型

let userNotificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]

userNotificationTypes实际上是一个 OptionSetType 位掩码,从 C 的角度来看是这样的(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)

大批

let userNotificationTypes: [UIUserNotificationType] = [.Alert, .Badge, .Sound]

userNotificationTypesUIUserNotificationType类型数组。

于 2015-09-03T10:12:33.430 回答