我看了一段關於ios7編程的視頻,並在下面看到了一段代碼。NSrange在這裏工作?
我無法真正瞭解變量範圍如何在這裏工作。
我們創建一個NSrange變量範圍,並且永遠不會給它一個值,所以我認爲&範圍指針應該返回nil。
那麼如何在有效範圍內使用以及range.location和range.length中的值是什麼?
非常感謝你的幫助!
- (NSMutableAttributedString *)charactersWithAttribute:(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 = range.location+range.length;
}
else{
index++;
}
}
return characters;
}
查看'attribute:atIndex:effectiveRange:'的代碼。注意它在'NSRange'指針中傳遞的內容。 – rmaddy 2014-10-17 18:15:17