This question already has an answer here:
这个问题已经有了答案:
- How do I cycle through the entire alphabet with Swift while assigning values? 5 answers
- 我如何在赋值的同时快速遍历整个字母表?5个回答
- Apply a number to each letter in text swift2 1 answer
- 在文本swift2 1中为每个字母加一个数字
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