Skip to content
Kevin Leong edited this page Aug 24, 2016 · 5 revisions

Swift Questions

What does !! do?

Alamofire.request(.GET, PhotosViewController.PHOTOS_URL)
    .responseJSON { response in
        var slugs = [String]()

        if let json = response.result.value {
            if let posts = json["response"]!!["posts"] as? [[String: AnyObject]] {
                for post in posts {
                    if let slug = post["slug"] as? String {
                        slugs.append(slug)
                    }
                }
            }
        }
        print(slugs)
}

What is as [[String: AnyObject]] for?

This is an array of dictionaries.

Each dictionary contains String keys that map to AnyObject values.

What does as? and as! do?

Forced Downcast

as! is a forced downcast, used when the downcast is expected to succeed.

This will downcast and force unwrap.

If the downcast returns a nil value a runtime error will occur.

Conditional Downcast

as? is a conditional downcast, which allows subsequent checks to see if a downcast was successful.

Clone this wiki locally