mack mack - 1 month ago 12
Swift Question

Values are not updating in my text label from api calling

I have one api calling, and I am passing one parameter value to that api. And I am doing append to one model data and from there I am trying to display in my label. But when I do api calling itself and try to print the label name , Its showing crash index out of range

func showprofileapi () {

let headers = [
"cache-control": "no-cache",
"postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec"
]

let jsonObj:Dictionary<String, Any> = [
"customerID" : "5"
]

if (!JSONSerialization.isValidJSONObject(jsonObj)) {
print("is not a valid json object")
return
}

if let postData = try? JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted) {
let request = NSMutableURLRequest(url: NSURL(string: "http://MyProfile.php")! as URL,
cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
///print(error)
} else {

DispatchQueue.main.async(execute: {

if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? Dictionary<String,AnyObject>
{
let status = json["status"] as? Int;
if(status == 1)
{
print("SUCCESS....")
if (json["myprofile"] as? NSDictionary) != nil
{

print("SUCCESS ......22....")
print(json)

DispatchQueue.main.async(execute: {

print("INSIDE CATEGORIES")



self.Profileddatas.append(MyProfiledData(json:json as NSDictionary))

print("Product Name : ", self.Profileddatas[0].custName)


})


}
}
}
})
}
})

dataTask.resume()
}
}


My above code is my api calling, And when I try to print the value in my console its crashing :

// print("Product Name : ", self.Profileddatas[0].custName)


My json output after api calling is :

{
"status": 1,
"message": "My Profile Details are.",
"myprofile": {
"CustomerName": "ram",
"CustomerEmail": "ram@gmail.com",
"CustomerMobile": "",
"CustomerAddress": "",
"CustomerUsername": "",
"CustomerPassword": " "
}
}


My append data model class is :

class MyProfiledData
{
var custName : String?
var custEmail : String?
var custMobile : String?
var custAddress : String?
var custUsername : String?
var custPassword : String?

init(json:NSDictionary)
{
self.custName = json["CustomerName"] as? String
self.custEmail = json["CustomerEmail"] as? String
self.custMobile = json["CustomerMobile"] as? String
self.custAddress = json["CustomerAddress"] as? String
self.custUsername = json["CustomerUsername"] as? String
self.custPassword = json["CustomerPassword"] as? String


}
}


Please help me out.

Thanks

Answer
if (json["myprofile"] as? NSDictionary) != nil
{
         print("SUCCESS  ......22....")
        print(json)

        DispatchQueue.main.async(execute: {

                print("INSIDE CATEGORIES")
              //  print("CartID:\(CartID)")

              self.Profileddatas.append(MyProfiledData(json:json["myprofile"] as! NSDictionary))
         print("Product Name : ", self.Profileddatas[0].custName)

         self.getvalue ()

         })


}