ios - api 调用的文本标签中的值未更新

标签 ios json swift

我有一个 api 调用,我正在将一个参数值传递给该 api。我正在附加到一个模型数据,然后我试图从那里显示在我的标签中。但是当我调用 api 并尝试打印标签名称时,它显示崩溃索引超出范围

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

我上面的代码是我的 api 调用,当我尝试在我的控制台中打印值时它崩溃了:

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

调用 api 后我的 json 输出是:

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

我的附加数据模型类是:

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


    }
}

请帮帮我。

谢谢

最佳答案

更改 if (json["myprofile"] as?NSDictionary) != nil 如果让 json = json["myprofile"] 作为? NSDictionary 因为你在初始化 MyProfiledData 上下文中的 'json' 出错了

关于ios - api 调用的文本标签中的值未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40212118/

相关文章:

ios - iOS 11 中带有大标题导航栏的自定义背景图片

ios - 使用 Parse-Server 时,Parse LocalDataStore 不起作用 - 'Method requires Pinning enabled.'

javascript - JSON.parse 未正确评估 JSON 字符串

java - 我如何从 json 恢复对象映射?

php - 我有一个变量需要发送到 php 以写入文件

swift - 将带有纪元时间和时区的时间戳字符串转换为 NSDate

ios - 如何在单独的类中在 iOS Swift 中制作计时器?

iOS:向可缩放图像添加非缩放按钮

ios - ios:图像缓存和检索

ios - 如何在 Xcode 4.5 中创建 IOS 5.1 应用程序