Home > OS >  How to convert 'String.Element' to 'Int'?
How to convert 'String.Element' to 'Int'?

Time:10-12

var numsInStr = "1abc2x30yz67"
var sum = 0
for i in numsInStr {
    if i.isNumber == true {
       sum  = i
    }
}
print(sum)
  • Problem is in if statemt to summing numbers. And it returns - "Cannot convert value of type 'String.Element' (aka 'Character') to expected argument type 'Int'"
  • It is possible to solve this problem like mine. I saw some answers but answers are very short and misunderstanding

Input: 1abc2x30yz67 Output: 100

CodePudding user response:

your solution is not working because you are adding character type in to integrate type value, first you need to convert your character object into string then convert into integer for sum. hope this may helps you thanks

  • Related