我已經設置瞭解析&全部,註冊完美根據需要。但在此之後,我無法將用戶詳細信息返回到設備上以供離線使用。保存用戶a/c詳細信息從設備解析離線使用。 Swift 3
一旦用戶登錄/註冊,我需要這些對象脫機。
類名稱:User
對象:
coverPhoto
:Image.png(保存爲文件)profilePicture
:Image.png(保存爲F ILE)usersUsername
:字符串username
/email
:字符串firstName
:字符串middleName
:字符串lastName
:字符串mobileNumber
:字符串birthDate
:字符串gender
:字符串about
:字符串
我試圖存儲它使用UserDefaults.standard.set(_variable name_, forKey: "String")
但沒有奏效。 :\
這就是我一再geting。
這是我寫的:
@IBAction func loginButtonPressed(_ sender: AnyObject) {
activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = .gray
activityIndicator.layer.zPosition = 1
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
if email.text == "" || password.text == "" {
createAlert(title: "Error", message: "Please enter your username & password!")
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
} else {
PFUser.logInWithUsername(inBackground: email.text!, password: password.text!, block: { (suser, error) in
if error != nil {
var displayErrorMessage = "Some Error Occured! Please Try Again Later!"
if let errorMessage = error as? NSError {
displayErrorMessage = errorMessage.userInfo["error"] as! String
}
self.createAlert(title: "Dang it!", message: displayErrorMessage)
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
} else {
self.performSegue(withIdentifier: "toMainFunction", sender: nil)
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let users = objects {
for object in users {
if let user = object as? PFUser {
if user.objectId != PFUser.current()?.objectId {
if let email = user.username {
UserDefaults.standard.set(email, forKey: "email")
}
if let coverFile = user["coverPhoto"] {
UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
}
if let profileFile = user["profilePicture"] {
UserDefaults.standard.set(profileFile, forKey: "profilePicture")
}
if let firstName = user["firstName"] {
UserDefaults.standard.set(firstName, forKey: "firstName")
}
if let middleName = user["middleName"] {
UserDefaults.standard.set(middleName, forKey: "middleName")
}
if let lastName = user["lastName"] {
UserDefaults.standard.set(lastName, forKey: "lastName")
}
if let mobileNumber = user["mobileNumber"] {
UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
}
if let birthday = user["BirthDate"] {
UserDefaults.standard.set(birthday, forKey: "BirthDate")
}
if let gender = user["gender"] {
UserDefaults.standard.set(gender, forKey: "gender")
}
if let aboutYou = user["about"] {
UserDefaults.standard.set(aboutYou, forKey: "about")
}
if let username = user["usersUsername"] {
UserDefaults.standard.set(username, forKey: "username")
}
gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
gloEmail = UserDefaults.standard.object(forKey: "email") as! String
gloUName = UserDefaults.standard.object(forKey: "username") as! String
gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
gloAbout = UserDefaults.standard.object(forKey: "about") as! String
if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
let coverPhoto = UIImage(data: coverImageData as! Data){
gloCoverImage = coverPhoto
}
if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
let profilePicture = UIImage(data: profileImageData as! Data){
gloProfilePicture = profilePicture
}
}
}
}
}
})
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
}
})
}
}
我在這裏結合。
編輯:
EDIT#2:
突出顯示的代碼:
let query = PFUser.query()
query?.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print(error)
} else if let users = objects {
for object in users {
if let user = object as? PFUser {
//-------------------------------------------------------------------------------------//
if user.objectId != PFUser.current()?.objectId {
if let email = user.username {
UserDefaults.standard.set(email, forKey: "email")
}
if let coverFile = user["coverPhoto"] {
UserDefaults.standard.set(coverFile, forKey: "coverPhoto")
}
if let profileFile = user["profilePicture"] {
UserDefaults.standard.set(profileFile, forKey: "profilePicture")
}
if let firstName = user["firstName"] {
UserDefaults.standard.set(firstName, forKey: "firstName")
}
if let middleName = user["middleName"] {
UserDefaults.standard.set(middleName, forKey: "middleName")
}
if let lastName = user["lastName"] {
UserDefaults.standard.set(lastName, forKey: "lastName")
}
if let mobileNumber = user["mobileNumber"] {
UserDefaults.standard.set(mobileNumber, forKey: "mobileNumber")
}
if let birthday = user["BirthDate"] {
UserDefaults.standard.set(birthday, forKey: "BirthDate")
}
if let gender = user["gender"] {
UserDefaults.standard.set(gender, forKey: "gender")
}
if let aboutYou = user["about"] {
UserDefaults.standard.set(aboutYou, forKey: "about")
}
if let username = user["usersUsername"] {
UserDefaults.standard.set(username, forKey: "username")
}
gloFirstName = UserDefaults.standard.object(forKey: "firstName") as! String
gloMiddleName = UserDefaults.standard.object(forKey: "middleName") as! String
gloLastName = UserDefaults.standard.object(forKey: "lastName") as! String
gloMobNumber = UserDefaults.standard.object(forKey: "mobileNumber") as! String
gloEmail = UserDefaults.standard.object(forKey: "email") as! String
gloUName = UserDefaults.standard.object(forKey: "username") as! String
gloBirthDate = UserDefaults.standard.object(forKey: "BirthDate") as! String
gloGenderStr = UserDefaults.standard.object(forKey: "gender") as! String
gloAbout = UserDefaults.standard.object(forKey: "about") as! String
if let coverImageData = UserDefaults.standard.object(forKey: "coverPhoto"),
let coverPhoto = UIImage(data: coverImageData as! Data){
gloCoverImage = coverPhoto
}
if let profileImageData = UserDefaults.standard.object(forKey: "profilePicture"),
let profilePicture = UIImage(data: profileImageData as! Data){
gloProfilePicture = profilePicture
}
}//-----------------------------------------------------------------------------//
}
}
}
})
問題是,該應用程序崩潰,因爲它意外地發現零,同時展開一個可選的值,是否正確?你能發佈發生此次崩潰的行嗎? – nathan
@nathan我不確定這是否是你需要的 - ![鏈接](https://i.stack.imgur.com/ofioG.png)。如果不是,請指導我 –
在Xcode Debug Navigator中,您應該能夠確定哪一行_your_代碼導致了問題。 – nathan