简体   繁体   English

类型不符合协议序列类型 - Swift

[英]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.

相关问题 类型不符合协议Swift - Type does not conform to protocol Swift Swift - 类型“*”不符合协议“*” - Swift - Type '*' does not conform to protocol '*' 类型“ customDataObject”不符合协议“序列” - Type 'customDataObject' does not conform to protocol 'Sequence' 类型“NSPersistentStore”在swift中不符合协议“BooleanType” - Type 'NSPersistentStore' does not conform to protocol 'BooleanType' in swift Swift:类型'ViewController'不符合协议'UIPageViewControllerDataSource' - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' Swift 2.0类型'()'不符合协议 - Swift 2.0 Type '()' does not conform to protocol Swift:`类型'[String]'不符合协议'StringLiteralConvertible' - Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'` 类型“myViewController”不符合Swift中的协议UIPIckerDataSource - Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift Swift-类型'CircularTransition'不符合协议'UIViewControllerAnimatedTransitioning' - Swift - Type 'CircularTransition' does not conform to protocol 'UIViewControllerAnimatedTransitioning' Swift - MultipeerConnectivity类型不符合协议 - Swift - MultipeerConnectivity Type does not conform to protocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM