solarenqu
solarenqu

Reputation: 814

swift iterate on array

i would like to ask a little help.

I have a method which call a webservice and get a json object from it.

it's look like this way:

 func wsServiceFeedTst()  {
        println("WS called...")
        println("tstFrames count: " + tstFrames.count.description)

        let json = JSON(url:"http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkkallerfeedtst")

        println(json)

        for (k, v) in json["bkkKallerFeedTst"] {

            let dateShow : NSDate? = v["feedDate"].asDate
            var finalFormatter = NSDateFormatter()
            finalFormatter.dateFormat = "yyyy.MM.dd - HH:mm"
            let finalDate = finalFormatter.stringFromDate(dateShow!)

            tstFrames.append(TimeFrame(text: v["feedText"].description, date: finalDate, image: nil, routeName: v["feedRouteShName"].description, postId: v["id"].description,routType: v["feedImgType"].description))

        }

    }

After i got the json i'm trying to iterate on it and try to add the nodes of the json to another array but when my json contains only one element it's failed because my json is not an array, this is the line where it's throws exception:

let finalDate = finalFormatter.stringFromDate(dateShow!)

it is waiting for optional, but it get nil caused by this line:

let dateShow : NSDate? = v["feedDate"].asDate

this is how my json looks like when it has only one element:

{"bkkKallerFeedTst":{"feedRouteShName":"143","id":"348","feedLat":"47.5998971180592","feedImgType":"3","feedDate":"2015-06-15T14:07:30+02:00","feedLon":"19.0457082953807","feedText":"Itthon :)”}}

And this is how it looks like when it has more then one elements (now it has two element)

{"bkkKallerFeedTst":[{"feedRouteShName":"H5","id":"349","feedLat":"47.5535475845461","feedImgType":"2","feedDate":"2015-06-15T15:27:02+02:00","feedLon":"19.0458004338391","feedText":"Hév ;)"},{"feedRouteShName":"143","id":"348","feedLat":"47.5998971180592","feedImgType":"3","feedDate":"2015-06-15T14:07:30+02:00","feedLon":"19.0457082953807","feedText":"Itthon :)"}]}

Does anybody has any ide about how to solve this?

Thank you very much!

By the answer i create this:

var bkkKallerFeedTst = json["bkkKallerFeedTst"]
var bkkKallerFeedTstArray : [[NSObject : AnyObject]]
bkkKallerFeedTstArray = []
if bkkKallerFeedTst.isDictionary {

    bkkKallerFeedTstArray.append(bkkKallerFeedTst.asDictionary!)
} else {

}

for feed in bkkKallerFeedTstArray {
    println(feed["feedRouteShName"]) //now its printing: Optional(143)

}

now it's printing this:

[feedImgType: 3, feedRouteShName: 143, feedLat: 47.5998971180592, feedText: Itthon :), feedLon: 19.0457082953807, id: 348, feedDate: 2015-06-15T14:07:30+02:00]

UPDATE: this is the solution..

 var bkkKallerFeedTst = json["bkkKallerFeedTst"]
        var bkkKallerFeedTstArray : [JSON]

        if bkkKallerFeedTst.isDictionary {
            bkkKallerFeedTstArray = [bkkKallerFeedTst] //initialize
        } else {
            bkkKallerFeedTstArray = bkkKallerFeedTst.asArray!
        }

        for bkk in bkkKallerFeedTstArray {

            let dateShow : NSDate = bkk["feedDate"].asDate!
            var finalFormatter = NSDateFormatter()
            finalFormatter.dateFormat = "yyyy.MM.dd - HH:mm"
            let finalDate = finalFormatter.stringFromDate(dateShow)

            tstFrames.append(TimeFrame(text: bkk["feedText"].description, date: finalDate, image: nil, routeName: bkk["feedRouteShName"].description, postId: bkk["id"].description,routType: bkk["feedImgType"].description))

        }

Upvotes: 0

Views: 117

Answers (1)

James Alvarez
James Alvarez

Reputation: 7219

It would probably be best to define the JSON object initially so it produces an array with a single value, rather than this way, but otherwise you could check to see if you get a valid date, and if you don't try it another way.

But looking at the API you've used, here https://github.com/dankogai/swift-json, it seems you can do checking to see whether you get a dictionary or an array. So I would cast the dictioanry value to a clearly typed variable for clarity using the '.isDictionary' method.

e.g. something akin to

var bkkKallerFeedTst = json["bkkKallerFeedTst"]
var bkkKallerFeedTstArray : [JSON]  

if bkkKallerFeedTst.isDictionary {
    bkkKallerFeedTstArray = [bkkKallerFeedTst] //initialize
} else {
    bkkKallerFeedTstArray = bkkKallerFeedTst.asArray
}

May not be this exact code - I don't have the api.

Then you can iterate first through the array (for ? in bkkKallerFeedTstArray), then inside through the dictionary contained (as you were doing before)

Basically make sure you have an array of dictionaries first, before doing the operations.

Example with your code:

func wsServiceFeedTst()  {
    println("WS called...")
    println("tstFrames count: " + tstFrames.count.description)

    let json = JSON(url:"http://79.172.249.175:7001/RestWebServiceApp/webresources/entity.bkkkallerfeedtst")

    println(json)

    var bkkKallerFeedTst = json["bkkKallerFeedTst"]
    var bkkKallerFeedTstArray : [JSON]

    if bkkKallerFeedTst.isDictionary {
        bkkKallerFeedTstArray = [bkkKallerFeedTst] //initialize
    } else {
        bkkKallerFeedTstArray = bkkKallerFeedTst.asArray
    }

    for bkk in bkkKallerFeedTstArray {

        let dateShow : NSDate = bkk["feedDate"].asDate!
        var finalFormatter = NSDateFormatter()
        finalFormatter.dateFormat = "yyyy.MM.dd - HH:mm"
        let finalDate = finalFormatter.stringFromDate(dateShow)

        tstFrames.append(TimeFrame(text: v["feedText"].description, date: finalDate, image: nil, routeName: v["feedRouteShName"].description, postId: v["id"].description,routType: v["feedImgType"].description))

    }

}

Upvotes: 1

Related Questions