错误域=NSCocoa错误域代码=3840 "No value."



我已经在我的应用程序中为登录创建了一个脚本。用户输入他们的用户名和密码,然后我将这些值发送到服务器。我有一个 php 脚本来检查数据库值,然后将一个 json 编码数组返回给我的应用程序。我解析结果,将值存储在用户默认值中,然后进入应用程序。一切都很好,直到我实现了另一个功能来从数据库返回报价。

该错误非常奇特,因为它不是每次都发生,但频率足以导致问题。我不明白为什么


斯威夫特

let myUrl = NSURL(string: "https://www.example.com/scripts/userLogin.php");
let request = NSMutableURLRequest(url:myUrl! as URL)
request.httpMethod = "POST";
let postString = "username=(username)&password=(password)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil {
print("error=(String(describing: error))")
return
}
// Parse the results of the JSON result and naivigate to app if success
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let resultValue:String = parseJSON["status"] as! String;
if (resultValue == "Success"){
let storedUserID: String = parseJSON["user_id"] as! String;
let storedUsername: String = parseJSON["username"] as! String;
let storedFirstname: String = parseJSON["firstname"] as! String;
let storedSurname: String = parseJSON["surname"] as! String;
let storedUniName: String = parseJSON["uni_name"] as! String;
let storedUniCourse: String = parseJSON["uni_course"] as! String;
let storedEmail: String = parseJSON["email"] as! String;
let storedVerificationCode: String = parseJSON["verification_code"] as! String;
let theQuote: String = parseJSON["quote"] as! String
let array = [storedUserID, storedUsername, storedFirstname, storedSurname, storedUniName, storedUniCourse, storedEmail, storedVerificationCode, theQuote]
UserDefaults.standard.set(array, forKey: "UserDetailsArray");
UserDefaults.standard.set(true, forKey: "isUserLoggedIn");
UserDefaults.standard.synchronize();
print("Successfully logged in with:")
print(array)
DispatchQueue.main.async{
let mainTabBar = self.storyboard?.instantiateViewController(withIdentifier: "mainTabBar") as! UITabBarController;
let appDelegate = UIApplication.shared.delegate as! AppDelegate;
appDelegate.window?.rootViewController = mainTabBar
}
}
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();

PHP:

$check = query_mysql("SELECT * FROM users WHERE username='".$username."'");
if ($check->num_rows == 0) {
$returnValue["status"] = "Error";
$returnValue["message"] = "No such details found on server";
echo json_encode($returnValue);
} else {
while ($row = $check->fetch_assoc()){
$storedUserID       = $row['user_id'];
$storedUsername     = $row['username'];
$storedfirstname    = $row['firstname'];
$storedsurname      = $row['surname'];
$storedPass         = $row['password'];
$storedUniName      = $row['uni_name'];
$storedUniCourse    = $row['uni_course'];
$storedEmail        = $row['email'];
$storedVerificationCode = $row['unique_code'];
$verified           = $row['verified'];
}
if ($storedPass != $password ) {
$returnValue["status"] = "Failed";
$returnValue["message"] = "The user and password combination do not match";
echo json_encode($returnValue);
} elseif ($verified == 0){
$returnValue["status"] = "Unverified";
$returnValue["message"] = "The email associated with this account has not been verified";
echo json_encode($returnValue);
} else {
// Retrieve a motivational quote
$get_quote = query_mysql("SELECT quote FROM study_quotes ORDER BY rand() LIMIT 1");
if (!$get_quote){
$returnValue["quote"] = "Failed";
} else {
while ($row = $get_quote->fetch_assoc()) $returnValue["quote"] = $row['quote'];
}
$returnValue["status"]              = "Success";
$returnValue["user_id"]             = $storedUserID;
$returnValue["username"]            = $storedUsername;
$returnValue["firstname"]           = $storedfirstname;
$returnValue["surname"]             = $storedsurname;
$returnValue["uni_name"]            = $storedUniName;
$returnValue["uni_course"]          = $storedUniCourse;
$returnValue["email"]               = $storedEmail;
$returnValue["verification_code"]   = $storedVerificationCode;
echo json_encode($returnValue);

数据库

有一个带有 id 和引号的表(预填充了我自己的数据,用户无法访问。我知道总会有行存在,所以我从不检查num_rows但只有在查询失败时才检查)


用法

我使用 UserDefaults,因为在下一个屏幕上,我使用第 8 个索引(即引号)设置为标签:

let userDetails: [String] = UserDefaults.standard.stringArray(forKey:"UserDetailsArray")!
self.quoteLabel.text = userDetails[8]

我似乎有50%的成功率和50%的失败率。我会收到错误:

Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

感谢 rmaddy 的帮助。我发现了一个黑客,它似乎可以基本上每次重新运行该函数,直到返回的数据集不为空,但我很确定这不是正确的做法,即使它有效,所以任何帮助仍然不胜感激:

} catch let error as NSError {
let dataSet = String(data: data!, encoding: .utf8)
print("Trying again")
if (dataSet?.isEmpty)!{
self.LoginButtonTapped(self)
}
err = error
print(err!);
}

相关内容

最新更新