Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Photo Bombers in Swift

After rewriting Crystal Ball and Blog Reader apps into Swift, I decided to take on a bit more complicated project, one that would require a bridging header. I also liked Photo Bombers because it's completely written in code.

So, here's my messy rewrite. If you run it, don't forget to add the Instagram API client ID into the App Delegate.

I hope this example helps, but keep in mind that with some more Swift experience, this code would probably look a lot nicer.

5 Answers

Thank you. This is great.

I'm working on a project of my own. It runs, the crashes immediately. I know it has something to do with the way a create my object with my custom init.

here is my class that stores the location data:

class TNVenueLocationDetails: NSObject {
    var name: String = "default"
    var identifier: String = ""

    var address: String = ""
    var cc: String = ""
    var city: String = ""
    var country: String = ""
    var crossStreet: String = ""
    var distance: String = ""
    var lat: Double
    var lng: Double
    var postalCode: String = ""
    var state: String = ""

    init(name: String, location: Dictionary<String, AnyObject>, identifier: String) {
        self.name = name
        self.address = location["address"]? as String
        self.cc = location["cc"]? as String
        self.city = location["location"]? as String
        self.country = location["country"]? as String
        self.crossStreet = location["crossStreet"]? as String
        self.distance = location["distance"]? as String
        self.lat = location["lat"]? as Double
        self.lng = location["lng"]? as Double
        self.postalCode = location["postalCode"]? as String
        self.state = location["state"]? as String
        self.identifier = identifier

        super.init()
    }
}

If I don't have '?' in all the lines it gives me error.

And (sorry for so much code) here is the class with the function that actually makes the request:

func performVenueLocationRequest(identifier: String, query: String, completionClosure: (venues: TNVenueLocationDetails[], error: NSError?) -> ()) {
        var error: NSError?
        var response: NSURLResponse?

        let buildRequest: NSURLRequest = buildRequestForVenueLocation(identifier, query: query)
        var data: NSData? = NSURLConnection.sendSynchronousRequest(buildRequest, returningResponse: &response, error: &error)

        if let realData = data {
            if let errorNotEmpty = error? {
                println(errorNotEmpty.localizedDescription)
            } else {
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
                    var error: NSError?
                    let responseDictionary: Dictionary<String, AnyObject> = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as Dictionary
                    let venuesArray: Array<AnyObject> = (responseDictionary as AnyObject).valueForKeyPath("response.venues") as Array<AnyObject>
                    var finalizedVenueArray = TNVenueLocationDetails[]()
                    for data: AnyObject in venuesArray {
                        var venueObject = TNVenueLocationDetails(name: data["name"] as String, location: data["location"] as Dictionary<String, AnyObject>, identifier: data["identifier"] as String)
                        finalizedVenueArray.append(venueObject)
                        println(venueObject)
                    }
                    completionClosure(venues: finalizedVenueArray, error: error);
                });
            }
        } else {
            println("Bad data/nil was returned")
        }
    }

If I just print out the 'venuesArray' it's gives me all the data just fine,If you'd have a quick look I'd really appreciate it :)

I'm still not fully comfortable with optionals and unwrapping, but I can give it a go. Could you post an example venuesArray? I'd like to see its structure. Is the structure of the dictionary objects inside the array always the same?

If you'd like I can email the project to you. I don't want to post it for everyone since I'm using API keys from foursquare.

Sure. That would probably be for the best. You can find my e-mail on my GitHub profile and I'll try to reply as soon as possible.

done :)

Thank you SO MUCH, I was about to go crazy until I saw how you had "#import <Foundation.Foundation.h> in your bridging header