I want a customer to sign up and after entering the data its mapped to a user default and saved there and when i want to verify the user i call a function Verify that gets the data that is saved from the user to verify the user yet the mapping of data in GET is not working
var UserInfoDefualts: UserModel {
set {
let userJson = try! JSONEncoder().encode(newValue)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
let userJosn = KeychainWrapper.standard.string(forKey: "UserData") ?? ""
let userData = Mapper<UserModel>().map(JSONString: userJosn) ?? UserModel()
return userData
}
}
The get is not mapping the data userdata is always NIL, where in teh set its Filled properly
The SignUp Function
@IBAction func continueButtonClicked(_ sender: UIButton) {
ApiServices.shared.signUp(name: fullName.text!, number: phoneNumber.text!, gender: 1, date: birthDate.text!) { (result, statusCode) in
print(result)
if statusCode == 200 {
let userData = result
print(userData.fullName)
UserInfoDefualts = userData
ScreenLoader().pushVC(rootView: self, viewController: Verify_VC)
// Shared.logIn(userData: userData)
}
}
}
The verify function
@IBAction func verifyClicked(_ sender: Any) {
var data = UserInfoDefualts
ApiServices.shared.verifyUser(userID: UserInfoDefualts.userId ?? 0, otpCode: self.code) { (result, statusCode) in
if statusCode == 200 {
ScreenLoader().pushVC(rootView: self, viewController: Welcome_VC)
}
}
}
In the structs replace Encodable
with Codable
and remove all code related to ObjectMapper
Then replace the computed property with
var userInfoDefaults: UserModel? {
set {
let userJson = try! JSONEncoder().encode(newValue!)
let jsonString = String(data: userJson, encoding: .utf8)!
KeychainWrapper.standard.set(jsonString, forKey: "UserData")
}
get {
guard let userJosn = KeychainWrapper.standard.string(forKey: "UserData") else { return nil }
let data = Data(userJosn.utf8)
do {
return try JSONDecoder().decode(UserModel.self, from: data)
} catch {
print(error)
return nil
}
}
}
As UserModel
has no custom initializer the computed property must be an optional. So you have to write in the IBAction
guard let data = userInfoDefaults else { return }