necessary to initialize NSSet?
-
05-06-2021 - |
Question
They changed some things with Xcode 4, so I was wondering if I needed to initialize an NSSet by
NSMutableSet *variablesUsed = [NSMutableSet set];
or is
NSMutableSet *variablesUsed;
sufficient? Or does the second option initialize to nil? Is that the same as empty set?
Thanks for your help!
Solution
If you just use NSMutableSet *variablesUsed;
then you will get a pointer which is set to nil
and therefore will not be pointing to any kind of set. nil
means that it is set to nothing, and is not the same thing as an empty set. You will get no functionality out of nil
so you will then need to set variablesUsed to some "real" set in order to make much use of it.
When you use NSMutableSet *variablesUsed = [NSMutableSet set];
or NSMutableSet *variablesUsed = [[NSMutableSet alloc] init];
you are setting the pointer to a brand new, empty set which has been initialized and is ready to use.
OTHER TIPS
The latter, in the latest versions of the runtime, will be initialized to nil. In older versions the result is undefined and it is usually a best practice to assign it to nil or give it an object to point to.
In your case, you'll want to initialize it before you start using it.