UIRemoteNotificationType invalid conversion

2020-07-13 07:57发布

I'm trying to use this fairly standard line of code in my app:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

But am receiving the follow error:

error: invalid conversion from 'int' to 'UIRemoteNotificationType'

It works if I only use one of the notification types but fails every time if I try and use more than one. Any ideas what I'm doing wrong?

3条回答
贼婆χ
2楼-- · 2020-07-13 08:23

Use this: This will solve ur problem.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationType)(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
查看更多
看我几分像从前
3楼-- · 2020-07-13 08:30

You have to cast the result as UIRemoteNotificationType:

(UIRemoteNotificationType)(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)

This way the method got what it is expecting.

查看更多
神经病院院长
4楼-- · 2020-07-13 08:32

You're probably using Objective-C++, which implicit conversion from int to an enum is disallowed.

Try to add an explicit cast:

[… registerForRemoteNotificationTypes:
     (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert | …)];
查看更多
登录 后发表回答