In which case we need a pointer of a BOOL variable in Objective C language?
I have a code for collapsible UITableView in which there is a function declaration:
- (void)toggle:(BOOL*)isExpanded section:(NSInteger)section;
and its definition is:
- (void)toggle:(BOOL*)isExpanded section:(NSInteger)section
{
*isExpanded = !*isExpanded;
NSArray *paths = [self indexPathsInSection:section];
if (!*isExpanded)
{
[self.tableview deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
}
else
{
[self.tableview insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
}
*isExpanded = !*isExpanded; What is the meaning of this statement as I have never used this kind of statement in case of BOOL Variable.
Following are other two functions of same code which are called in the sequence of above function:
- (NSArray*)indexPathsInSection:(NSInteger)section
{
NSMutableArray *paths = [NSMutableArray array];
NSInteger row;
for ( row = 0; row < [self numberOfRowsInSection:section]; row++ )
{
[paths addObject:[NSIndexPath indexPathForRow:row inSection:section]];
}
return [NSArray arrayWithArray:paths];
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return [[sectionDataArray objectAtIndex:section] count];
}
sectionDataArray is the array for number of rows in each section. I may be unclear but if you got my point please explain all this.
Thanks
Pointers to variables (or pointer to pointers of objects) are useful, if you want to change their value in another function. If you pass a plain
BOOLto a method, you can use it, but if you change it, this change is only local as it was passed by value. If you pass the pointer/address of the variable instead, you can also change its real value.This comes in handy if you need more than one return value and don’t want to wrap it up in an object. It’s also a common pattern in Cocoa where
NSErrorvariables are passed as pointers, i.e-(BOOL) doSomethingError:(NSError **)error.