Swift: NSUserDefaults – There IS a difference between the two in Swift 4 and Swift 5
The other day, I needed to use the simple NSUserDefaults to save information to the iOS device in my new Swift 5 project.
Thank you for reading this post, don't forget to subscribe!
Not using it that often, I went up on Google and found this helpful article. But to my surprise, the example was for Swift 4. Fortunately, Xcode helped me “convert” the code to Swift 5.
Here are the differences:
Swift 4:
let defaults = UserDefaults.standard
if let item = defaults.string(forKey: “itemCount”)
{
print (item)
}
Swift 5:
let defaults = UserDefaults.standard
defaults.set(item, forKey: “itemCount”)
{
print(item)
}
Note: “item” is a string variable that I am using to save an Integer that I defined earlier in the code.
About Post Author
Discover more from CompuScoop.com
Subscribe to get the latest posts sent to your email.