靜態變量與非靜態變量之間的區別

學習ios時候經常遇到有static修飾的變量和沒有static的變量,在iOS中主要用到它們之間的區別是:如 static NSString *identify = @"identify";與NSString *identify = @"identify";1、作用範圍不一樣,有static修飾的identity的內容表明是全局變量,儲存在靜態數據區;2、最重要的區別在於:下面代碼在初始化tableView的cell的時候

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    //NSString *identify = @"identify";
   //static NSString *identify = @"identify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (nil == cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentfy];
    }
帶有static修飾的標識符,在cell重用的時候不會重複聲明,只是聲明一次;而沒有static修飾的標識符,在cell重用的時候每重用一次都要重新聲明一下標識符,假如有幾十萬個cell,如果聲明標識符沒有static修飾,可能發生因爲大量內存被佔用而crash掉。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章