问题
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
}
}
}
}
also keep in mind that I have a property var responseitems:JSON = []
println("venue is: \(venue)")
prints a nice looking response to console, so I know that is working correctly...
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)")
}
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
What am I doing wrong here and how can I grab the name of the venue and assign my UILabel to it?
UPDATE:
I tried the following code
let labelData = self.headerInfo?["name"].error
println("labelData is: \(labelData)")
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?
回答1:
The problem is that the headerInfo
value is an error JSON
object, because you're trying to access a dictionary with an integer index.
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)
}
}
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.
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
The above is possible - this uses another auto-initialiser that initialises a valid JSON
object that represents the JSON null value.
So, how to fix it?
When you assign headerInfo
do it like this:
let headerInfo = self.responseitems["response"]["venue"]
And now in bindData
you can do:
self.venueDetailTitleLabel.text = self.headerInfo["name"].stringValue
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.
回答2:
The way to parse is the next
element: JSONValue //Something with JSONValue
if let title = element["name"]?.string {
cell.title.text = title
}
来源:https://stackoverflow.com/questions/29725241/how-can-i-use-swiftyjson-dictionaryvalue-as-a-usable-string-for-a-uilabel