很奇怪。我预计最后的NSLog打印3,但它不是objectivec - nsrange的计算很奇怪
NSString *value = @"0(234)6";
NSRange beginParenthesis = [value rangeOfString:@"("];
NSRange endParenthesis = [value rangeOfString:@")"];
if (beginParenthesis.location != NSNotFound && endParenthesis.location != NSNotFound)
{
NSLog(@"%ld", endParenthesis.location); // 5
NSLog(@"%ld", beginParenthesis.location + 1); // 2
NSLog(@"%ld", endParenthesis.location - beginParenthesis.location + 1); // 5?
}
我保存beginParenthesis.location + 1变量...它的工作以及我的预期......为什么?
NSRange beginParenthesis = [value rangeOfString:@"("];
NSRange endParenthesis = [value rangeOfString:@")"];
if (beginParenthesis.location != NSNotFound && endParenthesis.location != NSNotFound)
{
NSInteger start = beginParenthesis.location + 1;
NSLog(@"%ld", endParenthesis.location); //5
NSLog(@"%ld", start); // 2
NSLog(@"%ld", endParenthesis.location - start); // 3
}
这些论点有什么不同?
谢谢。我很尴尬...... – user1705636
嘿,好吧不要,会发生很多次。寒意:) –