NSUserDefaults standardUserDefaults setObject: forKey: not working for Multivalue preference

女生的网名这么多〃 提交于 2020-01-14 03:27:10

问题


I am trying to do following task

[[NSUserDefaults standardUserDefaults] setObject:@"Dry" forKey:@"vesselType_preference"];
[[NSUserDefaults standardUserDefaults] synchronize];

where my "vesselType_preference" is multivalue attribute, but it is not getting effected. Please help this is working for other type of attribute but not working for multivalue type.

Thanks


回答1:


    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];        
    if (![defaults objectForKey:@"vesselType_preference"])
    {
        [defaults setObject:@"Dry" forKey:@"vesselType_preference"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];

This should work.




回答2:


NSUserDefaults can only handle objects of NSDictionary, NSData, NSArray, NSString and BOOL. (There might be another in there, not sure) If you need to store a multiple value object like an array or dictionary, I would store your settings there first, then save that to the defaults.

Your code looks fine for storing information to the user defaults. Just make sure you have your object type specified before saving. (id) will not work... or won't work properly.



来源:https://stackoverflow.com/questions/9667046/nsuserdefaults-standarduserdefaults-setobject-forkey-not-working-for-multivalu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!