首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift类型推断混用?

Swift类型推断混用?
EN

Stack Overflow用户
提问于 2015-09-03 17:35:09
回答 1查看 126关注 0票数 0

这会将一个UIUserNotificationType数组赋给UIUserNotificationType的一个变量。这应该不起作用:

1)

代码语言:javascript
运行
复制
var userNotificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
print(userNotificationTypes.dynamicType) // UIUserNotificationType

下面是UIUserNotificationType的类型

2)

代码语言:javascript
运行
复制
let intermidiate = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
print(intermidiate.dynamicType) // Array<UIUserNotificationType>

如预期的那样,尝试分配它失败:

3)

代码语言:javascript
运行
复制
userNotificationTypes = intermidiate // Cannot assign a value of type '[UIUserNotificationType]' to a value of type 'UIUserNotificationType'

尝试将[UIUserNotificationType]赋值给UIUserNotificationType显然不起作用,那么为什么要编译1)呢?

EN

回答 1

Stack Overflow用户

发布于 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]

userNotificationTypesUIUserNotificationType类型的数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32371980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档