Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When we need Pointer of BOOL Variable in Objective C?

Tags:

iphone

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.

Here is the link to that code

Thanks

like image 804
Developer Avatar asked Jul 01 '11 09:07

Developer


3 Answers

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 BOOL to 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 NSError variables are passed as pointers, i.e -(BOOL) doSomethingError:(NSError **)error.

like image 99
Eiko Avatar answered Sep 22 '22 09:09

Eiko


It's unclear why you did have to pass the pointer to a BOOL. Even if you were tracking the expanded status of all sections using an array of BOOLs, I would guess setting it up as an instance variable would have been better.

*isExpanded = !*isExpanded;

This is basically flipping the value of the BOOL. Since you've passed a pointer, you will be able to see the flip in the caller method.

As such, the second part indulges itself in building a list of all index paths in a particular section so that the app can delete the section using deleteRowsAtIndexPaths:withRowAnimation: method of UITableView. Somehow using deleteSections:withRowAnimation: method could've been easier. Same thing goes for insert.. methods.

An array of booleans

Assuming that you need to maintain a dynamic array of BOOLs. NSMutableArray is a proper fit.

Say, you start off with a certain number of sections that have expanded set to NO.

/* In the interface of the view controller */
NSMutableArray * expandedStatuses;

@property (nonatomic, retain) NSMutableArray * expandedStatuses;

/* In viewDidLoad or viewWillAppear:, as needed */
self.expandedStatuses = [NSMutableArray array];
for ( int i = 0; i < numberOfSections; i++ ) {
    [self.expandedStatuses addObject:[NSNumber numberWithBool:NO]];
}

Later, in your toggle:section: method do this,

/* Getting the expanded status of the section */
BOOL expanded = [[self.expandedStatuses objectAtIndex:section] boolValue];

/* Flipping the expanded status of the section */
[self.expandedStatuses replaceObjectAtIndex:section withObject:[NSNumber numberWithBool:!expanded]];

If you are interested in deleting a particular section,

 [self.expandedStatuses removeObjectAtIndex:sectionIndexToDelete];

And if you are interested in adding,

 /* Adding at the end */
 [self.expandedStatuses addObject:[NSNumber numberWithBool:NO]];

 /* Or adding it in between */
 [self.expandedStatuses insertObject:[NSNumber numberWithBool:NO] atIndex:sectionIndexToInsertAt];

So NSMutableArray is pretty flexible to play with. You can consider it as an option to switch to.

like image 30
Deepak Danduprolu Avatar answered Sep 26 '22 09:09

Deepak Danduprolu


They are intended to use isExpanded even after the exit of toggle: method with the modified value.

*isExpanded = !*isExpanded;

You could avoid using BOOL pointer in method by declaring isExpanded as the member variable (iVar) pf your class .

And use belwo

isExpanded = !isExpanded;
like image 29
Jhaliya - Praveen Sharma Avatar answered Sep 25 '22 09:09

Jhaliya - Praveen Sharma