Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS NSUserDefaults check if float key exists

I want to check if a float stored in NSUserDefaults is pre-existing. The Apple documentation suggests that it floatForKey will return 0 if the key does not exist.

How do I correctly tell the difference between a stored 0 and a non-existent key?

like image 229
djskinner Avatar asked Jun 01 '26 12:06

djskinner


1 Answers

A reliable way to see if a default has been set is:

if (![[NSUserDefaults standardUserDefaults] valueForKey:@"foo"]) { ... }

This works regardless of the data type.

like image 130
woz Avatar answered Jun 04 '26 01:06

woz