简体   繁体   English

如何从 RxSwift 中的 onNext 获取正确的值?

[英]How do I get the correct value from onNext in RxSwift?

I have parsed JSON data from an URL and the subscriber on the array trigger accordingly as the array is populated.我已parsed JSON从数据URLsubscriberarray触发相应的array被填充。 But the data that I get from onNext look like this: MyProject.People .但是我从onNext得到的数据是这样的: MyProject.People How do I get the actual values?我如何获得实际值? Here's my code:这是我的代码:

guard let myURL = URL(string: "https://api.myjson.com/bins/e5gjk") else { return }
var myArray: Variable<[People]> = Variable([])

myArray.asObservable().subscribe(onNext: { arrayData in
    print("TRIGGERED", arrayData)

    }).disposed(by: bag)

Alamofire.request(myURL, method: .get)
    .validate()
    .responseJSON{ response in

    guard response.result.isSuccess else {
       print("Error")
       return
    }

    let json = JSON(response.result.value)

    for i in 0...json["employees"].count {
        let people = People()
        people.name = json["employees"][i]["firstName"].stringValue
         people.job = json["employees"][i]["job"].stringValue

         myArray.value.append(people)
    }

    for i in myArray.value {
        print(i.name)
        print(i.job)
    }
}

So, arrayData returns MyProject.People but should give strings.所以, arrayData返回MyProject.People但应该给出字符串。 I have tried arrayData.name and arrayData.value.name but it doesn't show anything.我试过arrayData.namearrayData.value.name但它没有显示任何内容。 People look like this: People看起来像这样:

class People {
    var name = ""
    var job = ""
}

I would suggested you to use Codable protocol instead of JSON pod.建议您使用Codable协议而不是JSON pod。 You can read more about Codable here: https://www.swiftbysundell.com/basics/codable/您可以在此处阅读有关Codable更多信息: https : Codable

And more about CustomStringConvertible here: https://developer.apple.com/documentation/swift/customstringconvertible以及更多关于CustomStringConvertible信息: https : //developer.apple.com/documentation/swift/customstringconvertible

This can be as simple as this:这可以像这样简单:

class Employees: Codable {
    let employees: [Employee]
}

/// If you want to print array with values
/// A textual representation of this instance.
extension Employees: CustomStringConvertible {
    var description: String {
        var text = ""
        for employee in employees {
            text += "Employee first name: \(employee.firstName), Job: \(employee.job)\n"
        }
        return text
    }
}

class Employee: Codable {
    let firstName: String
    let job: String
}

I also try simple request and it's finish successfully, i was able to get all entities: (you can change Alamofire response from responseJSON to responseData )我也尝试了简单的request ,它成功完成,我能够获得所有实体:(您可以将Alamofire响应从responseJSON更改为responseData

let employees = try! JSONDecoder().decode(Employees.self, from: response.data)
print(employees)
...
Employee first name: Jocke, Job: developer
Employee first name: Anna, Job: construction
Employee first name: Peter, Job: pilot

myArray.value is an array of [People], it's not allowed direct access people property (name, job). myArray.value 是一个 [People] 数组,不允许直接访问 people 属性(姓名、工作)。 You have to get people from particular index, then you can access people's property.您必须从特定索引中获取人员,然后才能访问人员的财产。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM