En Objective-C, entiendo que la directiva @ "foo" define una constante NSString. Si utilizo @ "foo" en varios lugares, se hace referencia al mismo objeto NSString inmutable.uso de NSString estático frente a constantes NSString en línea
¿Por qué aparece este fragmento de código en cuando (por ejemplo, en UITableViewCell reutilización):
static NSString *CellId = @"CellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellId];
En lugar de simplemente:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"CellId"];
supongo que es para protegerme de hacer un error tipográfico en el nombre del identificador que el compilador no captaría. Pero si es así, ¿no acabo:
#define kCellId @"CellId"
y evitar la NSString * poco estática? ¿O me estoy perdiendo algo?
Esto también ** garantiza la igualdad del puntero ** que puede proporcionarle algún beneficio de rendimiento. Hasta donde yo sé, los macro símbolos no tienen la garantía. – Eonil