ステートメント埋め込み式

3611 ワード

C系言語ではメソッド内部に任意のペアの{}を追加することでコードの役割範囲を限定することができる.このようにするのは一般的に2つの利点があり、まず役割ドメインを超える一時変数が失効し、方法内の命名をより容易にし、必要でない参照の回収を早期に行うことができる.次にカッコを挿入することは方法の整理に有利であり、単独の方法として抽出するのに不便であるが、現在の方法内の他の部分と区別すべきコードについては、カッコを使用して自然な区分を行うことができる.コードレイアウトを使用する場合、初期化でページノードツリーが生成され、layoutSubviewsメソッドでレイアウトされます.初期化には次のようなコードがあります.
_lbTitle = [[UILabel alloc] initWithFrame:CGRectZero];  
_lbTitle.backgroundColor = [UIColor clearColor];    
_lbTitle.textAlignment = NSTextAlignmentLeft;   
_lbTitle.font = [UIFont systemFontOfSize:15];   
[self.contentView addSubview:_lbTitle];  

_lbContent = [[UILabel alloc] initWithFrame:CGRectZero];
_lbContent.font = [UIFont systemFontOfSize:14];
_lbContent.textAlignment = NSTextAlignmentLeft;     
_lbContent.numberOfLines = 3;   
_lbContent.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;     
[self.contentView addSubview:_lbContent];   

_lbTime = [[UILabel alloc] initWithFrame:CGRectZero];
_lbTime.backgroundColor = [UIColor clearColor];
_lbTime.font = [UIFont systemFontOfSize:12]; 
_lbTime.textColor = [UIColor grayColor];
_lbTime.textAlignment = NSTextAlignmentRight;   
[self.contentView addSubview:_lbTime];

これは3つのページノードが生成されただけで,実際のページはこれより複雑になる.上のコードで構成を_lbTimeのコードは_lbTitle、コンパイラはエラーを報告しません.このようなバグは発見しにくいです.したがって、このコードが山積みになっているが再利用が不可能な場合は、ローカルscopeを使用して区切ることをお勧めします.例は次のとおりです.
{ 
   UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
  label.backgroundColor = [UIColor clearColor]; 
  label.textAlignment = NSTextAlignmentLeft; 
  label.font = [UIFont systemFontOfSize:15]; 
  [self.contentView addSubview:label];
  _lbTitle = label;
} 
{
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
  label.font = [UIFont systemFontOfSize:14]; 
  label.textAlignment = NSTextAlignmentLeft; 
  label.numberOfLines = 3;
  label.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail; 
  [self.contentView addSubview:label]; 
  _lbContent = label; 
} 
{ 
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
  label.backgroundColor = [UIColor clearColor]; 
  label.font = [UIFont systemFontOfSize:12]; 
  label.textColor = [UIColor grayColor]; 
  label.textAlignment = NSTextAlignmentRight; 
  [self.contentView addSubview:label]; 
  _lbTime = label; 
}

これにより、ノード構成エラーが発生することは少なく、タイムリーに発生しやすく、コードブロックごとに集中することができます.文の埋め込み式はGNU Cの宣言拡張であるhttps://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.htmlローカル役割ドメインを式で制限しながら付与することができ、運用後、コードがよりコンパクトで清潔になります.
_lbTitle = ({ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.textAlignment = NSTextAlignmentLeft; 
    label.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:label];
    label; 
}); 
_lbContent = ({ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.font = [UIFont systemFontOfSize:14]; 
    label.textAlignment = NSTextAlignmentLeft; 
    label.numberOfLines = 3; 
    label.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail; 
    [self.contentView addSubview:label]; 
    label; 
}); 
_lbTime = ({ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont systemFontOfSize:12]; 
    label.textColor = [UIColor grayColor]; 
    label.textAlignment = NSTextAlignmentRight; 
    [self.contentView addSubview:label]; 
    label; 
});

以上のコードで、ノードツリーのコードにノードを追加するかどうか([self.contentView addSubview:label])は、ノードの初期化コードではないので、文埋め込み式に配置するかどうかは議論されます.