在这种情况下,我们需要一个Objective C语言的BOOL变量指针?
我有一个可折叠UITableView的代码,其中有一个函数声明:
- (void)toggle:(BOOL*)isExpanded section:(NSInteger)section;
Run Code Online (Sandbox Code Playgroud)
它的定义是:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
*isExpanded =!*isExpanded; 这个陈述的含义是什么,因为我从未在BOOL变量的情况下使用过这种语句.
以下是在上述函数序列中调用的相同代码的其他两个函数:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
sectionDataArray是每个部分中行数的数组.我可能不清楚,但如果你明白我的意思请解释这一切.
谢谢
如果要在另一个函数中更改其值,则指向变量(或指向对象指针)的指针很有用.如果将plain BOOL传递给方法,则可以使用它,但如果更改它,则此更改仅在本地进行,因为它是按值传递的.如果您传递变量的指针/地址,您也可以更改其实际值.
如果您需要多个返回值并且不想将其包装在对象中,这会派上用场.它也是Cocoa中的一种常见模式,其中NSError变量作为指针传递,即-(BOOL) doSomethingError:(NSError **)error.
| 归档时间: |
|
| 查看次数: |
2853 次 |
| 最近记录: |