It is up to you to choose the right tool for your string. We learn different ways to do a string to integer conversion. There are more styles and properties that you can override to suit your need. If you change numberStyle, the above string would no longer valid and produce nil. decimal format, which will take decimal separators and thousand separators into consideration. ![]() If you know that your string will be a decimal format integer, you can create NumberFormatter like this to do the parsing. intValueĪpart from this whitespace rule, everything is depending on NumberFormatter properties and numberStyle. ![]() For example, the strings “ 123”, “123 ”, and “ 123 ” all produce the number 123. Any leading or trailing space separator characters in a string are ignored. The only shared valid characteristic regardless of number format is whitespace insensitive. It supports a wide range of number formatted strings based on locale-appropriate group or decimal separators, which you can set. NumberFormatter is far more sophisticated than the former solution. Here is an exmaple how we convert a string to integer with NumberFormatter. func number (from string : String ) -> NSNumber ? Then we call intValue on NSNumber instance to get back an integer. To use it, we initialize NumberFormatter, then passing a string to the number(from:) method, which will return NSNumber?. But if you have a string that represents curtain format of integer, you should use NumberFormatter. You can only convert only from a string that exactly represent an integer. Convert with NumberFormatterĪs you can see, the integer initializer is quite simple. Sponsor and reach thousands of iOS developers. You can easily support by checking out this sponsor. In the following example, we trimming out whitesapces character from a string before we do a conversion. You can mitigate the problem in some cases, like a string with whitespace, by clean it up before the conversion. ![]() You might want to assign a default value or wrap it in if-let to ensure the presence of value. Since the initializer is failable initialzier, all of the above would produce nil as a result. Int ( "1,234" ) // Characters out of bounds Int ( "123untitled" ) // Characters out of bounds Int ( "untitled123" ) // Characters out of bounds Int ( "ff6600" ) // Characters out of bounds It is everything that is not valid, but I will give you some cases that you might encounter. ![]() It only supports strings that begin with a plus or minus sign character ( + or -), followed by one or more numeric digits ( 0-9). let x = Int ( "123" )īefore you go with these methods, you should know which kind of strings that Int can convert to integer. To convert a string to integer, we initialize an Int with desired string. Int has an initializer that creates a new integer value from the given string. There are many ways to convert a string of integers in Swift. It happens most of the time when you try to consume data from an external source like a website or API where you have no control over them. There will be a time when you need to convert a string of integers ( "123") back to an integer ( 123).
0 Comments
Leave a Reply. |