ios - 无法显示部分 JSON 数据

标签 ios json swift

我一直在尝试将某些 JSON 数据显示到 Storyboard 中,但由于某种原因一直无法实现。它的工作部分是 var 名称,它是一个字符串,它不需要转换,所以它可以工作。我遇到问题的部分是尝试将两个 Int64 转换为字符串,但我将它们列为 AnyObjects。这真的让我感到困惑,但这是代码中的问题:

程序运行良好,但不显示 profileIconId 和 summonerLevel 的任何信息。

import UIKit

class ViewController: UIViewController, NSURLConnectionDelegate {

lazy var data = NSMutableData()

@IBOutlet weak var searchField: UITextField!

@IBOutlet weak var name: UILabel!

@IBOutlet weak var summonerLevel: UILabel!

@IBOutlet weak var profileIconId: UILabel!

@IBAction func enterButton(sender: AnyObject) {

    startConnection()

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func startConnection(){
    let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/soon2challenger?api_key=(removed my private api key for obvious reasons)"
    var url: NSURL = NSURL(string: urlPath)!
    var request: NSURLRequest = NSURLRequest(URL: url)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
    connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
    self.data.appendData(data)
}

func buttonAction(sender: UIButton!){
    startConnection()
}


func connectionDidFinishLoading(connection: NSURLConnection!) {
    var err: NSError
    // throwing an error on the line below (can't figure out where the error message is)
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

    let include: AnyObject = jsonResult.objectForKey(searchField.text)!

    var name1: AnyObject = include.objectForKey("name")!
    var summLevel: AnyObject = include.objectForKey("summonerLevel")!
    var profIconId: AnyObject = include.objectForKey("profileIconId")!

    name.text = name1 as? String
    profileIconId.text = profIconId as? String
    summonerLevel.text = summLevel as? String

    println(name1)
    println(summLevel)
    println(profIconId)

  }
}

处理和显示所有内容的代码在代码最底部的 connectionDidFinishLoading 函数中。

最佳答案

这是对您的 connectionDidFinishLoading(_:) 方法的重构,它使用可选绑定(bind)正确解包值。

您也可以考虑使用 NSNumberFormatter 而不是 "\()"

func connectionDidFinishLoading(connection: NSURLConnection) {

    var err: NSError?

    if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary,
        let include = jsonResult.objectForKey(searchField.text) as? NSDictionary {

        if let name1 = include[ "name" ] as? String {
            name.text = name1
            println(name1)
        }

        if let summLevel = include[ "summonerLevel" ] as? NSNumber {
            summonerLevel.text = "\(summLevel.integerValue)"
            println(summLevel)

        }
        if let profIconId = include[ "profileIconId" ] as? NSNumber {
            profileIconId.text = "\(profIconId.integerValue)"
            println(profIconId)
        }
    }
}

关于ios - 无法显示部分 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31151042/

相关文章:

构建时的 iOS App 模块化

ios - 如何检测用户是否在免费试用期间取消了自动续订订阅?

ios - CoreData 返回空数据

ios - 如何对 UIViewController 进行单元测试 - TDD/BDD

json - 在 JSON 响应中省略结构字段

ios - 在 SwiftUI NavigationView 中呈现 UINavigationBar 的旧小标题

java - JSON : Unrecognized field "value" (<objectClass>), 未标记为可忽略

java - 使用 HttpURLConnection 读取 REST 请求输出

使用动态协议(protocol)类型的 Swift 动态类型初始化

ios - 将数据从 UITableViewCell cocoa touch 类传输到 UIViewController 类