这会将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)
会编译?
答案 0 :(得分:1)
[.Alert, .Badge, .Sound]
的语法可用于两种不同的目的。它可以为Array<UIUserNotificationType>
OptionTypeSet
或创建位掩码UIUserNotificationType
结果取决于声明的赋值变量类型。
let userNotificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
userNotificationTypes
实际上是一个OptionSetType位掩码,它从C&#39}的角度看起来像(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
let userNotificationTypes: [UIUserNotificationType] = [.Alert, .Badge, .Sound]
userNotificationTypes
是UIUserNotificationType
类型的数组。