Click to find out more information

The other day, I needed to use the simple NSUserDefaults to save information to the iOS device in my new Swift 5 project.

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

(Visited 31 times, 1 visits today)

Dan Uff
Senior Writer / Owner
https://www.compuscoop.com/