2013-09-04 38 views
5

Tôi đã làm theo hướng dẫn để sử dụng số UITableView. Mã đã hoàn thành@ [indexPath] làm gì

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

Câu hỏi của tôi là: @[indexPath] làm gì? Có giống như không ?:

[NSArray arrayWithObject:indexPath] 

Trả lời

9

Vâng, nó chỉ là ký hiệu ngắn để xác định mảng. Bạn cũng có thể làm tương tự với NSDictionary và NSNumber. Dưới đây là một số mẫu (and some more here):

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

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

NSNumber *shortNotationNumber = @69; 
2

Vâng, đúng vậy. Đó là một tính năng mới của mục tiêu hiện đại-C.

Bạn có thể tạo mảng mới với chữ số @ theo nghĩa đen, như trong ví dụ bạn có. Này hoạt động tốt không chỉ cho NSArrays, nhưng đối với NSNumbersNSDictionaries quá, như trong:

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 

Nó đẹp để truy cập vào các yếu tố quá, như thế này:

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 

Bạn có thể có thêm thông tin ở đây: http://clang.llvm.org/docs/ObjectiveCLiterals.html

+0

Tôi cũng muốn đặt câu trả lời của bạn làm câu trả lời được chấp nhận, nhưng tiếc là SO không cho phép điều này. Nguyên nhân Emilio nhanh hơn tôi phải cho anh ta các khoản tín dụng cho nó. Nhưng lời giải thích tốt đẹp! – Michael90