作者:春阳 | 来源:互联网 | 2023-10-11 11:51
FrommyswiftappImfetchingdatafromawebservice.Datacomesasanarray:从我的快速应用程序,我正在从Web服务获取
From my swift
app I'm fetching data from a webservice. Data comes as an array:
从我的快速应用程序,我正在从Web服务获取数据。数据以数组形式出现:
{"hashtags": ["first", "second"]}
I want to put every hashtag in a separate row of my UITableView
. I have the logic to do that, but first - I'm getting werid error while parsing data.
我想把每个hashtag放在我的UITableView的一个单独的行中。我有逻辑做到这一点,但首先 - 我在解析数据时遇到了错误的错误。
I wrote a custom function:
我写了一个自定义函数:
class SingleHashtag: NSObject {
var hashtagText: [String]
init(hashtagText: [String]) {
self.hashtagText = hashtagText
super.init()
}
class func fromJSON(json: JSON) -> SingleHashtag? {
let hashtagText:[String] = json["hashtags"].arrayValue.map { $0.stringValue}
return SingleHashtag(hashtagText: hashtagText)
}
}
and then in my main class I have:
然后在我的主要课程中我有:
Alamofire.request(.GET, "\(serverURL)/hashtags"/*, headers: headers*/)
.validate()
.responseJSON { response in
print(response.description)
switch response.result {
case .Success:
dispatch_async(dispatch_get_main_queue(),{
self.items.removeAllObjects()
if let jsOnData= response.result.value as? [[String: AnyObject]] {
for hashtag in jsonData {
if let userHashtags = SingleHashtag.fromJSON(JSON(hashtag)){
for hash in userHashtags {
self.items.addObject(hash)
self.hashtagTable.reloadData()
}
}
}
}
self.hashtagTable.reloadData()
})
case .Failure(let error):
print(error)
}
}
but this line:
但这一行:
for hash in userHashtags {
throws an error during compilation:
编译时抛出错误:
type SingleHashtag does not conform to protocol 'SequenceType'
I tried adding as AnyObject
but that didn't help. Can you tell me what might be wrong here?
我尝试添加为AnyObject,但这没有帮助。你能告诉我这里可能有什么问题吗?
1 个解决方案