NSUserDefaults の setValue と setObject

事象

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key String Value.'

と出てアプリがクラッシュする。

解決方法

setValue じゃなくて setObject を使う。

NSUserDefaults.setObject("String Value",forKey:"String Key")

説明

set(_:forKey:) - NSUserDefaultshttps://developer.apple.com/documentation/foundation/userdefaults/1414067-set
The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. For more information, see What is a Property List? in Property List Programming Guide.