-3

I have the following method:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    //print(userInfo)
    // Print APS.
    let aps = userInfo["aps"]
    let itemId = aps!["category"] as! String
    print("ITEM ID: \(itemId)")
    let temp = Int(itemId)
    if (temp > 0) {
        print("handle item details”)
    } else {
        print("open home")
    }
}

The line let aps = userInfo["aps"] gives the following error

Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String'

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • 2
    Start by using the proper signature for `didReceiveRemoteNotification`. Please review the UIApplicationDelegate documentation. – rmaddy Jul 20 '18 at 17:42
  • 2
    Possible duplicate of [Cannot subscript a value of type '\[NSObject : AnyObject\]' with an index of type 'String'](https://stackoverflow.com/questions/50141190/cannot-subscript-a-value-of-type-nsobject-anyobject-with-an-index-of-type) – Satachito Jul 20 '18 at 17:43

2 Answers2

2

You can try

 func application(_ application: UIApplication, 
   didReceiveRemoteNotification userInfo: [AnyHashable : Any], 
    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

    if let aps = userInfo["aps"] as? [String:Any] {

    }
 }

//

OR

func  application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

I suggest you to define your variables type if you don't know what type it will get.

You need to declare aps by your expected type:

let aps : [String:Any]? 

In this case, if you couldn’t initialize aps variable, Xcode will tell you how you can do your job. Not always, but most of time.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mohammad Reza Koohkan
  • 1,656
  • 1
  • 16
  • 36