Why does initial assignment to nil only happen once for a local static variable in a class method?

Solutions Collect From Internet About “Why does initial assignment to nil only happen once for a local static variable in a class method?”

Because statics are initialised only once. Despite the fact that the variable is inside the function, it’s storage duration is that of the entire program. It’s initialised once and maintains its value in between invocations of the function.

That code you posted is exactly the same conceptually as:

NSArray *motivations = nil;
+ (NSArray *)motivations {
    if (!motivations) {
        motivations = [[NSArray alloc] initWithObjects:@"Greed", @"Revenge",
            @"Bloodlust", @"Nihilism", @"Insanity", nil];
    }
    return motivations;
}

in terms of the storage duration (though not the scope since motivations is now visible from outside). What you have here is a kind of singleton pattern which initialises the array to nothing, then populates it once the first time you use this code.

The ISO C99 standard (yes, I do realise this isn’t C but the concepts are the same) states:

All objects with static storage duration shall be initialized (set to their initial values) before program startup.