asked    Devin     2018-10-22       ios       75 view        1 Answer

[SOLVED] Parsing JSON, data turns Nil in API Service Using Alamofire

I was about to get data from API, when I tried to print the response from the API, it printed successfully, and when I tried to print the data from an Object, it also printed successfully, but when I checked the variable attendees in my APIService, the value is always nil,that's why when passing the value in UI it also turns into nil. How can I get the data from JSON to reflect to attendess in Api Service. I am too puzzled regarding this. Please help me. Thank you.

getParticipant Function in API Service

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)
            }
        }
    }

}

attendees in UI which should accept data also turns into nil because attendees in APIService is 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
        }
    }

Sample 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": ""
        },

  1 Answer  

        answered    Roderick     2018-10-22      

Because of your Attendees model's data type is mismatched

  • You should check every parameter's datatype of response and compare with your created Attendees datatype

I hope this will work because of I faced this problem and solved it





Your Answer





 2018-10-22         Tammy

Using returned arrays

I am trying to run a procedure that takes an array called ds_out, changes values of ds_out(0) and ds(1) to either 0 or 1 depending on the state of a checkbox and returns it. I want to then output the values after the procedure but it looks like the procedure is not returning the array and printing the values in the initialising array. If you have the puts lines in the procedure it works.I've looked at tutorials and examples but I don't understand them. I want the basic of basic examples but can't find them.Below is the code I have used:global ds_outarray set ds_out {0 01 ...
 tcl                     1 answers                     52 view
 2018-10-22         Miles

zoom image on mouse scroll in angular4?

I have a piece of code which will zoomin/out on mouse scroll as shown below<!DOCTYPE html><html><body><script src='https://www.cssscript.com/demo/image-zoom-pan-mouse-interactions-zoomnpan/zoomnpan.js'></script><style>#demo { width: 460px; height: 250px; background-image: url(https://unsplash.it/3000/2000/?random); background-repeat: no-repeat; background-position: 50% 50%;}</style><div id="demo"></div><script>var p;function enable(){ p = new ZoomNPan(demo); p.minScale = 100;}enable();</script></bod...
 javascript                     2 answers                     51 view
 2018-10-22         Hamiltion

Anchor link is not working from JSON string in ionic 3 and angular 4 : ur "<a (click)='openActionPage()'>terms and conditions</a>"

Am using this string in JSON file.In order to proceed, you need to agree with our "terms and conditions" and adding for accessing in frontend using <message *ngFor="let message of messages" [innerHTML]="message.content"> {{message.content}}</message>but the link openActionPage() is not opening in front end. Can anyone please suggest how me to solve this ? Thank you in advance so what you are trying to do here is to add dynamically some html code as innerHTML with anchor tag (a) and event binding to a method inside your ts file - this won't really work like...
 angular                     1 answers                     55 view