0

我实际上试图将用户设置保存在 plist 中,以便我可以从任何地方访问它们。但是当将 UISlider 值保存到我的数组时,我总是会收到消息

将“float”发送到不兼容类型“id”的参数;

数组看起来像这样:

NSArray *value = [[NSArray alloc] initWithObjects: [theGradeSlider value], [theBackgroundSound isOn], [theButtonSound isOn], nil];

我也尝试过使用 MSMutableArray 但这也不起作用....


另一种解决方案

我尝试使用 NSUserDefaults 知道它并且它有效。

保存设置(应插入 viewDidUnload 或 viewDidDiassappear):

NSUserDefaults *settings = [[NSUserDefaults alloc] initWithUser:@"User"];

[settings setFloat:theGradeSlider.value forKey:@"theGradeSlider"];
[settings setBool:theBackgroundSound.on forKey:@"theBackgroundSound"];
[settings setBool:theButtonSound.on forKey:@"theButtonSound"];
[settings synchronize];

加载设置(应插入 viewDidLoad 或 ViewDidAppear):

NSUserDefaults *settings = [[NSUserDefaults alloc] initWithUser:@"User"];

theGradeSlider.value = [settings floatForKey:@"theGradeSlider"];
theBackgroundSound.on = [settings boolForKey:@"theBackgroundSound"];
theButtonSound.on = [settings boolForKey:@"theButtonSound"];
4

3 回答 3

2

这里的问题是您放入数组的值不是对象。为了做到这一点(尽管我建议使用字典而不是数组),您需要NSNumber为每个创建对象。

NSNumber *sliderValueObj = [NSNumber numberWithFloat: [theGradeSlader value]];
NSNumber *backgroundSoundObj = [NSNumber numberWithBool: [theBackgroundSound isOn]];
NSNumber *buttonSoundObj = [NSNumber numberWithBool: [theButtonSound isOn]];

NSArray *value = [[NSArray alloc] initWithObjects: sliderValueObj,backgroundSoundObj,buttonSoundObj, nil];

或者

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: sliderValueObj, @"slider",
                      backgroundSoundObj, @"backgroundSound",
                      buttonSoundObj, @"buttonSound",
                      nil];

为了更好的形式,使用内容作为键。

于 2012-06-07T21:32:34.573 回答
0

您应该使用NSNumber该类将值存储到数组中。特别是方法是-numberWithFloat:-numberWithBool

NSArray *value = [[NSArray alloc] 
         initWithObjects:[NSNumber numberWithFloat:[theGradeSlider value]], 
                         [NSNumber numberWithBool:[theBackgroundSound isOn]], 
                         [NSNumber numberWithBool:[theButtonSound isOn]], nil];
于 2012-06-07T21:58:30.933 回答
0

这是真的,您不能将基元存储在数组中。您最好将它们保存NSNumberNSUserDefaults. 这就是它的用途。

于 2012-06-07T21:55:10.940 回答