1

I started to watch a YouTube Swift tutorial from user Brian Advent, in specific this tutorial about Remote Push Notification using Parse https://www.youtube.com/watch?v=__zMnlsfwj4 . After downloading the sample app and opening it on Xcode 6 beta 6 the compiler shows one error in the code below:

func application(application: UIApplication!, didReceiveRemoteNotification userInfo:NSDictionary!) {

        var notification:NSDictionary = userInfo.objectForKey("aps") as NSDictionary

        if (notification.objectForKey("content-available") != nil){
            if notification.objectForKey("content-available").isEqualToNumber(1){
                NSNotificationCenter.defaultCenter().postNotificationName("reloadTimeline", object: nil)
            }
        }else{
            PFPush.handlePush(userInfo)
        }

The error is in this line below inside AppDelegate.swift

if notification.objectForKey("content-available").isEqualToNumber(1){

And the message showed is 'AnyObject?' does not have a member named 'isEqualToNumber'

Any tip/help on how to fix this ? I would be thankfu

Sreeraj VR
  • 1,524
  • 19
  • 35

1 Answers1

1

objectForKey() returns an optional AnyObject?, so you have to unwrap it explicitly with ! :

if notification.objectForKey("content-available")!.isEqualToNumber(1) { ...

Alternatively, use an optional assignment. If combined with an optional cast (as?) you can even use the fact that NSNumber is automatically bridged to native number types:

if let contentAvailable = notification.objectForKey("content-available") as? NSNumber {
    if contentAvailable == 1 {
       // ...
    }
} else {
    // ...
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Could you take a look below in the new errors I got trying o fix those first ones above –  Aug 22 '14 at 01:10