Swift:将索引转换为int [duplicate]

时间:2021-09-04 02:57:40

This question already has an answer here:

这个问题已经有了答案:

I am new to swift development. Actually it is a simple problem but I cannot figure it out. I have a String variable "name1" where I want to determine the position in the alphabet for each character and sum these positions. Therefore I loop through the string and use indexOf() for every character

我对快速发展很陌生。其实这是一个很简单的问题,但是我没有办法解决。我有一个字符串变量“name1”,在这里我想确定每个字符在字母表中的位置并将这些位置相加。因此,我对字符串进行循环,并对每个字符使用indexOf()

let name1="myname"
let sumPositions=0

for index in 0...name1.characters.count-1{
sumPositions+="ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.indexOf(name1.startIndex+index);
}

I get the error "Binary operator "+" cannot be applied to operands of type 'index' aka 'String.characterView.index' and 'Int'. This means I have to cast 'index' to 'Int' but I did not find a solution for this.

我得到错误"二进制运算符"+"不能应用到'index' aka 'String.characterView类型的操作数。指数”和“Int”。这意味着我必须将'index'转换为'Int',但我没有找到解决方案。

2 个解决方案

#1


1  

Leo Dabus has a nice extension for this in this post

Leo Dabus在这篇文章中有一个很好的扩展

extension String {
    var letterValue: Int {
        return Array("abcdefghijklmnopqrstuvwxyz".characters).indexOf(Character(lowercaseString)) ?? 0
    }

    var wordValue: Int {
        // I prefer to use reduce
        return characters.reduce(0) { $0 + String($1).letterValue }
    }
}

let name1 = "myname"
print(name1.wordValue) // 65

#2


1  

It can't work like that. My suggestion is that you create an array of letters from the alphabet, an array of letters from the name, then you use indexOf to find the letter index, you append all indices in an array then you sum the contents of the array:

它不能像那样工作。我的建议是从字母表中创建一个字母数组,从名字中创建一个字母数组,然后使用indexOf找到字母索引,将所有的索引添加到数组中,然后对数组的内容进行求和:

let alphabetArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".lowercaseString.characters.map { String($0) } // ["a", "b", "c", "d", "e", "f", "g", ...]

let name1 = "myname"
let nameArray = name1.lowercaseString.characters.map { String($0) } // ["m", "y", "n", "a", "m", "e"]

var positions = [Int]()

for nameLetter in nameArray {
    if let index = alphabetArray.indexOf(nameLetter) {
        positions.append(index)
    }
}

print(positions) // [12, 24, 13, 0, 12, 4]

let sum = positions.reduce(0, combine: +) // 65

#1


1  

Leo Dabus has a nice extension for this in this post

Leo Dabus在这篇文章中有一个很好的扩展

extension String {
    var letterValue: Int {
        return Array("abcdefghijklmnopqrstuvwxyz".characters).indexOf(Character(lowercaseString)) ?? 0
    }

    var wordValue: Int {
        // I prefer to use reduce
        return characters.reduce(0) { $0 + String($1).letterValue }
    }
}

let name1 = "myname"
print(name1.wordValue) // 65

#2


1  

It can't work like that. My suggestion is that you create an array of letters from the alphabet, an array of letters from the name, then you use indexOf to find the letter index, you append all indices in an array then you sum the contents of the array:

它不能像那样工作。我的建议是从字母表中创建一个字母数组,从名字中创建一个字母数组,然后使用indexOf找到字母索引,将所有的索引添加到数组中,然后对数组的内容进行求和:

let alphabetArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".lowercaseString.characters.map { String($0) } // ["a", "b", "c", "d", "e", "f", "g", ...]

let name1 = "myname"
let nameArray = name1.lowercaseString.characters.map { String($0) } // ["m", "y", "n", "a", "m", "e"]

var positions = [Int]()

for nameLetter in nameArray {
    if let index = alphabetArray.indexOf(nameLetter) {
        positions.append(index)
    }
}

print(positions) // [12, 24, 13, 0, 12, 4]

let sum = positions.reduce(0, combine: +) // 65