Categories Obj-C, NestedArrays
I'm reading an example in More Iphone Development book in the Core Data section, and the author creates a category to turn the NSIndexPath into its row key and row label. Here's the code:
@implementation NSArray(NestedArrays)
- (id)nestedObjectAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray *subArray = [self objectAtIndex:section];
if (![subArray isKindOfClass:[NSArray class]])
return nil;
if (row >= [subArray count])
return nil;
return [subArray objectAtIndex:row];
}
- (NSInteger)countOfNestedArray:(NSUInteger)section {
NSArray *subArray = [self objeectAtIndex:section];
return [subArray count];
}
@end
In the first method, after开发者_如何学运维 he gets the row and section for the NSIndexPath, I'm not sure what's going on afterwards. I don't see why he creates a new array at the section, and then I don't understand the reason behind the two if statements that follow. Thanks in advance.
The code does not actually create a new array, the author is simply holding a reference to an object that is already stored in self
. Annotations for each line are below.
Get the row from the indexPath:
NSUInteger row = [indexPath row];
Get the section index from the indexPath:
NSUInteger section = [indexPath section];
Dereference the array within self
at index section
NSArray *subArray = [self objectAtIndex:section];
If the sub array is not really an array, fail:
if (![subArray isKindOfClass:[NSArray class]])
return nil;
If the row within the sub array is greater than its last index, fail:
if (row >= [subArray count])
return nil;
Return the object in the sub array at the given row:
return [subArray objectAtIndex:row];
Maybe it would help if some comments were included:
// Get the subarray corresponding to the requested section
NSArray *subArray = [self objectAtIndex:section];
// verify that it is really an NSArray
if (![subArray isKindOfClass:[NSArray class]])
return nil; // not an array - fail
// Check the array subscript (row #) against the size of the subarray
if (row >= [subArray count])
return nil; // out of bounds - fail
return [subArray objectAtIndex:row]; // return the requested element
He doesn't create a new array. He get's the "sub" array that represents the section.
The first if statements is there because you can create a NSArray that (let's say) is full of NSStrings. And if the subItem would be a NSString, objectAtIndex: would raise an exception.
And the second one protects you when you try access an item that is on a index that is not in the subArray. If you wouldn't return nil at this position there would be an exception too.
First, he is not creating a new array. He is just taking object for specified indexPath and checks weather it is array. If it is not array, or specified row is larger then array's size he returns nil.
He gets the row and the section, and then he gets the array specified by that section. Then he does two sanity checks:
Is thing I've got actually an array?
Is the row number valid for this particular array?
If both of these are true, he returns the object from the section array at the index specified by row
.
精彩评论