2016-06-08 190 views
0

我是新来的编程和目标C,抱歉,我不得不问这个基本问题。我无法弄清楚下面的代码行。为什么评估属性时我需要使用&符号作为范围?但是当我再次调用属性时不使用它?是因为第一个是一个setter,第二个是getter?访问属性

感谢您的建议!

-(NSAttributedString*)characterWithAttribute: (NSString*)attributeName{ 
    NSMutableAttributedString* characters = [[NSMutableAttributedString alloc]init]; 

    int index = 0; 

    while(index < [self.textToAnalyze length]){ 

     NSRange range; 

     id value = [self.textToAnalyze attribute:attributeName atIndex:index effectiveRange:&range]; 

     if(value){ 

      [characters appendAttributedString:[self.textToAnalyze attributedSubstringFromRange:range]]; 
      index = (int)range.location + (int)range.length; 
     } 
     else{ 
      index++; 
     } 
    } 

    return characters; 
} 

回答

0

是因为第一个是一个setter和第二个是吸气剂?

基本上,是的。和号指定变量的地址而不是值。当被调用的例程有一个地址时,它可以为它分配一个值供你在返回时使用。如果被调用的例程只有一个值,它将无法以调用者可访问的任何方式替换它。

这里有可能会给你更深入的细节链接: https://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference

+0

感谢菲利普感谢你的解释和链接 –