尝试将元素添加到NSUserDefaults时出现App Delegate奇怪错误 [英] App Delegate weird error when trying to add element to NSUserDefaults

查看:154
本文介绍了尝试将元素添加到NSUserDefaults时出现App Delegate奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 7(Swift 2)上运行我的应用程序时,我遇到了一个非常奇怪的错误,该错误在我的应用程序的App Delegate类中显示线程1:信号SIGABRT"运行错误消息.但是,实际上我已经在App Delegate类中多次出现此线程1:信号SIGABRT"运行错误消息,主要是在删除代码中的插座引用并且忘记同时从情节提要中删除它时.但这肯定是我第一次尝试执行此命令时遇到同样的错误:

I've got a really weird error while running my app on Xcode 7 (Swift 2) that shows a "Thread 1: signal SIGABRT" running error message in the App Delegate class of my app. However I've actually already got this "Thread 1: signal SIGABRT" running error message in the App Delegate class lots of times, mainly when deleting an outlet reference in my code and forgetting to also delete it from storyboard. But that's certainly the first time I've got this same error when trying to make the command:

let wasteGain = WastesGainsClass(value: enteredMoney, originOrCat: segControlArray[segControl.selectedSegmentIndex], specification: plusEspecTField.text!, date: dateArray, mode: "gain")

gains.append(wasteGain)

NSUserDefaults.standardUserDefaults().setObject(gains, forKey: "gains")

发生的事情是,如果我仅注释行NSUserDefaults.standardUserDefaults().setObject(gains, forKey: "gains"),该应用程序将不会崩溃!所以错误可能就在那一行.

What happens is that if I just comment the line NSUserDefaults.standardUserDefaults().setObject(gains, forKey: "gains") the app doesn't crash! So the error might just be in that line.

如果有人可以帮助我,我会非常感谢您.

If anyone could help me, I`d thank you so much.

PS:WastesGainsClass格式如下:

PS: WastesGainsClass format is like this:

class WastesGainsClass {

    var value:Int = 0
    var origin:String
    var specification:String
    var date:[String]
    var mode:String
    var rowMode:Int = 0

    init(value:Int, originOrCat:String, specification:String, date:[String], mode:String) {

        self.value = value
        self.origin = originOrCat
        self.specification = specification
        self.date = date
        self.mode = mode

    }

}

推荐答案

来自文档:

NSUserDefaults类提供用于访问的便捷方法 常见类型,例如浮点数,双精度数,整数,布尔值和URL.一种 默认对象必须是属性列表,即(或 对于集合的实例的组合):NSData,NSString, NSNumber,NSDate,NSArray或NSDictionary.如果你想存储任何 其他类型的对象,通常应将其归档以创建一个 NSData的实例.

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. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

在Swift中,您还可以使用:

In Swift you can also use:

  • IntUIntDoubleFloatBool类型,因为它们会自动桥接到NSNumber;
  • String桥接到NSString
  • [AnyObject],因为它已桥接到NSArray;
  • [NSObject: AnyObject],因为它已桥接到NSDictionary.
  • Int, UInt, Double, Float and Bool types because they are automatically bridged to NSNumber;
  • String bridged to NSString
  • [AnyObject] because it is bridged to NSArray;
  • [NSObject: AnyObject] because it is bridged to NSDictionary.

当然,数组元素和字典值的类型必须是上述类型之一.词典密钥类型必须为NSString(或桥接的String).

Of course type of array elements and dictionary values must be one of above types. Dictionary key type must be NSString (or bridged String).

要存储任何其他类的实例,您有两个选择:

To store instances of any other class you have two options:

  1. 您的自定义类必须是NSObject的子类并符合 NSCoding协议,然后可以使用NSKeyedArchiver.archivedDataWithRootObject()将此类的对象归档到NSData并将其保存到NSUserDefaults,然后从NSUserDefaults检索它,然后使用NSKeyedUnarchiver.unarchiveObjectWithData()取消归档:

  1. Your custom class must be subclass of NSObject and conform to NSCoding protocol and then you can archive object of this class to NSData with NSKeyedArchiver.archivedDataWithRootObject() and save it to NSUserDefaults and later retrieve it from NSUserDefaults and unarchive with NSKeyedUnarchiver.unarchiveObjectWithData():

import Foundation

class WastesGainsClass: NSObject, NSCoding {
    var value: Int

    init(value: Int) {
        self.value = value
    }

    required init(coder aDecoder: NSCoder) {
        value = aDecoder.decodeObjectForKey("value") as! Int
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(value, forKey: "value")
    }
}

var gains = [WastesGainsClass(value: 1), WastesGainsClass(value: 2)]
NSUserDefaults.standardUserDefaults().setObject(gains.map {  NSKeyedArchiver.archivedDataWithRootObject($0) }, forKey: "gains")

if let gainsData = NSUserDefaults.standardUserDefaults().objectForKey("gains") as? [NSData] {
    gains = gainsData.map { NSKeyedUnarchiver.unarchiveObjectWithData($0) as! WastesGainsClass }
}

  • 您可以将自定义对象属性保存到字典并将其存储 NSUserDefaults中的字典:

  • You can save your custom object properties to dictionary and store that dictionary in NSUserDefaults:

    import Foundation
    
    class WastesGainsClass {
        var value: Int
    
        init(value: Int) {
            self.value = value
        }
    }
    
    extension WastesGainsClass {
        convenience init(dict: [NSObject: AnyObject]) {
            self.init(value: dict["value"] as? Int ?? 0)
        }
    
        func toDict() -> [NSObject: AnyObject] {
            var d = [NSObject: AnyObject]()
            d["value"] = value
            return d
        }
    }
    
    var gains = [WastesGainsClass(value: 1), WastesGainsClass(value: 2)]
    
    NSUserDefaults.standardUserDefaults().setObject(gains.map { $0.toDict() }, forKey: "gains")
    
    if let dicts = NSUserDefaults.standardUserDefaults().objectForKey("gains") as? [[NSObject: AnyObject]] {
        gains = dicts.map { WastesGainsClass(dict: $0) }
    }
    

  • 这篇关于尝试将元素添加到NSUserDefaults时出现App Delegate奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆