简体   繁体   中英

Replacing an Objective-C iOS app with a brand new Swift app while keeping values from NSUserDefaults

I have a general question when it comes to replacing an old application with a brand new one. The old application is several years old and written in Objective-C and I am developing an update from scratch in Swift.

I am wondering how to preserve a variable from the old app called " email_preference " which is stored in NSUserDefaults . How do I access the variable when the update is complete, and how do I test that I get the variable correctly (I feel like I have to do it correctly the first time or the value will be lost)?

These are the mentions I have found in the old code:

//Saves the email
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.email.text forKey:@"email_preference"];

//Gets the email
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *email = [defaults objectForKey:@"email_prefe

Thank you.

如果“新”应用程序具有与“旧”应用程序相同的包标识符,则可以像下面这样获取已保存的电子邮件:

let email = UserDefaults.standard.object(forKey: "email_preference")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM