2013-09-04 8 views
5

Ho seguito un tutorial per l'utilizzo di UITableView. Il codice finitoCosa fa @ [indexPath]

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     Message *message = [messageList objectAtIndex:indexPath.row]; 
     [self.persistencyService deleteMessagesFor:message.peer]; 
     [messageList removeObject:message]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
} 

La mia domanda è: Che cosa vuol @[indexPath] fare? È lo stesso di ?:

[NSArray arrayWithObject:indexPath] 

risposta

9

Sì, è lo stesso, è solo la notazione breve per la definizione di un array. Puoi fare lo stesso anche con NSDictionary e NSNumber. Ecco alcuni esempi (and some more here):

NSArray *shortNotationArray = @[@"string1", @"string2", @"string3"]; 

NSDictionary *shortNotationDict = @{@"key1":@"value1", @"key2":@"value2"}; 

NSNumber *shortNotationNumber = @69; 
2

Sì, lo è. È una nuova funzionalità dell'obiettivo moderno-C.

È possibile creare nuovi array con il valore letterale @, come nell'esempio in uso. Questo metodo funziona bene non solo per NSArrays, ma per NSNumbers e NSDictionaries troppo, come in:

NSNumber *fortyTwo = @42; // equivalent to [NSNumber numberWithInt:42] 

NSDictionary *dictionary = @{ 
    @"name" : NSUserName(), 
    @"date" : [NSDate date], 
    @"processInfo" : [NSProcessInfo processInfo] //dictionary with 3 keys and 3 objects 
}; 

NSArray *array = @[@"a", @"b", @"c"]; //array with 3 objects 

E 'bello per l'accesso agli elementi troppo, in questo modo:

NSString *test = array[0]; //this gives you the string @"a" 

NSDate *date = dictionary[@"date"]; //this access the object with the key @"date" in the dictionary 

È possibile avere maggiori informazioni qui: http://clang.llvm.org/docs/ObjectiveCLiterals.html

+0

Vorrei anche impostare la risposta come risposta accettata, ma purtroppo SO non lo consente. Perché Emilio è stato più veloce, devo dargli i crediti. Ma bella spiegazione! – Michael90