In Objective-C, my understanding is that the directive @"foo" defines a constant NSString. If I use @"foo" in multiple places, the same immutable NSString object is referenced.
Why do I see this code snippet so often (for example in UITableViewCell reuse):
static NSString *CellId = @"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellId];
Instead of just:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"CellId"];
I assume it is to protect me from making a typo in the identifier name that the compiler wouldn't catch. But If so, couldn't I just:
#define kCellId @"CellId"
and avoid the static NSString * bit? Or am I missing something?