Tutorial by Examples

To set a value in NSUserDefaults, you can use the following functions: Swift < 3 setBool(_:forKey:) setFloat(_:forKey:) setInteger(_:forKey:) setObject(_:forKey:) setDouble(_:forKey:) setURL(_:forKey:) Swift 3 In Swift 3 the names of function is changed to set insted of set folloed by ...
To get a value in NSUserDefaults you can use the following functions: Swift arrayForKey(_:) boolForKey(_:) dataForKey(_:) dictionaryForKey(_:) floatForKey(_:) integerForKey(_:) objectForKey(_:) stringArrayForKey(_:) stringForKey(_:) doubleForKey(_:) URLForKey(_:) Objective-C -(nullab...
NSUserDefaults are written to disk periodically by the system, but there are times when you want your changes saved immediately, such as when the app transitions into background state. This is done by calling synchronize. Swift NSUserDefaults.standardUserDefaults().synchronize() Objective-C [[...
While you can use the NSUserDefaults methods anywhere, it can sometimes be better to define a manager that saves and reads from NSUserDefaults for you and then use that manager for reading or writing your data. Suppose that we want to save a user’s score into NSUserDefaults. We can create a class ...
Swift let bundleIdentifier = NSBundle.mainBundle().bundleIdentifier() NSUserDefaults.standardUserDefaults().removePersistentDomainForName(bundleIdentifier) Objective-C NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersi...
Every application needed to store User Session or User related details inside application in UserDefaults.So we made whole logic inside a Class for managing UserDefaults better way. Swift 3 import Foundation public struct Session { fileprivate static let defaults = UserDefaults.standard ...

Page 1 of 1