2013-09-04 4 views
5

क्या करता है मैंने UITableView का उपयोग करने के लिए एक ट्यूटोरियल का पालन किया। समाप्त कोड@ [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]; 
    } 
} 

मेरा प्रश्न है: @[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

हां, यह है। यह आधुनिक उद्देश्य-सी की एक नई विशेषता है।

आप अपने उदाहरण में जैसे शाब्दिक @ के साथ नए सरणी बना सकते हैं। यह अच्छी तरह से न केवल 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

मैं आपका उत्तर स्वीकार किए गए उत्तर के रूप में भी सेट करना चाहता हूं, लेकिन दुर्भाग्यवश एसओ इसे अनुमति नहीं देता है। कारण एमिलियो तेज था, मुझे उसे इसके लिए क्रेडिट देना होगा। लेकिन अच्छी व्याख्या! – Michael90

संबंधित मुद्दे