私は、少し速いというだけで、私の現在のアプリケーションの構築/修正につながったいくつかの入門コースを取っただけです。私は私のアプリのログイン機能に少し問題がありました。私は最近いくつかのマイナーチェンジを行いました(APIにいくつかのフィールドを追加して、フロントエンドのアプリケーションをカテゴリに追加しました)、私は挿入しようとするためにキャッチされない例外があることを示すエラーが発生しました。アプリがクラッシュする前に非プロパティリストオブジェクト。私はチェーンのifとguardを使用して構造体を変更しようとして以来、Typeを(NSMutableDictionaryに)設定していて、運がなくなっているように見えるNull値をキャッチするロジックを設定しています。WebAPIパラメータが追加されたNSDictionaryの使用
これは、コンソールからJSONプラスのエラーです:
Attempt to set a non-property-list object {
imagePath = "<null>";
email = "[email protected]";
firstname = xxxx;
id = 1;
lastname = xxxxx;
category = 1;
message = "Logged in successfully";
status = 200;
username = xxxxxx;
} as an NSUserDefaults/CFPreferences value for key parseJSON
[7286:636470] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
ava = "<null>";
email = "[email protected]";
firstname = xxxx;
id = 1;
lastname = xxxxx;
category = 1;
message = "Logged in successfully";
status = 200;
username = xxxxxx;
} for key parseJSON'
これは私がオブジェクトに対して持っている設定です。
//stores all information about current member
var member : NSDictionary?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// image to be animated
let backgroundImg = UIImageView()
// boolean to check is erroView is currently showing or not
var infoViewIsShowing = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// load content in member var
member = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// if member has been logged in/registered, keep them logged in
if member != nil {
let id = member!["id"] as? String
if id != nil {
login()
}
}
return true
}
これは私が取得/このためにJSONを使用するために使用している例示的な方法である:
// if text is entered
// remove keyboard
self.view.endEditing(true)
// shortcuts
let username = usernameTxt.text!.lowercased()
let password = passwordTxt.text!
// send request to API
// set url
let url = URL(string: "http://xxxx.xxx/xxxx/login.php")!
// request url
var request = URLRequest(url: url)
// POST to the API
request.httpMethod = "POST"
// append body to url
let body = "username=\(username)&password=\(password)"
// append body to request
request.httpBody = body.data(using: .utf8)
// launch session
URLSession.shared.dataTask(with: request) { data, response, error in
// if there's no error
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
guard let parseJSON = json else {
print("Error while parsing")
return
}
// remove keyboard
let id = parseJSON["id"] as? String
// successfully logged in
if id != nil {
// save member information from host
UserDefaults.standard.set(parseJSON, forKey: "parseJSON")
member = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// go to home page
DispatchQueue.main.async(execute: {
appDelegate.login()
})
// error
} else {
// get queue to send issue info to member
DispatchQueue.main.async(execute: {
let message = parseJSON["message"] as! String
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} catch {
// get queue to send issue info to member
DispatchQueue.main.async(execute: {
let message = "\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
} else {
// queue to send issue info to member
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
})
return
}
NULL値を返すフィールドがDBにVARCHARフィールドです。 IDとカテゴリ(両方ともINTです)を除く他のフィールドはすべてVARCHARです。他に必要な情報がある場合は、私に知らせてください。
ありがとうございます!