首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift2 NotificationType选项错误

Swift2 NotificationType选项错误
EN

Stack Overflow用户
提问于 2015-06-09 18:50:26
回答 2查看 933关注 0票数 2

将Swift1.2突变为Swift2后,有一个错误..。不知道如何修复它,smb在Swift2中尝试了这一点?

代码语言:javascript
复制
func application(application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->        Bool {

        let notificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
        application.registerUserNotificationSettings(settings)
        return true
            }

编辑:

错误:“无法找到接受类型参数列表的'UIUserNotificationSettings‘类型的初始化项”(forTypes:[UIUserNotifivati.“)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-13 17:32:26

只需从代码中更改此部分即可。

代码语言:javascript
复制
let notificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]

转入:

代码语言:javascript
复制
let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]
票数 4
EN

Stack Overflow用户

发布于 2015-07-06 22:13:45

不知道为什么人们不阅读发布说明或文档(UIUserNotificationTypeOptionSetType)。

下面是发布说明的摘录:

NS_OPTIONS类型是按照OptionSetType协议导入的,该协议为选项提供了一个类似集合的接口。(18069205)而不是使用按位操作,例如:

代码语言:javascript
复制
// Swift 1.2:
object.invokeMethodWithOptions(.OptionA | .OptionB)
object.invokeMethodWithOptions(nil)
if options & .OptionC == .OptionC {
  // .OptionC is set
}

选项集支持集文本语法和类似集合的方法(如contains )。

代码语言:javascript
复制
object.invokeMethodWithOptions([.OptionA, .OptionB])
object.invokeMethodWithOptions([])
if options.contains(.OptionC) {
  // .OptionC is set
}

可以使用Swift作为符合OptionSetType协议的结构来编写新的选项集类型。如果类型将rawValue属性和选项常量指定为static let常量,标准库将提供其他选项集API的默认实现:

代码语言:javascript
复制
struct MyOptions: OptionSetType {
  let rawValue: Int
  static let TuringMachine  = MyOptions(rawValue: 1)
  static let LambdaCalculus = MyOptions(rawValue: 2)
  static let VonNeumann     = MyOptions(rawValue: 4)
}
let churchTuring: MyOptions = [.TuringMachine, .LambdaCalculus]

正如@iEmad所写的,只需将代码的一行更改为:

代码语言:javascript
复制
let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]

你怎么能自己找到这个?错误是。

错误:“无法找到接受类型的参数列表的'UIUserNotificationSettings‘类型的初始化项.

..。这基本上意味着你要把无效的参数传递给初始化器。什么是正确的论点?同样,文件:

代码语言:javascript
复制
convenience init(forTypes types: UIUserNotificationType,
      categories categories: Set<UIUserNotificationCategory>?)

让我们从最后一个开始- categories。您正在传递nil,这是可以的,因为categories类型是Set<UIUserNotificationCategory>? =可选= nil是可以的。

所以,问题在于第一个问题。回到我们可以找到UIUserNotificationType声明的文档:

代码语言:javascript
复制
struct UIUserNotificationType : OptionSetType {
    init(rawValue rawValue: UInt)
    static var None: UIUserNotificationType { get }
    static var Badge: UIUserNotificationType { get }
    static var Sound: UIUserNotificationType { get }
    static var Alert: UIUserNotificationType { get }
}

嗯,它采用OptionSetType__。在Swift 1.2中没有看到这一点,它肯定是新的东西。让我们打开https://developer.apple.com/library/prerelease/mac/documentation/Swift/Reference/Swift_OptionSetType_Protocol/index.html并了解更多有关它的信息。啊,有趣,很好,我必须修改我的代码。

请开始阅读发布说明和文档。你会节省一些时间的。

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

https://stackoverflow.com/questions/30740410

复制
相关文章

相似问题

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