我创建了一个mySwitchCollection,我需要从中检索boolForKey值,以便在我的视图上设置开关。 这是代码,但我对[defs boolForKey:arrayCostanti [i]];
有疑问arrayCostanti是静态NSString * arrayCostanti [] = {k3D,kAnimazione};
如何将我的arrayCostanti [i]值推送到boolForKey?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//Leggo dal defs gli stati di ogni switch e gli setto lo stato
//NSUserDefaults
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
//mySwitch1.on = [defs boolForKey: k3D];
//ciclo for per settare tutti gli stati degli switch che ho raccolto nel mySwitchCollection
for (int i = 0; i <= 45; i++){
mySwitchCollection[i].on = [defs boolForKey:arrayCostanti[i]];
}
}
答案 0 :(得分:0)
您还需要确保正确存储它。
我将假设'k3D'是您键的已定义的NSString。
// saving
mySwitch1.on = YES;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:mySwitch1.on] forKey:k3D];
// retrieving
BOOL switchOn = [[NSUserDefaults standardUserDefaults] boolForKey:k3D];
要记住的重要一点是,NSUserDefaults只能存储主要对象类型,而BOOL不是其中之一。因此,请确保首先将BOOL设置为NSNumber,然后你应该好好去。