简体   繁体   English

如何从AnyObject转换UserDefault值? 到一个整数,所以我可以保存它

[英]How do I convert a UserDefault value from AnyObject? to an Int so I can save it

I'm trying to learn how to work with UserDefaults and can't figure out how to store one of the values I need to store. 我正在尝试学习如何与UserDefaults一起使用,并且无法弄清楚如何存储需要存储的值之一。

I have this enum: 我有这个枚举:

enum CalculationFormula: Int {

  case Epley

  case Brzychi

  case Lander

  case Lombardi

  case MayhewEtAl

  case OConnerEtAl
}

and this class with a property called 'selectedFormula' that I want to be able to store as a NSUserDefaults value: 此类具有名为“ selectedFormula”的属性,我希望能够将其存储为NSUserDefaults值:

class CalculatorBrain: NSObject {

  var weightLifted: Double
  var repetitions: Double
  var oneRepMax: Double?
  var selectedFormula = CalculationFormula.Epley

  init(weightLifted: Double, repetitions: Double) {
    self.weightLifted = weightLifted
    self.repetitions = repetitions
    let userDefaults = NSUserDefaults.standardUserDefaults()
    self.selectedFormula = userDefaults.objectForKey("selectedFormula") <-- error
  }

The self.selectedFormula line gives me an error: self.selectedFormula行给我一个错误:

'Cannot assign value of type 'AnyObject?' to type 'CalculationFormula'

After much searching on SO and reading the Apple documentation I learned a lot about UserDefaults and AnyObject, but didn't find an answer. 在进行大量搜索并阅读了Apple文档之后,我学到了很多有关UserDefaults和AnyObject的知识,但是没有找到答案。

I thought I did when I tried to cast it as an Int: 当我尝试将其转换为Int时,我以为是这样做的:

self.selectedFormula = userDefaults.objectForKey("selectedFormula") as! Int

// 'Cannot assign value of type 'Int' to type 'CalculationFormula'

and

self.selectedFormula = userDefaults.objectForKey("selectedFormula") as Int

// 'Cannot convert value of type 'AnyObject?' to type 'Int' in coercion

I know the preference values need to be one of these types (NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary) but I'm storing an Int so I should be good there. 我知道首选项值必须是这些类型之一(NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary),但是我要存储一个Int,所以我应该在那里。

I'm just stuck and would really appreciate some help. 我只是被困住了,非常感谢您的帮助。 Please and thank you. 谢谢,麻烦您了。

Since your enum 自从你的枚举

enum CalculationFormula: Int {
    case Epley, Brzychi, Lander, Lombardi, MayhewEtAl, OConnerEtAl
}

extends Int you can use its rawValue . 扩展Int可以使用其rawValue So you can read/save an Int from/to NSUserDefaults . 因此,您可以从NSUserDefaults读取/保存一个Int

This is how you save a formula 这是保存公式的方式

func saveFormula(formula: CalculationFormula) {
    NSUserDefaults.standardUserDefaults().setInteger(formula.rawValue, forKey: "selectedFormula")
}

And this is how you retrieve it 这就是您检索它的方式

func loadFormula() -> CalculationFormula? {
    guard NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys.contains("selectedFormula") else {
        print("No value found")
        return nil
    }
    guard let retrievedFormula = CalculationFormula(rawValue: NSUserDefaults.standardUserDefaults().integerForKey("selectedFormula")) else {
        print("Wrong value found")
        return nil
    }
    return retrievedFormula
}

您必须使用rawValue初始化枚举:

self.selectedFormula = CalculationFormula(rawValue: userDefaults.objectForKey("selectedFormula") as! Int)

You should save your value as: 您应该将值另存为:

userDefaults.setInteger(yourValue, forKey: "yourKey")

Load your value as: 将值加载为:

userDefaults.integerForKey("yourKey") // Returns 0 if the value is nil.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM