1

我正在尝试使用 SwiftyJSON 访问一个简单的 JSON。作为单视图 iOS 应用程序,以下工作:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let test = [ "turn" : "5", "row" : "1", "column" : "2" ]
    let json = JSON(test)
    println(json)

    var turn: String = ""
    turn = json["turn"].stringValue
    println("swifty json .stringValue turn = \(turn)") // correctly prints "swifty json .stringValue turn = 5"

    if let turn = json["turn"].string {
        println("swifty json .string turn = \(turn)") // correctly prints "swifty json .string turn = 5"
    }

}

终端输出为:

{“列”:“2”,“转”:“5”,“行”:“1”}

swifty json .stringValue 转 = 5

swifty json .string 转 = 5

然而,当我从 iOS 游戏开始创建一个新项目时,它不再有效。

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let test = [ "turn" : "5", "row" : "1", "column" : "2" ]
    let json = JSON(test)
    println(json)

    var turn: String = ""
    turn = json["turn"].stringValue
    println("swifty json .stringValue turn = \(turn)") // prints "swifty json .stringValue turn = "

    if let turn = json["turn"].string {
        println("swifty json .string turn = \(turn)") // never gets here because turn is nil or null
    }

终端输出为:

{“列”:“2”,“转”:“5”,“行”:“1”}

swifty json .stringValue 转 =

在 iOS 游戏中使用 SwiftyJSON 时如何访问 JSON?我正在使用 Swift 1.2 和 Xcode 6.3。

4

0 回答 0