iosswifthexnsscanner

Confusion regarding NSScanner (Scanner)


I wrote an extension to return a UIColor from a hexadecimal string. Although it works, I don't quite understand the purpose of this piece of code

var rgbValue: UInt32 = 0
Scanner(string: cleanHexStr).scanHexInt32(&rgbValue)

Could you provide some insight/detailed understanding for this? Thank you.


Solution

  • The first part creates a instance of NSScanner for the string cleanHexString. (Scanners are "attached" to a string.)

    Then scanHexInt32() is executed on this scanner to get the integer value of the string representation. rgbValue is an out-argument. (The pointer to it is passed, what is the technique for out-arguments in C. NSScanner is an Objective-C class.)