How to save a string in NSUserDefaults in Swift? (iOS)
Hello everyone! I'm a beginner in iOS development with Swift. I need to save a simple text string (e.g., username) and then read it when the app launches next time. I've heard that NSUserDefaults can be used for this, but I can't figure out the syntax.\n\nHere's what I tried:\n\n```swift\nlet defaults = NSUserDefaults.standardUserDefaults()\ndefaults.setObject("John", forKey: "username")\ndefaults.synchronize()\n```\n\nBut Xcode complains about `NSUserDefaults` — it says this type is deprecated. I'm using Swift 5 and iOS 15. Then I tried `UserDefaults.standard`:\n\n```swift\nlet defaults = UserDefaults.standard\ndefaults.set("John", forKey: "username")\ndefaults.synchronize()\n```\n\nThis seems to work, but I'm not sure if I'm doing it correctly. Do I need to call
No answers yet.
Log in to leave an answer