将Swift1.2突变为Swift2后,有一个错误..。不知道如何修复它,smb在Swift2中尝试了这一点?
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.“)
发布于 2015-06-13 17:32:26
只需从代码中更改此部分即可。
let notificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]转入:
let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]发布于 2015-07-06 22:13:45
不知道为什么人们不阅读发布说明或文档(UIUserNotificationType,OptionSetType)。
下面是发布说明的摘录:
NS_OPTIONS类型是按照OptionSetType协议导入的,该协议为选项提供了一个类似集合的接口。(18069205)而不是使用按位操作,例如:
// Swift 1.2:
object.invokeMethodWithOptions(.OptionA | .OptionB)
object.invokeMethodWithOptions(nil)
if options & .OptionC == .OptionC {
// .OptionC is set
}选项集支持集文本语法和类似集合的方法(如contains )。
object.invokeMethodWithOptions([.OptionA, .OptionB])
object.invokeMethodWithOptions([])
if options.contains(.OptionC) {
// .OptionC is set
}可以使用Swift作为符合OptionSetType协议的结构来编写新的选项集类型。如果类型将rawValue属性和选项常量指定为static let常量,标准库将提供其他选项集API的默认实现:
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所写的,只需将代码的一行更改为:
let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]你怎么能自己找到这个?错误是。
错误:“无法找到接受类型的参数列表的'UIUserNotificationSettings‘类型的初始化项.
..。这基本上意味着你要把无效的参数传递给初始化器。什么是正确的论点?同样,文件:
convenience init(forTypes types: UIUserNotificationType,
categories categories: Set<UIUserNotificationCategory>?)让我们从最后一个开始- categories。您正在传递nil,这是可以的,因为categories类型是Set<UIUserNotificationCategory>? =可选= nil是可以的。
所以,问题在于第一个问题。回到我们可以找到UIUserNotificationType声明的文档:
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并了解更多有关它的信息。啊,有趣,很好,我必须修改我的代码。
请开始阅读发布说明和文档。你会节省一些时间的。
https://stackoverflow.com/questions/30740410
复制相似问题