gpt4 book ai didi

ios - Swift JSON 崩溃与零

转载 作者:搜寻专家 更新时间:2023-11-01 07:34:18 25 4
gpt4 key购买 nike

我有一个问题.. 我有一个应用程序通过 jSON 连接数据库,现在的问题是他无法在数据库的响应中找到该元素。这是代码:

 func uploadtoDB(){
var SelectVehicle = save.stringForKey("SelectVehicleChoosed")

if SelectVehicle == nil {

var alertView:UIAlertView = UIAlertView()
alertView.title = "Submit Failed!"
alertView.message = "Please Select the Vehicle"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()


}else {

var post:NSString = "vechicleNumber=\(SelectVehicle)"

NSLog("PostData: %@",post);

var url:NSURL = NSURL(string: "http://saimobileapp.com/services/sai_service_history.php?")!

var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")


var reponseError: NSError?
var response: NSURLResponse?

var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)

if ( urlData != nil ) {
let res = response as! NSHTTPURLResponse!;

NSLog("Response code: %ld", res.statusCode);

if (res.statusCode >= 200 && res.statusCode < 300)
{
var responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

NSLog("Response ==> %@", responseData);
println(responseData)
var error: NSError?

let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as! NSDictionary


var serviceDate = ((jsonData as NSDictionary)["serviceHistory"] as! NSDictionary) ["serviceDate"] as! String
var serviceType = ((jsonData as NSDictionary)["serviceHistory"] as! NSDictionary) ["serviceType"] as! String
var kms = ((jsonData as NSDictionary)["serviceHistory"] as! NSDictionary) ["mileage"] as! String
var serviceLocation = ((jsonData as NSDictionary)["serviceHistory"] as! NSDictionary) ["serviceLocation"] as! String


var serviced:Void = save.setObject(serviceDate, forKey: "ServiceDateChoosed")
var servicet:Void = save.setObject(serviceType, forKey: "ServiceTypeChoosed")
var kmsc:Void = save.setObject(kms, forKey: "KmsChoosed")
var servicel:Void = save.setObject(serviceLocation, forKey: "ServiceLocationChoosed")
save.synchronize()

TableView.reloadData()

}
}
}
}

这是回应

{"serviceHistory":[{"id":"2","vehicleNumber":"mh03aw0001","mobileNumber":"9503322593","customerName":"samsun","serviceDate":"2012-06-02","serviceType":"Paid Service","mileage":"65","serviceState":"Maharashtra","serviceLocation":"Lower Parel","PUC":""}]}

应用程序在 var serviceDate = ((jsonData as NSDictionary)["serviceHistory"] as!NSDictionary) ["serviceDate"] as 行崩溃! String 和 nil 因为我认为他找不到元素。

在此先感谢您的帮助。

最佳答案

// serviceHistory is an Array
var serviceHistoryArray = jsonData["serviceHistory"] as! NSArray

// fetch the first item...
var serviceHistoryItem = serviceHistoryArray[0] as! NSDictionary
var serviceDate = serviceHistoryItem["serviceDate"]
var serviceType = serviceHistoryItem["serviceType"]
var kms = serviceHistoryItem["mileage"]
var serviceLocation = serviceHistoryItem["serviceLocation"]

关于ios - Swift JSON 崩溃与零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30937236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com