简体   繁体   English

访问UILocalNotification用户信息时出现Swift错误

[英]Swift error when accessing UILocalNotification userinfo

I am trying to retrieve the currently scheduled UILocalNotifications. 我正在尝试检索当前计划的UILocalNotifications。 I get an error when I try to implicitly unwrap userInfo from the notification. 尝试从通知中隐式解包userInfo时出现错误。 The error happens on the line 错误发生在线

          if let info = notification.userInfo

The error says "Could not find an overload for 'userInfo' that accepts the supplied arguments" 错误显示“找不到'userInfo'的重载,该重载接受提供的参数”

I also tried doing optional chaining with 我也尝试过使用

if let info = notification.userInfo?["Id"]

And I got an error that userInfo does not accept subscripts. 而且我收到一个错误,指出userInfo不接受下标。

It worked when I used explicit unwrapping after userinfo with 当我在userinfo之后使用显式解包时,它起作用了

if let info = notification.userInfo!

But I wanted to avoid doing that. 但我想避免这样做。 Any help would be great. 任何帮助都会很棒。 The full method is below. 完整的方法如下。

func test()
{

  let application = UIApplication.sharedApplication()
  let scheduledNotifications = application.scheduledLocalNotifications

  for notification in scheduledNotifications
  {
    println( "found it" ) 

      if let info = notification.userInfo
      {
        println( "in")
      } else {
        // no userInfo dictionary present
        println( "else")
      }
  }
}

application.scheduledLocalNotifiactions提供了一个AnyObject数组,因此您缺少转换为UILocalNotification的类型

 let scheduledNotifications = application.scheduledLocalNotifications as [UILocalNotification]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM