[英]type does not conform to protocol Sequence Type - Swift
From my swift
app I'm fetching data from a webservice. 从我的
swift
应用程序中,我正在从Web服务中获取数据。 Data comes as an array: 数据以数组形式出现:
{"hashtags": ["first", "second"]}
I want to put every hashtag in a separate row of my UITableView
. 我想将每个hashtag放在我的
UITableView
一个单独的行中。 I have the logic to do that, but first - I'm getting werid error while parsing data. 我有逻辑做到这一点,但首先 - 我在解析数据时遇到了错误的错误。
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. 我尝试添加
as AnyObject
但没有帮助。 Can you tell me what might be wrong here? 你能告诉我这里可能有什么问题吗?
Based on our conversation in the comments it looks like there are lots of things at play here. 根据我们在评论中的对话,看起来这里有很多东西在玩。
Defining items
as an objective-c object like NSMutableArray
is fighting against Swift and robbing it of its typing strength. 将
items
定义为像NSMutableArray
这样的Objective-c对象,正在与Swift作斗争并抢夺它的打字强度。 If items
is only ever a list of hashtag strings, then it should be typed as such. 如果
items
只是一个hashtag字符串列表,那么它应该这样输入。 Try changing your items declaration to this: 尝试将您的商品声明更改为:
var items = [String]()
Based on what you've shared, it also doesn't look like a separate class for SingleHashtag
is necessary. 根据您共享的内容,它看起来也不像
SingleHashtag
的单独类。 If it only has one String
variable, it would be simpler to just pass the strings into items directly. 如果它只有一个
String
变量,那么将字符串直接传递给项目会更简单。 An example of that is here: 这方面的一个例子是:
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.removeAll()
//cast the jsonData appropriately, then grab the hashtags
if let jsonData = response.result.value as? [String: [String]],
let hashtags = jsonData["hashtags"] {
//hashtags is now type [String], so you can loop
//through without the error and add the strings to 'items'
for hashtagText in hashtags {
self.items.append(hashtagText)
self.hashtagTable.reloadData
}
}
})
case .Failure(let error):
print(error)
}
}
Note: this is written in Swift 3, so there will be some syntax differences if you are using an older version of Swift. 注意:这是用Swift 3编写的,因此如果您使用的是旧版本的Swift,会有一些语法差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.