I have two possibilities:
1) Store an object in a variable and use that variable in my code. But this uses memory to store the object right?
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL bool1 = [userDefaults boolForKey: key1];
BOOL bool2 = [userDefaults boolForKey: key2];
2) Don’t store it in a variable and create it from scratch when I needed.
BOOL bool1 = [[NSUserDefaults standardUserDefaults] boolForKey: key1];
BOOL bool2 = [[NSUserDefaults standardUserDefaults] boolForKey: key2];
What would be recommended in this case? If there’s a difference between objects then how would I know which one to use?
As far as the particular example you showed in your question is concerned, there’s no practical difference between those two ways of getting the
NSUserDefaults. Unless you are dealing with large data objects (likeUIImages) you should be concentrating on the clarity and readability of your code. If it turns out that you have a memory issue during your testing, come back and find ways to use less memory later.