Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the static keyword used in UITableViewCell identifiers?

Tags:

I've read up on "static" on several occasions, including just before posting this question. I'm still searching for an "Aha" though.

In the context of UITableView's static comes up in Cell Identifiers in every piece of code I've looked at. For example in a recent CellForRowAtIndexPath:

    static NSString *defaultIndentifier = @"Managed Object Cell Identifier";

My question is why do we need and use "static"?

like image 651
Aaronium112 Avatar asked Jan 13 '11 23:01

Aaronium112


2 Answers

There's no real benefit here. It's mostly just a hint to the reader that the same value is used for all cells in this particular bit of code. As the identifier itself is a constant string, it gets compiled into an immutable chunk of memory and referenced as the same pointer every time, e.g. there is no cost involved in constructing the string even if you remove the static keyword.

like image 64
Lily Ballard Avatar answered Dec 30 '22 20:12

Lily Ballard


So that it will only be constructed once. If it's not static, you'll be making one every time the message is sent (which is a lot)

like image 25
Lou Franco Avatar answered Dec 30 '22 20:12

Lou Franco