0

我正要从 API 获取数据,当我尝试从 API 打印响应时,它打印成功,当我尝试从 Object 打印数据时,它也打印成功,但是当我检查变量参加者APIService,值总是nil,这就是为什么在 UI 中传递值时它也会变成nil。如何从 JSON 中获取数据以反映Api 服务中的出席者。我对此感到很困惑。请帮我。谢谢你。

API 服务中的 getParticipant 函数

func getParticipants(passcode: String,
                     participantType: ParticipantType,
                     successBlock: @escaping (Attendees?) -> Void,
                     failureBlock: @escaping (Error) -> Void)
{
    let attendeesURL = URL(string: "\(GET_PARTICIPANTS_URL)/\(passcode)/\(participantType)")

    Alamofire.request(attendeesURL!, method: .get).responseJSON { (response) in
        print(response)

        if let error = response.error
        {
            failureBlock(error)
            return
        }

        if let attendeeJSON = response.result.value as? [Dictionary<String, Any>],
            let attendeeObj = attendeeJSON.first {
            print(attendeeObj)
            let attendees = Attendees.init(JSON: attendeeObj)
            successBlock(attendees)
            }
        }
    }

}

UI 中应该接受数据的参加者也变为 nil,因为 APIService 中的参加者为 nil

var passcode = ""
var attendees = [Attendees]()

 @IBAction func submitButton(_ sender: UIButton) {

    //readvalues

    passcode = passcodeLabel.text!


    //check if empty

    if(passcode.isEmpty)
    {
        _ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please input valid event code.")
    } else {
        getParticipants()
    }

}

//GET PARTICIPANTS FUNCTION

func getParticipants() {

   // var participantType: ParticipantType!



    let api = APIService(APIKey: passcode)

    api.getParticipants(passcode: passcode, participantType: .all, successBlock: { (attendees) in


        if let attendeesArray = attendees {

            self.attendees = [attendeesArray]
            do {
            DispatchQueue.main.async{

                _ = SCLAlertView(appearance: appearance).showError("Message", subTitle: "Details:\(self.attendees)")

                return
            }

        }
        }

    }) { (error) in
        DispatchQueue.main.async{

            _ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "Network Error:\(error)")

            return
        }
    }

示例 JSON

[
{
    "event_name": "Laugh Trip",
    "event_participants": [
        {
            "participant_id": "6f1e7fd5-6da9-4d5b-bc91-4771aeaa5235",
            "employee_number": "",
            "last_name": "name",
            "first_name": "name",
            "middle_name": "",
            "display_name": "name, name ",
            "department_name": "IT",
            "position_name": "Application Developer",
            "registered_flag": true,
            "registered_datetime": "2018-07-16T14:51:57.813",
            "registration_type": 1,
            "delete_flag": false,
            "manual_reg_flag": false,
            "out_flag": true,
            "out_datetime": "2018-07-16T14:54:00.000",
            "classification": 1,
            "others": ""
        },
4

1 回答 1

0

由于您的参加者模型的数据类型不匹配

  • 您应该检查每个参数的响应数据类型,并与您创建的参加者数据类型进行比较

我希望这会起作用,因为我遇到了这个问题并解决了它

于 2018-09-05T07:04:31.567 回答