Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift 2.0-二进制运算符“|&引用;无法应用于两个UIUserNotificationType操作数_Ios_Swift_Swift2 - Fatal编程技术网

Ios Swift 2.0-二进制运算符“|&引用;无法应用于两个UIUserNotificationType操作数

Ios Swift 2.0-二进制运算符“|&引用;无法应用于两个UIUserNotificationType操作数,ios,swift,swift2,Ios,Swift,Swift2,我正在尝试通过以下方式注册我的本地通知应用程序: UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil)) 在Xcode 7和Swift 2.0中-I get error二进制运算符“|”不

我正在尝试通过以下方式注册我的本地通知应用程序:

UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

在Xcode 7和Swift 2.0中-I get error
二进制运算符“|”不能应用于两个UIUserNotificationType操作数
。请帮帮我。

在Swift 2中,您通常会这样做的许多类型都已更新,以符合OptionSetType协议。这允许使用类似数组的语法,在您的情况下,可以使用以下语法

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
另一方面,如果要检查选项集是否包含特定选项,则不再需要使用按位And和nil检查。您可以简单地询问选项集是否包含特定值,就像检查数组是否包含值一样

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)

if settings.types.contains(.Alert) {
    // stuff
}
在Swift 3中,必须按照以下方式书写样本:

let settings = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)


您可以编写以下内容:

let settings = UIUserNotificationType.Alert.union(UIUserNotificationType.Badge)

对我有效的是

//This worked
var settings = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)

这已在Swift 3中更新

        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)

用“()”包围我UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes:(UIUserNotificationType.Alert | UIUserNotificationType.Badge),categories:nil)现在我有:
找不到接受提供的参数的重载“|”
我没有其他想法,对不起,哇,这看起来太可怕了<代码>NSTrackingAreaOptions.MouseEnteredAndExited.union(NSTrackingAreaOptions.MouseMoved).union(NSTrackingAreaOptions.ActiveAlways),但感谢您提供了一个有效的解决方案如果我没有错的话,您可以编写
变量选项:NSTrackingAreaOptions=[.MouseenteredExited.MouseMo]‌​ved、.ActiveAlways]
看起来几乎与上面接受的答案完全相同。如果你有“<代码>标志>=.警报?<代码> >,你可以使用<代码>标志= [标志,警报] < /COD> >这是否被视为一个值唯一的集合,还是一个可能导致错误的最终值的数组?@ USSER246173这取决于如何声明标志变量。如果标志的类型显式声明为
UIUserNotificationType
,即
var标志:UIUserNotificationType=[.Alert.Badge]
,那么它将被视为一个集合,您可以通过使用集合实例方法(如
insert()
union()
unionInPlace()
)添加元素,或者使用您提到的方法,而不必担心重复。如果您没有显式地将标志声明为具有类型
UIUserNotificationType
,并在声明中使用类似
var flags=[UIUserNotificationType.Alert,UIUserNotificationType.Badge]
,那么标志的类型将被推断为
[UIUserNotificationType]
,并通过
append()
或其他方法向其添加元素将导致重复。对于后者,您只需使用数组作为输入初始化
UIUserNotificationType
的实例即可,但为了清晰起见,我建议使用基于集合的方法。
        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)