在Objective-C中,我的理解是指令@"foo"定义了一个常量字符串.如果我在多个地方使用@"foo",则引用同一个不可变的NSString对象.
为什么我经常看到这个代码片段(例如在UITableViewCell重用中):
static NSString *CellId = @"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellId];
而不仅仅是:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"CellId"];
我认为这是为了防止我在标识符名称中输入一个编译器无法识别的错误.但如果是这样,我就不能:
#define kCellId @"CellId"
避免静态NSString*位?还是我错过了什么?