I do not understand the Objective-C in the following method taken from Apple’s Master Detail Template:
- (void)insertNewObject:(id)sender
{
if (!_objects)
{
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0
inSection:0];
[self.tableView insertRowsAtIndexPaths: @[indexPath]
withRowAnimation: UITableViewRowAnimationAutomatic];
}
The code is in the MasterViewController.m file. The points of confusion for me are:
– what does @[indexPath] mean? Is this a syntax for making something an array. I have only run into the ‘@’ as the character denoting a string. Clarification: I understand what an NSIndexPath is and how it functions. I am not familiar with the syntax for putting an object in brackets preceded by the @ character. Does this make something an array? What if I had several NSIndexPaths? Can I load all of them into an array in this manner @[indexPath1, indexPath2, indexPath3] Does this work with anything derived from NSObject? Is the result always an array? Where is this documented in the language – what is this language feature called (so I can look it up)?
- The method insertRowsAtIndexPaths:withRowAnimation seems to specify the locations to put something into the table but not exactly what to put. How does the method know what object to use as the data source when inserting items into the table. The delegate and data source relationships are specified in the xib so it is apparent that the MasterViewController will handle the association of data with the table, but there does not seem to be any relationship with the
NSMutable *_objects;array and the table specified anywhere.
Thanks for the help in explaining this as it is apparent I am missing some pretty basic stuff.
Yes. In modern Objective-C, as understood by the latest
clangcompiler, there are some more@directives for easily creating hard-coded objects:creates an NSArray;
creates an NSDictionary,
creates an NSString, as usually, and
create NSNumber instances, respectively.
It uses the data source currently set on the table view it is called on.