我尝试使用userDefaults从我的api中获取jobSeeker ID,但我的回复是nil。
基本上,我正在做的是:
创建一个辅助类作为NSObject并保存令牌(与我的userID不同)。
class helper: NSObject {
class func restartApp(){
guard let window = UIApplication.shared.keyWindow else {return}
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController : UIViewController
if getApiToken() == nil {
//go to auth screen
viewController = storyBoard.instantiateInitialViewController()!
} else {
//go to main screen
viewController = storyBoard.instantiateViewController(withIdentifier: "profileNavigationController")
}
window.rootViewController = viewController
UIView.transition(with: window, duration: 0.5, options: .transitionFlipFromTop, animations: nil, completion: nil)
}
class func saveApiToken(token: String){
//save api token to UserDefailts
let def = UserDefaults.standard
def.setValue(token, forKey: "token2")
def.synchronize()
restartApp()
}
class func saveJobSeekerIdDefault(defaultID: String){
//save userDefaultID to UserDefailts
let defID = UserDefaults.standard
defID.setValue(defaultID, forKey: "default_profile_id")
defID.synchronize()
restartApp()
}
class func getApiToken() -> String? {
let def = UserDefaults.standard
return def.object(forKey: "token2") as? String
}
class func getJobseekerID() -> String? {
let defID = UserDefaults.standard
return defID.object(forKey: "default_profile_id") as? String
}
}
在此之后,初始化者:
class UserProfileDefaults{
var userID : String
var defaultProfileType : String
var defaultProfileID : String
init?(profileDefaultsDict: [String:Any]){
guard let userID = profileDefaultsDict["user_id"] as? String,
let defaultProfileType = profileDefaultsDict["default_profile_type"] as? String,
let defaultProfileID = profileDefaultsDict["default_profile_id"] as? String else {return nil}
self.userID = userID
self.defaultProfileID = defaultProfileID
self.defaultProfileType = defaultProfileType
}
}
然后,我使用Alamofire获取id:
class func userProfileDefault(completion: @escaping( _ error: Error?, _ userProfileDefaults: UserProfileDefaults?) -> Void){
let url = URLs.profileDefaultsUrl
guard let api_token = helper.getApiToken() else {
completion(nil, nil)
return
}
let headers: HTTPHeaders = [
"Authorization": "Bearer \(api_token)",
"Accept": "application/json"
]
Alamofire.request(url, method: .get, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
switch response.result{
case .failure(let error):
completion(error, nil)
print(error)
case .success(let result):
let json = JSON(result)
print(json)
//Data - JobSeekerDefaults
guard let jobSeekerDefaults = json["data"].dictionaryObject else {
completion(nil, nil)
return
}
let userDefaultsData = UserProfileDefaults(profileDefaultsDict: jobSeekerDefaults)
//print("Test userID01: \(userDefaultsData?.userID)")
helper.saveJobSeekerIdDefault(defaultID: "97")
completion(nil, userDefaultsData)
}
}
}
我认为这是获取此数据的方法,但我的回报是零。 我真的希望有人帮助我,因为我长期坚持这一点并且让我发疯。
答案 0 :(得分:0)
更准确地说,您的服务器响应很可能与您不匹配。在UserProfileDefaults
类初始化设置guard
上的断点,并在profileDefaultsDict
的控制台值中打印,如po profileDefaultsDict
。确保您在初始化时提供的模型符合服务器端