Categories
Uncategorized

Updating RayWenderlich.com’s “AFNetworking Crash Course” for iOS 6

weather app

Whether you’re new to iOS programming or a long-timer, RayWenderlich.com is a valuable resource for the iOS developer. They regularly publish tutorials, tips, tricks and other goodies that you’d be crazy to do without if you’re serious about writing apps for iDevices. In addition to articles on the site, they go deeper with their books, which are excellent.

RayWenderlich.com recently published an article titled AFNetworking Crash Course, which covers how to write networking apps using AFNetworking, a library created by the folks at Gowalla that simplifies iOS network programming. In this tutorial, you build a weather app that uses AFNetworking to get its data from the World Weather Online service. Check it out; AFNetworking’s useful, and the tutorial’s pretty nice.

In order to reach the widest possible audience, the tutorial was written for iOS 5 and earlier versions of Xcode. If you’re developing with the current version of Xcode and for iOS 6 (which accounted for 83% of all iOS traffic in North America in February), you might want to make a few changes to the code in the tutorial. I’ve listed the changes below:

Use Modern Array Notation

Here’s the old way to get at the element of an array whose index is theIndex in Objective-C:

element = [theArray objectAtIndex:theIndex];

It’s a little clunky, and as I wrote in an earlier article, Objective-C’s New NSNumber, NSArray and NSDictionary Syntaxes Mean Less “Yak Shaving” for iOS and OS X Developers, there’s a much nicer way to do it:

element = theArray[theIndex];

In AFNetworking Crash Course, where you see code like this:

daysWeather = [upcomingWeather objectAtIndex:indexPath.row];

path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"WeatherHTTPClientImages/"];

change it to this:

daysWeather = upcomingWeather[indexPath.row];

path = [paths[0] stringByAppendingPathComponent:@"WeatherHTTPClientImages/"];

Use Modern Dictionary Notation

Here’s the old way to get at the item in a dictionary whose key is theKey:

item = [theDictionary objectForKey:theKey];

Again: it’s clunky. Also again, in my earlier article, I showed the modern way to access dictionary items:

item = theDictionary[theKey];

Setting items for a dictionary used to be like this:

[theDictionary setObject:theObject forKey:theKey];

Now, it’s like this:

theDictionary[theKey] = theObject;

So, in the places where you see code like:

*array = [self.xmlWeather objectForKey:@"weather";];

change it to:

*array = xmlWeather[@"weather"];

…and where you see code like:

[self.xmlWeather setObject:array forKey:@"weather"];

change it to:

self.xmlWeather[@"weather"] = array;

Update the Deprecated Location Manager Delegate Method

If you use the code as-is with iOS 6, you’ll get an error message that looks like this:

Deprecated in iOS 6.0
locationManager:didUpdateToLocation:fromLocation:
Tells the delegate that a new location value is available. (Deprecated in iOS 6.0. Use locationManager:didUpdateLocations: instead.)

Instead of using the deprecated locationManager:didUpdateToLocation:fromLocation: method, use the current locationManager:didUpdateLocations: method instead:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    // locations is an array of locations where the device has recently been
    // starting with the least recent and ending with the most recent.
    CLLocation *lastLocation = (CLLocation *)[locations lastObject];

    // Fetch the weather only if the last location is less than 5 minutes old
    if ([lastLocation.timestamp timeIntervalSinceNow] < 300) {
        [self.locationManager stopUpdatingLocation];
        WeatherHTTPClient *client = [WeatherHTTPClient sharedWeatherHTTPClient];
        client.delegate = self;
        [client updateWeatherAtLocation:lastLocation forNumberOfDays:5];
    }
}
Categories
Uncategorized

How and Where is _why?

why_missing_milk_carton Photo courtesy of "ejc".

A Little Bit About _why

Cartoon foxes from "Why's (Poignant) Guide to Ruby" screaming "Chunky Bacon!"If you were to walk up to someone and utter the seemingly meaningless phrase “Chunky bacon!” and get a smile rather than a look a bewilderment, you could probably mark that person down as a Ruby programmer. That strange two-word combination is seared in the minds of those who have read what is probably the most whimsical programming language book in existence, Why’s (Poignant) Guide to Ruby. The book is one of the strange and beautiful Ruby-related works created by the enigmatic programmer, musician, artist, comic illustrator and wag known only as why the lucky stiff, or _why for short.

whys_poignant_guide

_why’s contributions to the world of Ruby programming are many. In addition to the (Poignant) Guide, some of his goodies that I’ve made use of are:

  • Camping, an incredibly tiny Ruby web application framework
  • Hackety Hack, a “coder’s starter kit” for Ruby, meant to bring back the spirit of experimentation of those days when the BASIC programming language was built into every home computer
  • Hpricot, a parser that’s great at scraping HTML and even parsing XML
  • Redcloth, a library that implements the Textile markup language
  • Shoes, a desktop UI toolkit
  • Syck, a YAML library

_why made it a point to reveal as little about himself as possible, and most of us were happy to indulge him. Most people were happy to simply know and address him as “why”, and in the community, it was a point of etiquette to not try and dig too deeply.

_why Vanishes from the Net

Yesterday, _why’s presence vanished from the web. The places online where you could find him have been taken down. These included:

John Resig has written a lovely “eulogy” for _why, and while I think it’s premature to say that he’s gone forever, it’s still nice to see a nice tribute to him. My favourite part of the eulogy is where John likens _why’s works to a sand mandala:

Sand mandalas are incredibly intricate works of art that take many people many days to construct. They’re very expressive, but fragile, works of art.

After a mandala has been constructed – and displayed – it is ceremoniously deconstructed – which is meant "to symbolize the Buddhist doctrinal belief in the transitory nature of material life."

_why’s entire online presence and code was presented in the sand mandala that was ‘_why’. The person behind ‘_why’ simply decided to move on and close that portion of his life.

I hope that _why’s disappearance is a brief hiatus. The Ruby world – hey, the programming world, the art world, the music world too – just isn’t the same without him.

Finding _why’s Stuff

There’s only one problem with _why’s deletion of his online presence: a number of people have come to depend on his works, particularly his code. The (Poignant) Guide is downloadable from Scribd, and I figure that if it hasn’t happened already, someone will start a Github repository of his code. There’s also Facebook group called Missing why the lucky stiff — let’s hope it doesn’t get all maudlin and support-group-y.

Last but not least, there’s programmer Leah Culver, who commissioned a tattoo from _why:

leah-culver

leah-culver-tattoo

In Closing

I’ll finish with my favourite tweet from _why, which I blogged about a year ago:

when you don’t create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create.