NSScanner
今天来介绍一下NSScanner, 并讲述一下如何使用这个东西来避免服务器传回的数组类型的数据错误的问题。
@class NSString, NSCharacterSet, NSDictionary;
@interface NSScanner : NSObject <NSCopying>
@property (readonly, copy) NSString *string;
@property NSUInteger scanLocation;
@property (copy) NSCharacterSet *charactersToBeSkipped;
@property BOOL caseSensitive;
@property (retain) id locale;
- (instancetype)initWithString:(NSString *)string NS_DESIGNATED_INITIALIZER;
@end
@interface NSScanner (NSExtendedScanner)
// On overflow, the below methods will return success and clamp
- (BOOL)scanInt:(int *)result;
- (BOOL)scanInteger:(NSInteger *)result NS_AVAILABLE(10_5, 2_0);
- (BOOL)scanLongLong:(long long *)result;
- (BOOL)scanUnsignedLongLong:(unsigned long long *)result NS_AVAILABLE(10_9, 7_0);
- (BOOL)scanFloat:(float *)result;
- (BOOL)scanDouble:(double *)result;
- (BOOL)scanHexInt:(unsigned *)result; // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexLongLong:(unsigned long long *)result NS_AVAILABLE(10_5, 2_0); // Optionally prefixed with "0x" or "0X"
- (BOOL)scanHexFloat:(float *)result NS_AVAILABLE(10_5, 2_0); // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
- (BOOL)scanHexDouble:(double *)result NS_AVAILABLE(10_5, 2_0); // Corresponding to %a or %A formatting. Requires "0x" or "0X" prefix.
- (BOOL)scanString:(NSString *)string intoString:(NSString **)result;
- (BOOL)scanCharactersFromSet:(NSCharacterSet *)set intoString:(NSString **)result;
- (BOOL)scanUpToString:(NSString *)string intoString:(NSString **)result;
- (BOOL)scanUpToCharactersFromSet:(NSCharacterSet *)set intoString:(NSString **)result;
@property (getter=isAtEnd, readonly) BOOL atEnd;
+ (instancetype)scannerWithString:(NSString *)string;
+ (id)localizedScannerWithString:(NSString *)string;
@end
首先, 上边的NSScanner 主要的作用就是初始化一个NSScanner, 下边的扩展呢, 就是一些扫描的方式方法。 顾明思意的, 我们重点使用下边的scanUpToCharactersFromSet
还有主类中的属性charactersToBeSkipped
.
下边我们开实际的案例。
1、在项目开发中, 很多数据都是用数组方式传输, 本地也使用数组方式添加, 通常我们避免添加出错会为数组加扩展SafeAdd
等
但在实际开发中, 这种扩展反而不是很好使用, 我们推荐使用NSScanner来做这件事情。
e.g: 应该返回数据
[100, 2, 3, 4, 5, 6]
实际返回错误的数据
[100, 2, 3, 4, 5, 6, ]
明显的看到后边多一个, 因为服务器数据没有做兼容。 所以我们在本地处理的时候就要很小心。
使用NSScanner + 服务器解决办法, 服务器传字串, 比如: "100,2,3,4,5", 本地接数据使用NSScanner处理。
E.g
NSMutableCharacterSet *whitespaceAndPunctuationSet = [NSMutableCharacterSet punctuationCharacterSet];
[whitespaceAndPunctuationSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSScanner *stringScanner = [[NSScanner alloc] initWithString:@"John & Paul & Ringo & George."];
stringScanner.charactersToBeSkipped = whitespaceAndPunctuationSet;
NSString *name;
while ([stringScanner scanUpToCharactersFromSet:whitespaceAndPunctuationSet intoString:&name]) {
NSLog(@"%@", name);
}
这样我们就可以通过NSScanner方便的处理我们接到的服务器数据。
同样 NSScanner还有很多使用的场景, 他可以方便我们处理很多固定类型的数据。
double price;
NSScanner *gasPriceScanner = [[NSScanner alloc] initWithString:@"2.09 per gallon"];
[gasPriceScanner scanDouble:&price];
// 2.09
// use a german locale instead of the default
NSScanner *benzinPriceScanner = [[NSScanner alloc] initWithString:@"1,38 pro Liter"];
[benzinPriceScanner setLocale:[NSLocale localeWithLocaleIdentifier:@"de-DE"]];
[benzinPriceScanner scanDouble:&price];
// 1.38
网友评论