Ios NSUserDefaults与EXC_BAD_指令一起崩溃

Ios NSUserDefaults与EXC_BAD_指令一起崩溃,ios,swift,nsuserdefaults,nskeyedunarchiver,exc-bad-instruction,Ios,Swift,Nsuserdefaults,Nskeyedunarchiver,Exc Bad Instruction,我的应用程序因EXC_BAD_指令代码=EXC_i386_INVOP,子代码=0x0而崩溃。控制台记录lldb。我正在尝试将NSUserDefaults与NSKeyedArchiver和NSKeyedUnachiver一起使用。我试图序列化的类是NSManagedObject的一个子类,所以我不能直接序列化它,实际上我可以,但是我不能反序列化它,因为它需要一个NSManagedObject上下文。但实际上,如果有一种方法可以直接序列化类,请这样说= 为了解决这个问题,我创建了一个与对象具有相同属

我的应用程序因EXC_BAD_指令代码=EXC_i386_INVOP,子代码=0x0而崩溃。控制台记录lldb。我正在尝试将NSUserDefaults与NSKeyedArchiver和NSKeyedUnachiver一起使用。我试图序列化的类是NSManagedObject的一个子类,所以我不能直接序列化它,实际上我可以,但是我不能反序列化它,因为它需要一个NSManagedObject上下文。但实际上,如果有一种方法可以直接序列化类,请这样说=

为了解决这个问题,我创建了一个与对象具有相同属性的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);
        }
    }

NSUserDefaults.dataForKey返回隐式展开的可选值。尝试更改您的第二行数据?到NSData!,或者完全删除该类型

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