NSUserDefaults与EXC_BAD_INSTRUCTION崩溃

时间:2014-07-07 04:53:57

标签: ios swift nsuserdefaults nskeyedunarchiver exc-bad-instruction

我的应用程序因EXC_BAD_INSTRUCTION(code=EXC_i386_INVOP, subcode=0x0)而崩溃。控制台记录(lldb)。我尝试将NSUserDefaultsNSKeyedArchiverNSKeyedUnarchiver一起使用。我尝试序列化的类是NSManagedObject的子类,所以我不能直接将它序列化(实际上我可以,但我不能反序列化因为它需要NSManagedObjectContext 1}})。但实际上,如果有直接串行化的方法,请说出=)

为了解决这个问题,我创建了一个与对象具有相同属性的NSDictionary,然后将其序列化。以下是:

    var userDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults();
    var data: NSData? = userDefaults.dataForKey(DefaultSessionProfileKey);

    // Only saves if necessary
    if data == nil {

        var dict: NSDictionary = [
            "firstName": profile.firstName,
            "lastName": profile.lastName,
            "nameFormat": profile.nameFormat,
            "gender": profile.gender,
            "birthday": profile.birthday,
            "picture": profile.picture,
            "uid": profile.uid
        ];

        // Save the profile to user defaults
        data = NSKeyedArchiver.archivedDataWithRootObject(dict);
        userDefaults.setObject(data, forKey: DefaultSessionProfileKey);
    }

然后做相反的事情。当从标准用户默认值中检索数据时,此代码在第二行崩溃:

    var userDefaults: NSUserDefaults = NSUserDefaults.standardUserDefaults();
    var data: NSData? = userDefaults.dataForKey(DefaultSessionProfileKey);
    var profile: Profile?;

    if data != nil {

        // Unserialize the profile data. We must serialize/deserialize an NSDictionary instead of directly using a Profile instance because Profile is NSMutableObject, which means that it cannot exist without an NSManagedObjectContext. We don't have one for the serialization/deserialization process, so we generate this dictionary first
        var dict = NSKeyedUnarchiver.unarchiveObjectWithData(data) as NSDictionary;

        if dict != nil {

            // Create the profile instance
            var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate;
            var entity: NSEntityDescription = NSEntityDescription.entityForName("Profile", inManagedObjectContext: appDelegate.managedObjectContext);

            var profile: Profile = Profile(entity: entity, insertIntoManagedObjectContext: appDelegate.managedObjectContext);

            profile.firstName = dict.objectForKey("first_name") as String;
            profile.lastName = dict.objectForKey("last_name") as String;
            profile.nameFormat = dict.objectForKey("name_format") as String;
            profile.gender = dict.objectForKey("gender") as String;
            profile.birthday = dict.objectForKey("birthday") as NSDate;
            profile.picture = dict.objectForKey("picture") as String;
            profile.uid = dict.objectForKey("uid") as String;
        }

        else {

            // A profile exists in user defaults, but it's not a valid profile, so we take the chance to clean up
            userDefaults.removeObjectForKey(DefaultSessionProfileKey);
        }
    }

1 个答案:

答案 0 :(得分:0)

NSUserDefaults.dataForKey返回一个隐式解包的可选项。尝试更改第二行NSData?到NSData !,或完全删除类型。

此外,通常在使用NSKeyedArchiver时,应该保存到磁盘而不是NSUserDefaults。默认值是键/值存储,因此无需将其存档到一个键中,只需使用data.writeToFile()保存数据