6
\$\begingroup\$

I've just started learning Swift. I started with an example of RSS feed parser. I have a wrapper class written in Objective-C which converted the XML response into NSDictonary. I'm using the NSDictionary returned from my wrapper class. Then I need to iterate through the NSDictionary and fetch all the title and display it in a tableview.

Here is the code which I've written to iterate through NSDictionary and fetch all the value for the key "title":

let tempDict:NSDictionary = responseDict["rss"] as NSDictionary
let tempDict1:NSDictionary = tempDict["channel"] as NSDictionary
let contentArray:NSArray = tempDict1["item"] as NSArray;
for (key, value : AnyObject) in enumerate(contentArray){

    let titleDict = value["title"] as NSDictionary
    let title = titleDict["text"] as NSString
    println("Title \(title)")
}

This feed that I'm trying to parse.

How can I optimise the above code? Also, can you provide some hints on getting started with Swift basics? I've already tried the docs but I feel it's a bit hard.

\$\endgroup\$
  • \$\begingroup\$ What is your familiarity level with Objective-C and the Foundation framework? Also, is enumerate() a Swift method? Or is this one you've written yourself? \$\endgroup\$ – nhgrif Jun 19 '14 at 21:18
  • \$\begingroup\$ @nhgrif: this code is written by me, i just want to know can we simply it a bit. like in Objective-C we do it like NSArray *itemsArray = [[[responseDict valueFOrKey:@"rss"] valueForKey:@"channel"] valueForKey:@"item"]; \$\endgroup\$ – skyler Jun 20 '14 at 6:33
  • \$\begingroup\$ I meant the enumerate() function specifically. Is that built in? And what is your familiarity level with ObjC and Foundation framework? \$\endgroup\$ – nhgrif Jun 20 '14 at 11:17
  • \$\begingroup\$ yes the enumerate function is built in and my familiarity level in ObjC and foundation is good. \$\endgroup\$ – skyler Jun 20 '14 at 11:36
2
\$\begingroup\$

Calling the enumerate() function here is completely unnecessary as we don't seem to care about the index of the objects. What enumerate() does is give you a tuple containing the index of the object in the array and the object at that index.

Normally, forin-ing through an enumerated array in Swift would look like this:

for (index, value : AnyObject) in enumerate(myArray) {
    println("Object at index \(index) is \(value)")
}

Here, we don't care about the index, so we don't need to waste processor time or memory on the index.

for value in contentArray {
    let titleDict = value["title"] as NSDictionary
    let title = titleDict["text"] as NSString
    println("Title \(title)")
}

But there's still room for improvement.

Given that we know that contentArray is an array of dictionary, we can do this:

for dictionary in contentArray as NSDictionary[] {
    let titleDict = value["title"]
    let title = titleDict["text"] as NSString
    println("Title \(title)")
}

But we can get better still. This is a deeply nested dictionary. NSArray has a method called objectsForKey which assumes the original array contains nothing but dictionaries and returns an array of the objects for the given key for all of these dictionaries.

It should work something like this:

let tempDict:NSDictionary = responseDict["rss"] as NSDictionary
let tempDict1:NSDictionary = tempDict["channel"] as NSDictionary
let contentArray:NSArray = tempDict1["item"] as NSArray

So far everything is the same.

Next:

let titleDictArray:NSArray = contentArray.objectsForKey("title")

Now titleDictArray is an array of dictionaries since the objects for key "title" are dictionaries, so now we can loop:

for title in titleDictArray.objectsForKey("text") as NSString {
    println("Title: \(title)")
}

Altogether, like this:

let tempDict:NSDictionary = responseDict["rss"] as NSDictionary
let tempDict1:NSDictionary = tempDict["channel"] as NSDictionary
let contentArray:NSArray = tempDict1["item"] as NSArray
let titleDictArray:NSArray = contentArray.objectsForKey("title")
for title in titleDictArray.objectsForKey("text") as NSString {
    println("Title: \(title)")
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.