Convert Character to Int in Swift

我的未来我决定 提交于 2019-12-12 11:21:16

问题


I'm working on a project which includes verifying the checksum of an Int input with the Damm Algorithm. I've managed to create a the operational table and my method for accessing the value in the table involves passing an interim value and a digit to pass in as the column value. ex.

     self.tableToUse[interim,checkSumArray[i]]

Unfortunately, I've run into a snag when I'm trying to pass the digits from my input into the the get/set method where I cannot find a way to convert the Characters into Ints.

    func encode(number: Int) -> Int{
    var checkSumArray = [Int]()
    if number > 99999999 {
        println("number is too large")
        return 0
    }
    else if number < 0 {
        println("invalid input")
        return 0
    }
    else {
        checkSumArray.append(number%(10))
        checkSumArray.append((number%(100)-checkSumArray[0])/10)
        checkSumArray.append((number%(1000)-checkSumArray[1])/100)
        checkSumArray.append((number%(10000)-checkSumArray[2])/1000)
        checkSumArray.append((number%(100000)-checkSumArray[3])/10000)
        checkSumArray.append((number%(1000000)-checkSumArray[4])/100000)
        checkSumArray.append((number%(10000000)-checkSumArray[5])/1000000)
        checkSumArray.append((number%(100000000)-checkSumArray[6])/10000000)
        checkSumArray = checkSumArray.reverse()

        var interim: Int = 0

        for i in 0..<checkSumArray.count{
            interim = self.tableToUse[interim,checkSumArray[i]]
        }
        return interim
    }
}

As you can see, I've had to resort to a really nasty way of dealing with this. It works, but it's very limited, inefficient, and just ugly to look at or maintain. I've looked at the option of using Characters instead of Ints in the Damm Table I've constructed and altering the get/set method to deal with those instead, but that's a lot of extra work and could introduce other issues. Any suggestions of alternative ways to handle this, or a way to convert Characters to Ints would be appreciated.

Thanks!


回答1:


There is no need to work with characters, but your code to create an array with the decimal digits of the input number can be greatly simplified:

var checkSumArray = [Int]()
var tmp = number
while tmp > 0 {
    checkSumArray.append(tmp % 10)
    tmp /= 10
}
checkSumArray = checkSumArray.reverse()  



回答2:


Based on How convert a *positive* number into an array of digits in Swift, you could do (the number has to be positive):

let checkSumArray = map(number.description) { String($0).toInt()! }



回答3:


if let int = Int(String(Character("1"))) {
    print(int)
}

You can also create a character extension as follow:

extension Character {
    var integerValue: Int? {
        return Int(String(self)) 
    }
}

Testing

Character("1").integerValue  // 1
Character("2").integerValue  // 2
Character("3").integerValue  // 3
Character("4").integerValue  // 4
Character("5").integerValue  // 5
Character("6").integerValue  // 6
Character("7").integerValue  // 7
Character("8").integerValue  // 8
Character("9").integerValue  // 9
Character("0").integerValue  // 0
Character("a").integerValue  // nil

Array("9876").first!.integerValue  // 9
Array("9876")[1].integerValue      // 8
Array("9876")[2].integerValue      // 7
Array("9876").last!.integerValue   // 6

edit/update Swift 5

Swift 5 adds many new properties to the Character and one of them fits exactly to this purpose. It is called wholeNumberValue

Character("1").wholeNumberValue  // 1
Character("2").wholeNumberValue  // 2
Character("3").wholeNumberValue  // 3
Character("4").wholeNumberValue  // 4
Character("④").wholeNumberValue  // 4
Character("5").wholeNumberValue  // 5
Character("6").wholeNumberValue  // 6
Character("7").wholeNumberValue  // 7
Character("8").wholeNumberValue  // 8
Character("9").wholeNumberValue  // 9
Character("0").wholeNumberValue  // 0
Character("万").wholeNumberValue  // 10_000
Character("a").wholeNumberValue  // nil


来源:https://stackoverflow.com/questions/28822429/convert-character-to-int-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!