这会将一个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)呢?
发布于 2015-09-03 18:12:33
[.Alert, .Badge, .Sound]的语法可以用于两种不同的目的。它可以定义Array<UIUserNotificationType>或为UIUserNotificationType创建位掩码填充的OptionTypeSet
结果取决于赋值变量的声明类型。
OptionSetType
let userNotificationTypes: UIUserNotificationType = [.Alert, .Badge, .Sound]
userNotificationTypes实际上是一个OptionSetType位掩码,从C的角度来看,它看起来像(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
数组
let userNotificationTypes: [UIUserNotificationType] = [.Alert, .Badge, .Sound]
userNotificationTypes是UIUserNotificationType类型的数组。
https://stackoverflow.com/questions/32371980
复制相似问题