访问属性

时间:2016-06-08 12:27:53

标签: objective-c

我是编程和目标C的新手,抱歉,我不得不问这个基本问题。我无法弄清楚下面的代码行。为什么我需要使用&在评估属性时签署范围?但是当我再次调用属性时不使用它?是因为第一个是定位器而第二个是吸气剂?

感谢您的预付款建议!

-(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;
}

1 个答案:

答案 0 :(得分:0)

  

是因为第一个是setter而第二个是getter?

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

这是一个可能为您提供更深入细节的链接: https://www.codingunit.com/c-tutorial-call-by-value-or-call-by-reference