1
votes

I have this code in Swift 1.2:

self.publicDatabase!.performQuery(query, inZoneWithID: nil) {
            results, error in
            if error != nil {
                NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveFailed, userinfo: ["result":results, "error":error])
            }
            else
            {
                NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveSuccess, userinfo: ["result":results])
            }
        }

But in Swift 2.0 this yields several compiler errors:

"Type of expression is ambiguous without more context"

and

"Cannot convert value of type '[String : [CKRecord]?]' to expected argument type '[NSObject : AnyObject]?'"

I know how to fix it to make the error go away, but it seems very ugly and hacky:

self.publicDatabase!.performQuery(query, inZoneWithID: nil) {
            results, error in
            if error != nil {
                NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveFailed, userinfo: ["result":results as! AnyObject, "error":error as! AnyObject])
            }
            else
            {
                NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveSuccess, userinfo: ["result":results as! AnyObject])
            }
        }

Is there a better way than having to go through every item in the dictionary and force downcast it to "AnyObject"?

1

1 Answers

1
votes

You never want to cast down to AnyObject. My guess here is that your fix works because you're unwrapping an optional, not because you're casting to AnyObject. I'm guessing, because I don't know what type results and error actually are, but this will likely work:

self.publicDatabase!.performQuery(query, inZoneWithID: nil) {
        results, error in
        if error != nil {
            NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveFailed, userinfo: ["result":results!, "error":error!])
        }
        else
        {
            NotificationUtility.postNotification(Notify.CloudKitVenuesRetrieveSuccess, userinfo: ["result":results!])
        }
    }