swift代码错误,不知道写什么回来?

时间:2016-03-04 10:27:29

标签: json swift swift2 nsurl

我无法找到要返回的内容,尝试获取API但是NSURL链接的输出中没有信息产生,事件是一个硬编码的随机数组。谢谢

/private/var/tmp

1 个答案:

答案 0 :(得分:0)

这只是一个返回结果的示例。我建议使用二维数组。我希望我能得到你的帮助。

func parse (jsonResult: AnyObject?) ->(Dictionary<String,NSNumber>){

    var myDictionary = [String: NSNumber]()

    if let dictionary = jsonResult as? NSDictionary {

        if let event = dictionary["EVENT_ID"] as? NSDictionary {
            print("Event is \(event)")
            if let eventName = event["EVENT_NAME"] as? NSNumber {
                print ("Event name is \(eventName)")
                myDictionary["key1"] = eventName
            }
        }

        if let channel = dictionary["CATEGORY_NAME"] as? [String: AnyObject] {
            print("Channel is \(channel)")
            if let eventName = channel["EVENT_NAME"] as? NSNumber {
                print ("Event name is \(eventName)")
                myDictionary["key2"] = eventName
            }
        }

    } else {

        if let array = jsonResult as? NSArray {
            for element in array {
                print(element)
                //suppose an array are numbers
                myDictionary[String(element)] = element as? NSNumber
            }
        }

        if let array = jsonResult as? [AnyObject] {
            for element in array {
                print(element)
                //suppose an array are numbers
                myDictionary[String(element)] = element as? NSNumber
            }
        }
    }

    return myDictionary
}