gpt4 book ai didi

php - SWIFT_MySQL fatal error : unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-11-29 22:07:21 24 4
gpt4 key购买 nike

我正在使用 SWIFT 运行一个简单的应用程序,该应用程序从远程 MySQL 服务器提取数据,但收到以下错误:

fatal error: unexpectedly found nil while unwrapping an Optional value import UIKit

ViewController 类:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

processJSONData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func processJSONData(){

let urlPath = "http://dubaisinan.host22.com/service1.php"

let url : NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url,completionHandler: {(data, respose, error) -> Void in

if error != nil {
println(error)
}
else {
//println (data)

let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
print(jsonResult)
}

})
task.resume()
}
}

//service1.php:

<?php // Create connection
$con=mysqli_connect("host_name","user_name","password","database_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Countries";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>

有人知道导致此错误的原因吗?

最佳答案

嗯,这就是为什么你不应该将错误发送为 nil。

var parseError: NSError?
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &parseError);
println(parseError)

打印出错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x7feba8c533c0 {NSDebugDescription=Garbage at end.}

JSON 解析失败,让我们看看原因:

let response = NSString(data: data, encoding: NSUTF8StringEncoding)
println(response)

您不仅打印 json,还打印脚本

Optional(
[{"Country":"UAE","Capital":"Abu Dhabi"},{"Country":"Iraq","Capital":"Baghdad"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
)

您应该避免打印 Hosting24 分析脚本。以避免这种情况(顺便说一句,这违反了 Hosting24 政策)在 mysqli_close($con);exit(); 之后添加,它将避免打印分析脚本。

最后:

if error != nil {
println(error)
}
else {
var parseError: NSError?
let result:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &parseError);

if(parseError == nil){

if let dictResult = result as? NSArray{
println(dictResult[0]["Capital"]);
//The label does not show any data in the following????
self.countryLabel.text = dictResult[1]["Country"] as? String
}
}

}

关于php - SWIFT_MySQL fatal error : unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027141/

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