Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store the integer value to the NSUserdefaults in objective c? [duplicate]

Tags:

objective-c

int ramdomnumber = arc4random_uniform(900000) + 100000;
[[NSUserDefaults standardUserDefaults] setObject:ramdomnumber forKey:@"contactno"];
[[NSUserDefaults standardUserDefaults] synchronize];

i tried the above code but it showing the error like implicit conversion of 'int' to 'id_nullable' is disallowed with ARC.can anyone suggest me how to store the int value in the nsuserdefaults .

like image 241
pavithra Avatar asked Jan 23 '26 09:01

pavithra


1 Answers

The error occurs because you are passing a scalar type to a method which expects an object.

There is a dedicated method, it's good programming habit to cast the value to the expected type;

NSInteger ramdomnumber = (NSInteger)(arc4random_uniform(900000) + 100000);
[[NSUserDefaults standardUserDefaults] setInteger:ramdomnumber forKey:@"contactno"];
// [[NSUserDefaults standardUserDefaults] synchronize];

synchronize is not needed.

like image 77
vadian Avatar answered Jan 24 '26 23:01

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!