Class

Scanner

A string parser that scans for substrings or characters in a character set, and for numeric values from decimal, hexadecimal, and floating-point representations.

Declaration

class Scanner : NSObject

Overview

A Scanner object interprets and converts the characters of a String 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 Scanner 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, Scanner. The objects you create using this class are referred to as scanner objects (and when no confusion will result, merely as scanners).

To set a Scanner 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.substring(from: scanner.scanLocation).

Topics

Creating a Scanner

class func localizedScanner(with: String) -> Any

Returns an NSScanner object that scans a given string according to the user’s default locale.

init(string: String)

Returns an NSScanner object initialized to scan a given string.

Getting a Scanner’s String

var string: String

The string the scanner will scan.

Configuring a Scanner

var scanLocation: Int

The character position at which the receiver will begin its next scanning operation.

var caseSensitive: Bool

Flag that indicates whether the receiver distinguishes case in the characters it scans.

var charactersToBeSkipped: CharacterSet?

Character set containing the characters the scanner ignores when looking for a scannable element.

var locale: Any?

The locale to use when scanning.

Scanning Characters and Strings

func scanCharacters(from: CharacterSet, into: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool

Scans the string as long as characters from a given character set are encountered, accumulating characters into a string that’s returned by reference.

func scanUpToCharacters(from: CharacterSet, into: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool

Scans the string until a character from a given character set is encountered, accumulating characters into a string that’s returned by reference.

func scanString(String, into: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool

Scans a given string, returning an equivalent string object by reference if a match is found.

func scanUpTo(String, into: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool

Scans the string until a given string is encountered, accumulating characters into a string that’s returned by reference.

Scanning Numeric Values

func scanDecimal(UnsafeMutablePointer<Decimal>?) -> Bool

Scans for an NSDecimal value, returning a found value by reference.

func scanDouble(UnsafeMutablePointer<Double>?) -> Bool

Scans for a double value, returning a found value by reference.

func scanFloat(UnsafeMutablePointer<Float>?) -> Bool

Scans for a float value, returning a found value by reference.

func scanHexDouble(UnsafeMutablePointer<Double>?) -> Bool

Scans for a double value from a hexadecimal representation, returning a found value by reference.

func scanHexFloat(UnsafeMutablePointer<Float>?) -> Bool

Scans for a double value from a hexadecimal representation, returning a found value by reference.

func scanHexInt32(UnsafeMutablePointer<UInt32>?) -> Bool

Scans for an unsigned value from a hexadecimal representation, returning a found value by reference.

func scanHexInt64(UnsafeMutablePointer<UInt64>?) -> Bool

Scans for a long long value from a hexadecimal representation, returning a found value by reference.

func scanInt(UnsafeMutablePointer<Int>?) -> Bool

Scans for an NSInteger value from a decimal representation, returning a found value by reference

func scanInt32(UnsafeMutablePointer<Int32>?) -> Bool

Scans for an int value from a decimal representation, returning a found value by reference.

func scanInt64(UnsafeMutablePointer<Int64>?) -> Bool

Scans for a long long value from a decimal representation, returning a found value by reference.

func scanUnsignedLongLong(UnsafeMutablePointer<UInt64>?) -> Bool

Scans for an unsigned long long value from a decimal representation, returning a found value by reference.

Monitoring Scanner Progress

var isAtEnd: Bool

Flag that indicates whether the receiver has exhausted all significant characters.

Relationships

Inherits From

See Also

Pattern Matching

class NSRegularExpression

An immutable representation of a compiled regular expression that you apply to Unicode strings.

class NSDataDetector

A specialized regular expression object that matches natural language text for predefined data patterns.

class NSTextCheckingResult

An occurrence of textual content found during the analysis of a block of text, such as when matching a regular expression.

let NSNotFound: Int

A value indicating that a requested item couldn’t be found or doesn’t exist.