2013-09-04 3 views
5

Я следовал учебнику для использования UITableView. Готовое кодЧто делает @ [indexPath] do

- (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]; 
    } 
} 

Мой вопрос: Что @[indexPath] делать? Это то же самое, что ?:

[NSArray arrayWithObject:indexPath] 

ответ

9

Да, это то же самое, его краткое обозначение для определения массива. Вы можете сделать то же самое с NSDictionary и NSNumber. Здесь некоторые образцы (and some more here):

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

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

NSNumber *shortNotationNumber = @69; 
2

Да, это так. Это новая особенность современной цели-C.

Вы можете создать новые массивы с буквами @, как в примере, который у вас есть. Это хорошо работает не только для NSArrays, но NSNumbers и NSDictionaries тоже, как и в:

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 

Это хорошо для доступа к элементам тоже, как это:

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 

Вы можете иметь больше информации здесь: http://clang.llvm.org/docs/ObjectiveCLiterals.html

+0

Я также хотел бы задать ваш ответ как принятый ответ, но, к сожалению, SO не позволяет этого. Потому что Эмилио был быстрее, я должен дать ему кредиты. Но приятное объяснение! – Michael90