Domanda

Given the following snippet:

- (void)doSomething
{
    NSUInteger count;
}

What is count? Is it guaranteed to be 0?

È stato utile?

Soluzione

No, it isn't guaranteed to be zero, since it's a local automatic variable. Without initialization, its value is indeterminate. If you want it to be zero, either initialize it:

NSUInteger count = 0;

or define it as static:

static NSUInteger count;

since variables with static storage duration are implicitly intialized to zero, but note that this has side effects (namely the value persists between function calls).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top