Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save typedef enum objects in NSUserDefaults

I have written the following code:

typedef enum {
    kProductList,
    kProductGrid,
    kProductSingleView
} ProductViewType;

ProductViewType ViewType;

In my view change method, I am changing the ViewType. When the app closes I want to save the last ViewType in a UserDefauls, so that when first time load, I can check users last ViewType and set it to default.

I have written the following code:

[[NSUserDefaults standardUserDefaults] setObject:ViewType forKey:@"ProductViewType"];

But XCode shows an warning: incompatible pointer to integer conversion.

any solutions ? thanks.

like image 274
sumon Avatar asked Nov 29 '12 11:11

sumon


1 Answers

NSUserDefaults stored objects not C data types.

Not tried this but you might be able to get away with just the following change:

[[NSUserDefaults standardUserDefaults] setObject:@(ViewType) forKey:@"ProductViewType"];

The @() notation is short-hand for [NSNumber numberWithInt:viewType].

like image 192
Stephen Darlington Avatar answered Oct 21 '22 05:10

Stephen Darlington