调用 rangeOfCharacterFromSet 时会发生什么?

What happens when rangeOfCharacterFromSet is called?

以下代码是什么意思, 我知道这是为了避免在 TextField 中键入数字以外的字符。但是这个函数 rangeOfCharacterFromSet 背后实际上发生了什么。 会怎样 return.

if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
        return NO;

文档中说

Finds and returns the range in the receiver of the first character from a given character set. The range in the receiver of the first character found from aSet. Returns a range of {NSNotFound, 0} if none of the characters in aSet are found

我什至无法理解什么是receiver以及为什么这里使用NSNotFound。那是什么 aSet

请用一些例子来解释我以便更好地理解(比如当我按下数字以外的字符时会发生什么)

代码的作用是 returns NO 如果字符串包含不是数字的字符。

如果您调用 [myString rangeOfCharacterFromSet:mySet]myStringreceivermySetaSet

NSNotFound 只是一个任意常量,表示找到了集合中的 none 个字符。

- (void)test {
NSString *str = @"input content";
NSCharacterSet *characterSet = [NSCharacterSet decimalDigitCharacterSet];
NSRange range = [str rangeOfCharacterFromSet:characterSet];
NSLog(@"location:%ld, length:%ld", range.location, range.length);
// range.location is 9223372036854775807, in fact it is a NSNotFound which means not exists, range.length is 0

str = @"input 1";
range = [str rangeOfCharacterFromSet:characterSet];
NSLog(@"location:%ld, length:%ld", range.location, range.length);
// range.location is 6, range.length is 1

str = @"input 123";
range = [str rangeOfCharacterFromSet:characterSet];
NSLog(@"location:%ld, length:%ld", range.location, range.length);
// range.location is 6, range.length is 1

str = @"123 input 123";
range = [str rangeOfCharacterFromSet:characterSet];
NSLog(@"location:%ld, length:%ld", range.location, range.length);
// range.location is 0, range.length is 1
}

在测试中,str是接收者,当你调用一个方法时,谁调用谁是接收者:[receiver callTheMethod]。关于NSCharacterSet,我觉得文档解释的很清楚:

An NSCharacterSet object represents a set of Unicode-compliant characters. NSString and NSScanner objects use NSCharacterSet objects to group characters together for searching operations, so that they can find any of a particular set of characters during a search. The cluster’s two public classes, NSCharacterSet and NSMutableCharacterSet, declare the programmatic interface for static and dynamic character sets, respectively.

NSRange是[strrangeOfCharacterFromSet:characterSet]的结果,它是一个struct,range.location是str中包含在characterSet中的第一个索引,它是一个NSInteger类型,当str not exits a characterSet 的内容 它将是一个非常大的整数,这意味着 NSNotFound。 range.length 的意思就是字面意思,在这里,除非 range.location 是 NSNotFound.

,否则它总是 1

希望对您有所帮助。

好吧,让我用这句话来说吧:如果你不知道接收器是什么,你应该读一本关于Objective-C的入门书。

然而,...

接收者是你发送消息的对象。所以让它更容易理解:

NSCharacterSet *digits = [NSCharacterSet decimalDigitCharacterSet];
[string rangeOfCharacterFromSet:digits];

在第一行中,您将消息 decimalDigitCharacterSet 发送到 NSCharacterSet 的 class 对象。在第二行中,您将消息 rangeOfCharacterFromSet:digits 发送到 NSString.

的实例对象

所以让我们翻译文档,第 1 步:

Finds and returns the range in the string (formerly: receiver) of the first character from a given character set. The range in the receiver of the first character found from aSet. Returns a range of {NSNotFound, 0} if none of the characters in aSet are found

由于一个范围包含两个纯整数,您不能将 nil 分配给它们以标记错误。它将为 0。因此分配了一个幻数 (NSNotFound)。不过,这个不用管,看0上的长度就可以了。

集合是字符集,包含数字的集合。