如何从单个 json 值中获取字符串并将其附加到 swift 中的标签(使用 alamofire)
how can I fetch a string from a single json value and append it to my label in swift (using alamofire)
我有一个 json 结构:
{"numberOfPeople":120}
我想在 swift 中创建一个包含以下字符串的标签:
We had seen there more than <<numberOfPeople fetched from json>> adults
这是我目前所做的:
@IBOutlet weak var numberOfHelpersLabel: UILabel!
var text = "We had seen there more than "
var text2 = " adults"
Alamofire.request(.GET, "https://webservice.com/getnumber")
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let numberOfPeople = json["numberOfPeople"].description
self.numberOfHelpersLabel.text = self.text+numberOfPeople + self.text2
}
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
但它不起作用,错误说:
你能帮帮我吗?
你要做的是所谓的字符串插值。在字符串中,您可以将表达式放在转义括号 \()
中。像这样:
self.numberOfHelpersLabel.text = "We had seen there more than \(numberOfPeople) adults"
(第一个错误看起来很奇怪,我看不到该行中的可选链接,我建议您清理并重建。)
由于 JSON
对象中的值是 optionals 你需要打开它们(因此推荐使用 String?
的错误)。
另一个错误是 JSON 访问 属性,numberOfPeople
可能是 任何东西,编译器不知道如何将字符串连接在一起,因此您需要添加一个提示,表明它将是一个字符串。
这两个问题可以通过几种方式一起解决...一个简单的例子:
if let value = response.result.value {
let json = JSON(value)
if json["numberOfPeople"] != nil {
let numberOfPeople = json["numberOfPeople"] as? String
// your original string concatenation would then work
//self.numberOfHelpersLabel.text = self.text + numberOfPeople + self.text2
// or a bit cleaner using interpolation:
self.numberOfHelpersLabel.text = "We had seen there more than \(numberOfPeople) adults"
}
} else {
self.numberOfHelpersLabel.text = "Wow no idea how many people!"
}
我有一个 json 结构:
{"numberOfPeople":120}
我想在 swift 中创建一个包含以下字符串的标签:
We had seen there more than <<numberOfPeople fetched from json>> adults
这是我目前所做的:
@IBOutlet weak var numberOfHelpersLabel: UILabel!
var text = "We had seen there more than "
var text2 = " adults"
Alamofire.request(.GET, "https://webservice.com/getnumber")
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
let numberOfPeople = json["numberOfPeople"].description
self.numberOfHelpersLabel.text = self.text+numberOfPeople + self.text2
}
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
但它不起作用,错误说:
你能帮帮我吗?
你要做的是所谓的字符串插值。在字符串中,您可以将表达式放在转义括号 \()
中。像这样:
self.numberOfHelpersLabel.text = "We had seen there more than \(numberOfPeople) adults"
(第一个错误看起来很奇怪,我看不到该行中的可选链接,我建议您清理并重建。)
由于 JSON
对象中的值是 optionals 你需要打开它们(因此推荐使用 String?
的错误)。
另一个错误是 JSON 访问 属性,numberOfPeople
可能是 任何东西,编译器不知道如何将字符串连接在一起,因此您需要添加一个提示,表明它将是一个字符串。
这两个问题可以通过几种方式一起解决...一个简单的例子:
if let value = response.result.value {
let json = JSON(value)
if json["numberOfPeople"] != nil {
let numberOfPeople = json["numberOfPeople"] as? String
// your original string concatenation would then work
//self.numberOfHelpersLabel.text = self.text + numberOfPeople + self.text2
// or a bit cleaner using interpolation:
self.numberOfHelpersLabel.text = "We had seen there more than \(numberOfPeople) adults"
}
} else {
self.numberOfHelpersLabel.text = "Wow no idea how many people!"
}