1

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.

Tarang Hirani
  • 560
  • 1
  • 12
  • 43

1 Answers1

1

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.)

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • So is it comparing the two values? So after creating an instance of `Scanner` from `cleanHexString`, what exactly does `scanHexInt32` do? – Tarang Hirani Nov 08 '16 at 20:01
  • As I said: It scans the attached string for a 32-bit integer value and returns the result in `rgbValue`. Nothing is compared. What for? – Amin Negm-Awad Nov 09 '16 at 08:19
  • So it scans `cleanHexStr` for a 32-bit integer? For example, if i input the string `ffffff`, the value of `rbgValue` is **16777215** according to Xcode. I am not sure what that value means. Could you elaborate? – Tarang Hirani Nov 09 '16 at 16:49
  • 1
    It is the same value. 16777215 (or whatever) is the representation of the value in decimal notation, ffffff is the representation of the value in hexadecimal notation. https://en.wikipedia.org/wiki/Hexadecimal – Amin Negm-Awad Nov 10 '16 at 02:47