iOS NSKeyedUnarchiver 没有得到正确的值

iOS NSKeyedUnarchiver not get correct value

我简化了提问的代码。

我想保存更多 class 并加载 class 模型文件。

但现在我将参数简化为模型中的一部分(lim)(FlightSettingModel.h)。

在FlightSettingModel.h我这里设置了一些参数

FlightSettingModel.h 代码如下:

 #import <Foundation/Foundation.h>

 @interface FlightSettingModel : NSObject<NSCoding>

 // lim property part
 @property float limCurrentVal;
 @property float limDefaultVal;
 @property float limMaxVal;
 @property float limMinVal;

 // .... other property part
 @end

FlightSettingModel.m 下面的代码

 #import "FlightSettingModel.h"
 @interface FlightSettingModel()
 {
 }
 @end
 @implementation FlightSettingModel

 -(instancetype)init
 {
     self = [super init];
     if(self)
     {
    
         self.limDefaultVal = 3.0;
         self.limCurrentVal = 4.0;
         self.limMaxVal = 10;
         self.limMinVal = 0;   

         // ... other property part value init .....     
     }
     return self;
 }


 - (void)setFlightSettingToDefaultValue
 {
     self.limCurrentVal = self.limDefaultVal;

 }



 - (void) encodeWithCoder: (NSCoder *)encoder
 {
     [encoder encodeFloat:self.limCurrentVal forKey:@"limCurrentVal"];

 }


 - (id)initWithCoder:(NSCoder *)decoder {
     if((self = [super init])) {
         self.limCurrentVal = [decoder decodeFloatForKey:@"limCurrentVal"];

     }
     return self;
 }
 @end

然后我设置了单例 SettingData 文件来初始化 FlightSettingModel 和其他模型 class。

SettingData 模型 header 如下所示:

 #import <Foundation/Foundation.h>
 #import "FlightSettingModel.h"

 @interface SettingData : NSObject

 @property (nonatomic,strong) FlightSettingModel *flightSettingModel;
 +(SettingData*) sharedInstance;

 @end

SettingData.m 代码如下:

 #import "SettingData.h"

 @implementation SettingData

 SettingData *sharedInstance;

 +(SettingData*) sharedInstance{
     if( sharedInstance == nil )
     {
         sharedInstance = [SettingData new];
    
     }
     return sharedInstance;
 }

 -(id) init{
     self = [super init];

     if( self )
     {
    
         self.flightSettingModel = [FlightSettingModel new];
     }

     return self;
 }

 @end

在我的故事板 (UI) 中如下所示:

当我点击保存按钮时,我想在 NSKeyedArchiver 中保存自定义 class 模型(FlightSettingModel.h)。当我点击加载时,我想使用 NSKeyedUnarchiver 从存档器加载模型并重置为滑块。

但是现在,当我将滑块拖动到其他值(例如:10)时,然后单击保存,然后关闭应用程序并重新启动应用程序。我点击加载,滑块值会变成0.

不知道为什么加载值的时候所有的值都会变成0;

我的视图控制器代码.m如下:

    #import "ViewController.h"
    #import "SettingData.h"

    @interface ViewController ()
    {
    NSString *path;
    NSString *fileName;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

// file manage
    path =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    fileName = [path stringByAppendingPathComponent:@"flightFile"];

    [self setUIValue];

    NSLog(@"---");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (IBAction)saveAction:(UIButton *)sender {

    [NSKeyedArchiver archiveRootObject:[SettingData sharedInstance].flightSettingModel toFile:fileName];
}

- (IBAction)loadAction:(UIButton *)sender {

    [SettingData sharedInstance].flightSettingModel  =   (FlightSettingModel*) [NSKeyedUnarchiver unarchiveObjectWithFile: fileName];
    [self setUIValue];

    NSLog(@"current Value:%.f",[SettingData sharedInstance].flightSettingModel.limCurrentVal);
}

- (IBAction)sliderChangedAction:(UISlider *)sender {
    [SettingData sharedInstance].flightSettingModel.limCurrentVal = sender.value;
    self.theTextField.text =  [NSString stringWithFormat:@"%.f",self.theSlider.value];
}

-(void) setUIValue
{
    // setting slider property
    self.theSlider.maximumValue = [SettingData sharedInstance].flightSettingModel.limMaxVal;
    self.theSlider.minimumValue = [SettingData sharedInstance].flightSettingModel.limMinVal;
    self.theSlider.value = [SettingData sharedInstance].flightSettingModel.limCurrentVal;

    self.theTextField.text = [NSString stringWithFormat:@"%.f",self.theSlider.value];

}

@end

有人知道我的代码哪里出了问题吗?

非常感谢。

如果要下载完整代码(问题代码),我已经在github

上传了

出现此行为是因为 [SettingData sharedInstance].flightSettingModel.limMaxVal[SettingData sharedInstance].flightSettingModel.limMinVal 为零:

-(void) setUIValue
{
    // setting slider property
    self.theSlider.maximumValue = [SettingData sharedInstance].flightSettingModel.limMaxVal;
    // self.theSlider.maximumValue = 0
    self.theSlider.minimumValue = [SettingData sharedInstance].flightSettingModel.limMinVal;
    // self.theSlider.minimumValue = 0
    self.theSlider.value = [SettingData sharedInstance].flightSettingModel.limCurrentVal;
    // [SettingData sharedInstance].flightSettingModel.limCurrentVal = 10

    self.theTextField.text = [NSString stringWithFormat:@"%.f",self.theSlider.value];

}

编辑: 您可以通过添加以下内容来修复它:

- (void) encodeWithCoder: (NSCoder *)encoder {
    [encoder encodeFloat:self.limCurrentVal forKey:@"limCurrentVal"];
    [encoder encodeFloat:self.limMaxVal forKey:@"limMaxVal"];
    [encoder encodeFloat:self.limMinVal forKey:@"limMinVal"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        self.limCurrentVal = [decoder decodeFloatForKey:@"limCurrentVal"];
        self.limMaxVal = [decoder decodeFloatForKey:@"limMaxVal"];
        self.limMinVal = [decoder decodeFloatForKey:@"limMinVal"];
    }
    return self;
}