'How to count number of leading spaces in a string in Swift? [closed]

I want to know how to count the number of leading spaces in a string in Swift.

Say, if the string is " Hello world ", the result should be 2

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source