Search code examples
swiftpush-notificationparse-platformxcode6xcode6-beta6

XCode6 beta 6 Swift Compiler Error


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


Solution

  • 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 {
        // ...
    }