繁体   English   中英

iOS 9,Swift 2.0:从JSON文件检索字典

[英]iOS 9, Swift 2.0: Retrieving a Dictionary from JSON file

我试图访问JSON文件中的字典。 这是我第一次尝试读取JSON文件。 我正在寻找一种返回getImageId结果的方法,但是我的变量imageID不会更改为self.getImageId(keyWord,jsonDictionary:jsonDictionary!)返回的值! 并保持为零。

func getEmotes(keyWord: String)-> NSNumber{
    //Returns NSURL?.  nil if the url is not valid
    let url = NSURL(string: urlString)
    let request = NSURLRequest(URL: url!)
    var imageID: NSNumber? = nil
    //Response: HHTP response, NSData the data contained in the file, NSError no internet, etc.
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
        if error == nil{
            //To get URL response.
            let httpResponse = response as! NSHTTPURLResponse

            if httpResponse.statusCode == 200 {
                var jsonDictionary: NSDictionary? = nil

                do {
                    jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
                }catch{ }

                if jsonDictionary != nil{
                    //The retrieval Dictionary inside Json was sucesful.
                    //NEED: Return the value from this:
                    imageID = self.getImageId(keyWord, jsonDictionary: jsonDictionary!)!
                }
            }else{
                print(httpResponse.statusCode)
            }
        }

    }
    print(imageID)
    return imageID ?? 0
}

非常感谢。

您可以使用return块。 我没有在返回块中添加任何错误处理,您需要添加它。

getEmotes("keyword") { (imageID) in
    print(imageID)
}

func getEmotes(keyWord: String, returnBlock: (imageID: NSNumber?) -> Void)-> Void{
//Returns NSURL?.  nil if the url is not valid
let url = NSURL(string: urlString)
let request = NSURLRequest(URL: url!)
var imageID: NSNumber? = nil
//Response: HHTP response, NSData the data contained in the file, NSError no internet, etc.
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error: NSError?) in
    if error == nil{
        //To get URL response.
        let httpResponse = response as! NSHTTPURLResponse

        if httpResponse.statusCode == 200 {
            var jsonDictionary: NSDictionary? = nil

            do {
                jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
            }catch{ }

            if jsonDictionary != nil{
                //The retrieval Dictionary inside Json was sucesful.
                //NEED: Return the value from this:
                imageID = self.getImageId(keyWord, jsonDictionary: jsonDictionary!)!
                returnBlock(imageID: imageID)
            }
        }else{
            print(httpResponse.statusCode)
        }
    }

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM