将对象添加到枚举给我错误
Adding object to enum gives me error
我有一个enum
和一个指向enum
中某个对象的变量。
enum Collection {
case First, Second, Third, Fourth
}
var myCollection = Collection.Second
我想将 myCollection
传递给 NSUserDefaults
。这是我所做的:(我希望 viewDidLoad
是放置它的正确位置。)
override func viewDidLoad() {
super.viewDidLoad()
// Save the sort by NSUserDefualt
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.myCollection, forKey: "myKey")
}
在最后一行,我收到以下错误:
Cannot invoke 'setObject' with an argument list of type '(myViewController.Collection, forKey: String)'
我做错了什么,我该如何解决?
更新
这有意义吗?
let defaults = NSUserDefaults.standardUserDefaults()
let defaultRawValue = defaults.integerForKey("myKey")
if defaultRawValue != nil {
defaults.setInteger(myCollection.rawValue, forKey: "myKey")
} else {
defaults.setInteger(1, forKey: "myKey")
}
myCollection = Collection(rawValue: defaultRawValue)!
NSUserDefaults 只能取 PropertyList:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html
尝试
enum Collection:Int{
case First=1, Second, Third, Fourth
}
var myCollection = Collection.Second
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
let defaultRawValue = defaults.integerForKey("myKey")
if defaultRawValue > 0{
myCollection = Collection(rawValue: defaultRawValue)!
}
else
{
defaults.setInteger(1, forKey: "myKey")
myCollection = Collection(rawValue: 1)
}
}
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.
因此,如果您想将枚举存储在 NSUserDefaults 中,请使用 rawValue 作为您的枚举。这里我使用String,像这样:
enum Collection: String {
case First = "First"
case Second = "Second"
case Third = "Third"
case Fourth = "Fourth"
}
//create myCollection
var myCollection = Collection(rawValue: "Second")
并存储它:
override func viewDidLoad() {
super.viewDidLoad()
// Save the sort by NSUserDefualt
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.myCollection.rawValue, forKey: "myKey")
}
我有一个enum
和一个指向enum
中某个对象的变量。
enum Collection {
case First, Second, Third, Fourth
}
var myCollection = Collection.Second
我想将 myCollection
传递给 NSUserDefaults
。这是我所做的:(我希望 viewDidLoad
是放置它的正确位置。)
override func viewDidLoad() {
super.viewDidLoad()
// Save the sort by NSUserDefualt
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.myCollection, forKey: "myKey")
}
在最后一行,我收到以下错误:
Cannot invoke 'setObject' with an argument list of type '(myViewController.Collection, forKey: String)'
我做错了什么,我该如何解决?
更新
这有意义吗?
let defaults = NSUserDefaults.standardUserDefaults()
let defaultRawValue = defaults.integerForKey("myKey")
if defaultRawValue != nil {
defaults.setInteger(myCollection.rawValue, forKey: "myKey")
} else {
defaults.setInteger(1, forKey: "myKey")
}
myCollection = Collection(rawValue: defaultRawValue)!
NSUserDefaults 只能取 PropertyList:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html
尝试
enum Collection:Int{
case First=1, Second, Third, Fourth
}
var myCollection = Collection.Second
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
let defaultRawValue = defaults.integerForKey("myKey")
if defaultRawValue > 0{
myCollection = Collection(rawValue: defaultRawValue)!
}
else
{
defaults.setInteger(1, forKey: "myKey")
myCollection = Collection(rawValue: 1)
}
}
The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary.
因此,如果您想将枚举存储在 NSUserDefaults 中,请使用 rawValue 作为您的枚举。这里我使用String,像这样:
enum Collection: String {
case First = "First"
case Second = "Second"
case Third = "Third"
case Fourth = "Fourth"
}
//create myCollection
var myCollection = Collection(rawValue: "Second")
并存储它:
override func viewDidLoad() {
super.viewDidLoad()
// Save the sort by NSUserDefualt
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.myCollection.rawValue, forKey: "myKey")
}