如何使用swiftyJSON dictionaryValue作为UILabel的可用字符串? [英] How can I use swiftyJSON dictionaryValue as a usable string for a UILabel?
问题描述
我在UITableViewController中有一个 makeRequest()
方法,代码如下:
I have a makeRequest()
method inside a UITableViewController with the following code:
func makeRequest() {
Alamofire.request(.GET, self.foursquareEndpointURL, parameters: [
//"VENUE_ID" : self.foursquareVenueID,
"client_id" : self.foursquareClientID,
"client_secret" : self.foursquareClientSecret,
"v" : "20140806"
])
.responseJSON(options: nil) { (_, _, data, error) -> Void in
if error != nil {
println(error?.localizedDescription)
} else if let data: AnyObject = data {
let jObj = JSON(data)
if let venue = jObj["response"]["venue"].dictionaryValue as [String: JSON]? {
self.responseitems = jObj
println("venue is: \(venue)")
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData() // Update UI
}
}
}
}
还请记住,我有一个属性 var responseitems:JSON = []
also keep in mind that I have a property var responseitems:JSON = []
println( venue is:\(venue))
打印出对控制台的漂亮响应,所以我知道它可以正常工作...
println("venue is: \(venue)")
prints a nice looking response to console, so I know that is working correctly...
我还有一个带有 bindData()
方法的自定义UITableViewCell类,其代码如下:
I also have a custom UITableViewCell class with a bindData()
method with the following code:
func bindData() {
println("VenueDetailHeaderCell data did set")
self.venueDetailTitleLabel.text = self.headerInfo?["name"].stringValue
let labelData = self.headerInfo?["name"].stringValue
println("labelData is: \(labelData)")
}
如您所见,我试图将UILabel的文本设置为JSON响应中的[ name]。stringValue。但是,当我 println( labelData is:\(labelData))
时,我得到的labelData的控制台输出是:Optional(),显然是空的。
As you can see, I am attempting to set a UILabel's text to the ["name"].stringValue in the JSON response. However, when I println("labelData is: \(labelData)")
I get console output of labelData is: Optional("") which is obviously empty.
以下是我要抓取的屏幕截图
Here's a screenshot of what I'm trying to grab
我在这里做错了什么,我该如何获取场地名称并为其分配UILabel?
What am I doing wrong here and how can I grab the name of the venue and assign my UILabel to it?
更新:
我尝试了以下代码
let labelData = self.headerInfo?["name"].error
println("labelData is: \(labelData)")
并获得以下控制台输出: Error Domain = SwiftyJSONErrorDomain Code = 901 Array [0]失败,它不是数组 UserInfo = 0x7fd6d9f7dc10 {NSLocalizedDescription = Array [0]失败,它不是数组}如果这对任何人都有用。我真的很困惑……有什么想法吗?
And get a console output of: "Error Domain=SwiftyJSONErrorDomain Code=901 "Array[0] failure, It is not an array" UserInfo=0x7fd6d9f7dc10 {NSLocalizedDescription=Array[0] failure, It is not an array}" If that is of use to anyone. I am really confused here... Any ideas?
推荐答案
问题是 headerInfo
值是错误的 JSON
对象,因为您正尝试访问具有整数索引的字典。
The problem is that the headerInfo
value is an error JSON
object, because you're trying to access a dictionary with an integer index.
请注意, var responseitems:JSON = []
不会创建数组对象。 SwiftyJSON具有自动分配构造函数(我是swift的新手,所以不确定什么是正确的swift术语)...请参阅SwiftyJSON.swift源代码中的此初始化程序:
Note that var responseitems:JSON = []
does not create an array object. SwiftyJSON has auto-assignment-constructors (I'm new to swift, so not sure what the correct swift terminology is)... see this initialiser in the SwiftyJSON.swift source code:
extension JSON: ArrayLiteralConvertible {
public init(arrayLiteral elements: AnyObject...) {
self.init(elements)
}
}
这意味着当您执行 var responseitems时: JSON = []
您不是在创建数组,而是在创建 JSON
对象,该对象是使用上述<$ c的空数组构造的$ c> init 方法。然后,当您执行 self.responseitems = jObj
时,您会将 responseitems
变量重新分配给 JSON
对象,其中包含字典。因此 self.responseitems [0]
无效。
What this means is that when you do var responseitems:JSON = []
you are not creating an array, you are creating a JSON
object that is constructed with an empty array using the above init
method. Then when you do self.responseitems = jObj
you are re-assigning that responseitems
variable to a JSON
object with a dictionary in it. Therefore self.responseitems[0]
is invalid.
还请注意,使用SwiftyJSON时,没有这样的事情可选的 JSON
对象。我在您的评论中注意到您说您执行 var headerInfo:JSON吗? ...
-无法使用可选的 JSON
。
Also note that with SwiftyJSON, there is no such thing as an optional JSON
object. I notice in your comment you say that you do var headerInfo:JSON? ...
- it's not possible to have an optional JSON
.
var headerInfo: JSON = nil
以上是可能的-这使用了另一个自动初始化程序,初始化一个有效的 JSON
对象,该对象表示JSON空值。
The above is possible - this uses another auto-initialiser that initialises a valid JSON
object that represents the JSON null value.
因此,如何修复它?
当您分配 headerInfo
时,请执行以下操作:
When you assign headerInfo
do it like this:
let headerInfo = self.responseitems["response"]["venue"]
现在在 bindData
中,您可以执行以下操作:
And now in bindData
you can do:
self.venueDetailTitleLabel.text = self.headerInfo["name"].stringValue
上面的方法假设使用Swift 1.2和SwiftyJSON> = 2.2-同样,在您理解了上面的内容并解决了问题之后,您可能希望对代码进行一些重构以反映对数据模型的正确理解。
Note that all of the above assumes Swift 1.2 and SwiftyJSON >= 2.2 - also after you've understood the above and corrected the issue, you will probably want to refactor the code a bit to reflect the corrected understanding of the data-model.
这篇关于如何使用swiftyJSON dictionaryValue作为UILabel的可用字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!