Xcode 7 中的向下转换错误
Downcast error in Xcode 7
在 xcode 6 中,此代码运行良好,但在 Xcode 7GM 中,我收到一条错误消息:
Downcast from ‘[UILocalNotification]? to ‘[UILocalNotification]’ only unwraps optionals; did you mean to use ‘!’?
错误发生在我放了两个星号的那一行。在Xcode中,as!
部分的a
下面也有一个小红三角。
func removeItem(item: TodoItem) {
**for notification in (UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification]) { // loop through notifications...
if (notification.userInfo!["UUID"] as! String == item.UUID) { // ...and cancel the notification that corresponds to this TodoItem instance (matched by UUID)
UIApplication.sharedApplication().cancelLocalNotification(notification) // there should be a maximum of one match on UUID
break
}
}
在Xcode 6中scheduledLocalNotifications
声明为[AnyObject]!
(需要向下转换)。
在 Xcode 7 中,scheduledLocalNotifications
被声明为 [UILocalNotification]?
(只需要展开)
我建议使用可选绑定
func removeItem(item: TodoItem) {
if let scheduledLocalNotifications = UIApplication.sharedApplication().scheduledLocalNotifications {
for notification in scheduledLocalNotifications { // loop through notifications...
if (notification.userInfo!["UUID"] as! String == item.UUID) { // ...and cancel the notification that corresponds to this TodoItem instance (matched by UUID)
UIApplication.sharedApplication().cancelLocalNotification(notification) // there should be a maximum of one match on UUID
break
}
}
}
在 xcode 6 中,此代码运行良好,但在 Xcode 7GM 中,我收到一条错误消息:
Downcast from ‘[UILocalNotification]? to ‘[UILocalNotification]’ only unwraps optionals; did you mean to use ‘!’?
错误发生在我放了两个星号的那一行。在Xcode中,as!
部分的a
下面也有一个小红三角。
func removeItem(item: TodoItem) {
**for notification in (UIApplication.sharedApplication().scheduledLocalNotifications as! [UILocalNotification]) { // loop through notifications...
if (notification.userInfo!["UUID"] as! String == item.UUID) { // ...and cancel the notification that corresponds to this TodoItem instance (matched by UUID)
UIApplication.sharedApplication().cancelLocalNotification(notification) // there should be a maximum of one match on UUID
break
}
}
在Xcode 6中scheduledLocalNotifications
声明为[AnyObject]!
(需要向下转换)。
在 Xcode 7 中,scheduledLocalNotifications
被声明为 [UILocalNotification]?
(只需要展开)
我建议使用可选绑定
func removeItem(item: TodoItem) {
if let scheduledLocalNotifications = UIApplication.sharedApplication().scheduledLocalNotifications {
for notification in scheduledLocalNotifications { // loop through notifications...
if (notification.userInfo!["UUID"] as! String == item.UUID) { // ...and cancel the notification that corresponds to this TodoItem instance (matched by UUID)
UIApplication.sharedApplication().cancelLocalNotification(notification) // there should be a maximum of one match on UUID
break
}
}
}