swift迭代数组

时间:2015-06-15 16:10:11

标签: json swift for-loop

我想问一点帮助。

我有一个调用webservice并从中获取json对象的方法。

它看起来像这样:

 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))

        }

    }

我得到json之后我试图迭代它并尝试将json的节点添加到另一个数组但是当我的json只包含一个元素时它失败了,因为我的json不是数组,这就是行它抛出异常的地方:

let finalDate = finalFormatter.stringFromDate(dateShow!)

它正在等待可选,但是由此行导致nil:

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

这是我的json在只有一个元素时的样子:

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

这就是当它有多个元素(现在它有两个元素)时的样子

{"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 :)"}]}

有没有人有任何关于如何解决这个问题的想法?

非常感谢!

通过答案我创建了这个:

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)

}

现在正在打印这个:

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

更新:这是解决方案..

 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))

        }

1 个答案:

答案 0 :(得分:1)

最初可能最好定义JSON对象,因此它会生成一个具有单个值的数组,而不是这种方式,但是否则您可以检查是否获得了有效日期,如果不尝试另一种方式。

但是看看你使用的API,这里https://github.com/dankogai/swift-json,你似乎可以检查你是否得到字典或数组。因此,为了清楚起见,我会使用'.isDictionary'方法将dictioanry值转换为明确类型的变量。

e.g。类似于

的东西
var bkkKallerFeedTst = json["bkkKallerFeedTst"]
var bkkKallerFeedTstArray : [JSON]  

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

可能不是这个确切的代码 - 我没有api。

然后你可以先遍历数组(对于?在bkkKallerFeedTstArray中),然后通过包含的字典进行迭代(就像你之前做的那样)

在执行操作之前,首先要确保首先拥有一组字典。

代码示例:

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))

    }

}