我正在看一些代码,想知道这是如何工作的。在一节课上,我看到了类似这样的东西:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleCellIdentifier = @"SimpleCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCellIdentifier"];
....
return cell
}然后在另一个类中,我将看到与股票UITableViewCells相同的代码片段。我想知道发生了什么事
static NSString *simpleCellIdentifier;因为它是静态的,所以会在项目的整个生命周期内进行分配,对吧?那么,如果运行另一个viewController中的代码,会发生什么呢?它只是使用在另一个类中创建的旧simpleCellIdentifier吗?谢谢。
发布于 2012-07-07 04:47:24
在这种情况下,simpleCellIdentifier只存在于方法范围内。因此,在不同的方法中可以有任意多的simpleCellIdentifier,因为它们是不同的实例。
如果在类范围内声明了static变量,则无论何时在该类中读/写该变量,都是在使用同一个实例。
https://stackoverflow.com/questions/11369376
复制相似问题