简体   繁体   English

For-in 循环需要“JSON?” 符合“顺序”; 你的意思是解开可选的吗?

[英]For-in loop requires 'JSON?' to conform to 'Sequence'; did you mean to unwrap optional?

I want to append items to an array using a loop in swift.我想使用 swift 中的循环将 append 项添加到数组中。

My code looks like the following and I am seeing this error:我的代码如下所示,我看到了这个错误:

For-in loop requires 'JSON?' For-in 循环需要“JSON?” to conform to 'Sequence';符合“顺序”; did you mean to unwrap optional?你的意思是解开可选的吗?

In the code below I want to add each email to an array defined in the class:在下面的代码中,我想将每个 email 添加到 class 中定义的数组中:

func loadData() {
    Alamofire.request(URL, method: .get)
        .responseSwiftyJSON { dataResponse in
            let response = dataResponse.value

            for item in response { // For-in loop requires 'JSON?' to conform to 'Sequence'; did you mean to unwrap optional?
               print(item)

               // ideally I want to push the email here
               // something like emails.append(item.email)
            }
            
            if let email = response?[0]["email"].string{
                print(email) // This shows correct email
            }
        }
}

Can anyone advise what the solution is here?谁能建议这里的解决方案是什么?

The error here is that dataResponse.value is JSON so in order to use the value property you have to cast it.这里的错误是dataResponse.value是 JSON 所以为了使用value属性,你必须转换它。

So your code should look like that:所以你的代码应该是这样的:

func loadData() {
    Alamofire.request(URL, method: .get)
        .responseSwiftyJSON { dataResponse in
            guard let response = dataResponse.value as? [String: Any] else {
                print("error in casting")
                return
            }

            for item in response { // For-in loop requires 'JSON?' to conform to 'Sequence'; did you mean to unwrap optional?
               print(item)

               // ideally I want to push the email here
               // something like emails.append(item.email)
            }
            
            if let email = response?[0]["email"].string{
                print(email) // This shows correct email
            }
        }
}

I cast as dictionary because JSON responses most of the times are dictionaries.我将其转换为字典,因为 JSON 响应大多数时候都是字典。 I also suggest you to use Swift Codables in order to map your json responses.我还建议您使用 Swift Codables 来 map 您的 json 响应。 reference here: https://www.hackingwithswift.com/articles/119/codable-cheat-sheet参考这里: https://www.hackingwithswift.com/articles/119/codable-cheat-sheet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 For-in 循环需要“[UserVehicles]?” 符合“顺序”; 你的意思是解开可选的? 迅速 - For-in loop requires '[UserVehicles]?' to conform to 'Sequence'; did you mean to unwrap optional? Swift “for-in 循环需要 '[String]?' 符合“顺序”; 你的意思是解开可选的包装吗?” 但我不认为我在使用选项 - “For-in loop requires '[String]?' to conform to 'Sequence'; did you mean to unwrap optional?” but I don't think I am using optionals 可选类型“浮点型”的值 不展开 你是说用'!' 要么 '?'? - Value of optional type 'Float?' not unwrapped; did you mean to use '!' or '?'? 未包装的可选类型“ AnyObject?”的值不是您要使用的! 要么? - Value of optional Type “AnyObject?” not unwrapped did you mean to use ! or? 可选类型CGFloat的值? 没有解包,您是说要使用! 要么? - Value of optional type CGFloat? not unwrapped, did you mean to use ! or? 在Swift中,如果您解开“ Implicitly Unwrapped Optional”,这是双重解开吗? - In Swift if you unwrap a “Implicitly Unwrapped Optional” is this a double unwrap? Swift可选值在while循环条件下解包 - Swift optional value Unwrap in while loop condition FBSDKGraphRequest:结果-可选类型“ Any?”的值 不展开 你是说用'!' 要么 '?'? - FBSDKGraphRequest : result - Value of optional type 'Any?' not unwrapped; did you mean to use '!' or '?'? Swift问题:可选类型'[Int:Int]的值?' 不展开 你是说用'!' 要么 '?'? - Issue with Swift: value of optional type '[Int : Int]?' not unwrapped; did you mean to use '!' or '?'? Facebook登录-错误消息:可选类型“ Any?”的值 不展开 你是说用'!' 要么 '?'? - Facebook login - error message: Value of optional type 'Any?' not unwrapped; did you mean to use '!' or '?'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM