To set a value in NSUserDefaults
, you can use the following functions:
setBool(_:forKey:)
setFloat(_:forKey:)
setInteger(_:forKey:)
setObject(_:forKey:)
setDouble(_:forKey:)
setURL(_:forKey:)
In Swift 3 the names of function is changed to set
insted of set
folloed by the type.
set(_:forKey:)
-(void)setBool:(BOOL)value forKey:(nonnull NSString *)defaultName;
-(void)setFloat:(float)value forKey:(nonnull NSString *)defaultName;
-(void)setInteger:(NSInteger)value forKey:(nonnull NSString *)defaultName;
-(void)setObject:(nullable id)value forKey:(nonnull NSString *)defaultName;
-(void)setDouble:(double)value forKey:(nonnull NSString *)defaultName;
-(void)setURL:(nullable NSURL *)value forKey:(nonnull NSString *)defaultName;
Example usage would be:
NSUserDefaults.standardUserDefaults.setObject("Netherlands", forKey: "HomeCountry")
UserDefaults.standard.set("Netherlands", forKey: "HomeCountry")
[[NSUserDefaults standardUserDefaults] setObject:@"Netherlands" forKey:@"HomeCountry"];
public func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey:"name")
aCoder.encodeObject(unitId, forKey: "unitId")
}
required public init(coder aDecoder: NSCoder) {
super.init()
name = aDecoder.decodeObjectForKey("name") as? String
unitId = aDecoder.decodeIntegerForKey("unitId") as? NSInteger
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
name = [coder decodeObjectForKey:@"name"];
unitId = [coder decodeIntegerForKey:@"unitId"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder {
[coder encodeObject:name forKey:@"name"];
[coder encodeInteger:unitId forKey:@"unitId"];
}