官方的文档
A string parser that scans for substrings or characters in a character set, and for numeric values from decimal, hexadecimal, and floating-point representations.
An NSScanner object interprets and converts the characters of an NSString object into number and string values. You assign the scanner’s string when you create the scanner, and the scanner progresses through the characters of that string from beginning to end as you request items.
Because of the nature of class clusters, a scanner object isn’t an actual instance of the NSScanner class, but is one of its private subclasses. Although a scanner object’s class is private, its interface is public, as declared by this abstract superclass, NSScanner. The objects you create using this class are referred to as scanner objects (and when no confusion will result, merely as scanners).
To set an NSScanner object to ignore a set of characters as it scans the string, use the charactersToBeSkipped property. Characters in the skip set are skipped over before the target is scanned. The default set of characters to skip is the whitespace and newline character set.
To retrieve the unscanned remainder of the string, use [[scanner string] substringFromIndex: [scanner scanLocation]].
有道翻译
NSScanner是一个字符串解析器,用于扫描子字符串或字符集,以及十进制、十六进制和浮点表示法中的数值。
NSScanner对象把NSString对象的字符集解释并转换为number和string类型的值。在创建扫描器时为扫描器分配字符,当要从NSScanner对象获取内容时,扫描器会从头到尾遍历整个字符串(string)。
由于类簇的属性,scanner对象不是NSScanner类的实例,而是它的一个私有子类实例。虽然scanner对象的类是私有的,但是它的接口是公开的,这是由抽象超类NSScanner声明的。使用这个类创建的对象被称为扫描仪对象(当没有混淆时,仅仅作为扫描仪)。
当扫描一个字符串时要一个NSScanner对象去忽略一组字符集,请使用charactersToBeSkipped属性。在扫描目标字符串之前,那些忽略字符集中的字符将会被跳过。默认忽略字符集中包括空格和换行字符。
要获取未扫描的字符串,使用
[[scanner string] substringFromIndex: [scanner scanLocation]]。
NSScanner类是一个类簇的抽象父类,该类簇为一个从NSString对象扫描值的对象提供了程序接口。
主要的应用:
1.将字符串转换为number或者string数值
2.
注:使用的时候请注意每次扫描之后的位置
属性:
@property (readonly, copy) NSString *string; #要扫描的字符串
@property NSUInteger scanLocation; #扫描停止的位置即扫描到某字符时停止的位置
@property (nullable, copy) NSCharacterSet *charactersToBeSkipped; #忽略字符集
@property BOOL caseSensitive;#是否对字符的大小写敏感
@property (nullable, retain) id locale;
实例方法:
// On overflow, the below methods will return success and clamp
- (BOOL)scanInt:(nullable int *)result;
- (BOOL)scanInteger:(nullable NSInteger *)result API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
- (BOOL)scanLongLong:(nullable long long *)result;
- (BOOL)scanUnsignedLongLong:(nullable unsigned long long *)result API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
- (BOOL)scanFloat:(nullable float *)result;
- (BOOL)scanDouble:(nullable double *)result;
- (BOOL)scanHexInt:(nullable unsigned *)result; // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexLongLong:(nullable unsigned long long *)result API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexFloat:(nullable float *)result API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
- (BOOL)scanHexDouble:(nullable double *)result API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)); // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
注意:扫描的时候是从扫描的初始位置开始 默认是nil
(BOOL)scanString:(NSString *)string intoString:(NSString * _Nullable * _Nullable)result;#string作为整个字符比对scanner的字符串 从当前位置开始扫描 若扫描到该字符串则返回yes 同时result 为该字符串
(BOOL)scanCharactersFromSet:(NSCharacterSet *)set intoString:(NSString * _Nullable * _Nullable)result;#反模式 按照字符扫描 当扫描到第一个不包含在忽略字符集中的字符时即停止
(BOOL)scanUpToString:(NSString *)string intoString:(NSString * _Nullable * _Nullable)result;#扫描按照字符串 扫描到即停止
(BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)set intoString:(NSString * _Nullable * _Nullable)result;#按照字符扫描 注意当字符串第一个字符就包含在内时返回no
/*
Flag that indicates whether the receiver has exhausted all significant characters.
YES if the receiver has exhausted all significant characters in its string, otherwise NO.
If only characters from the set to be skipped remain, returns YES.
*/
@property (getter=isAtEnd, readonly) BOOL atEnd;
通常用于while循环
用于判断是否遍历了所有的字符
(instancetype)scannerWithString:(NSString *)string;
(id)localizedScannerWithString:(NSString *)string;
demo