NSCoding意外地发现没有

时间:2015-08-26 17:54:27

标签: ios swift nsobject nscoding

我有一个问题,我想不出任何解释。我有一个下面定义的课程。

在app委托中,我致电EyeGamesPrefs.loadData(),并在watchList = aDecoder.decodeObjectForKey("watchList") as! [Int]崩溃,说它遇到了零。

  • 如果我拿出与watchList变量相同的selectedConsoleIndeces变量,它可以正常工作。

  • 如果我将其更改为watchList = aDecoder.decodeObjectForKey("selectedConsoleIndeces") as! [Int],则可以正常使用。

  • 如果我将完全相同的代码复制并粘贴到空白项目中,它可以正常工作。

  • 它在iPhone 5模拟器上的当前项目中运行,但不在iPhone 6模拟器上运行。在一个新项目中,它运行在6。

任何人都可以解释为什么watchList无效但selectedConsoleIndeces无效?唯一的区别是变量的名称!为什么它会粘贴到一个不同的项目而不是在这个项目中?它没有任何意义。

class EyeGamesPrefs:NSObject,NSCoding {

// This is a shared instance available throughout the app
static var sharedInstance = EyeGamesPrefs()

// These are various preferences used thoughout the app
// They are saved and loaded from NSUserDefaults.standardUserDefaults() when the app launches or goes into the background
// See the App Delegate
var colorScheme:        Int     // index of color scheme in listOfColorSchemes
var boughtRemoveAds:    Bool
var boughtSkins:        Bool
var watchedTutorial:    Bool
var selectedConsoleIndeces:   [Int]
var watchList: [Int]

override init() {
    colorScheme      = 0
    boughtRemoveAds  = false
    boughtSkins      = false
    watchedTutorial  = false
    selectedConsoleIndeces = Array(0...(50 - 1))
    watchList = Array(0...(50 - 1))
    super.init()
}

// What to do when this object is saved to NSUserDefaults
func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(colorScheme, forKey: "colorScheme")
    aCoder.encodeObject(boughtRemoveAds, forKey: "boughtRemoveAds")
    aCoder.encodeObject(boughtSkins, forKey: "boughtSkins")
    aCoder.encodeObject(watchedTutorial, forKey: "watchedTutorial")
    aCoder.encodeObject(selectedConsoleIndeces, forKey: "selectedConsoles")
    aCoder.encodeObject(watchList, forKey: "watchList")
}

// What to do when this object is loaded from NSUserDefaults
required init(coder aDecoder: NSCoder) {
    colorScheme         = aDecoder.decodeObjectForKey("colorScheme") as! Int
    boughtRemoveAds     = aDecoder.decodeObjectForKey("boughtRemoveAds") as! Bool
    boughtSkins         = aDecoder.decodeObjectForKey("boughtSkins") as! Bool
    watchedTutorial     = aDecoder.decodeObjectForKey("watchedTutorial") as! Bool
    selectedConsoleIndeces    = aDecoder.decodeObjectForKey("selectedConsoles") as! [Int]
    watchList = aDecoder.decodeObjectForKey("watchList") as! [Int]
}



class func savePreferences() {
    let data = NSKeyedArchiver.archivedDataWithRootObject(EyeGamesPrefs.sharedInstance)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "preferences")
    NSLog("saved preferences")
}

class func loadPreferences() {
    if let data = NSUserDefaults.standardUserDefaults().objectForKey("preferences") as? NSData {
        let prefs = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! EyeGamesPrefs
        EyeGamesPrefs.sharedInstance = prefs
        NSLog("loaded preferences")
    }
}

1 个答案:

答案 0 :(得分:1)

不同之处在于selectedConsoleindeces正在解码密钥的对象" selectedConsoles"而watchList正在寻找键的对象" watchList"这是回归' nil'。这意味着没有带有键的对象" watchList"在 NSUserDefaults

你需要确保键有一个对象" watchList"和/或处理优雅地接收nil值。

您可以使用以下代码打印出整个 NSUserDefaults ,以验证您是否拥有密钥" watchList"如果是,则与之关联的变量:

println(NSUserDefaults.standardUserDefaults().dictionaryRepresentation());