我是斯威夫特的新人,任何人都可以向我解释为什么我会继续遇到这个问题。我使用Xcode 6.4,但这是我的问题,我希望我清理它但我需要我的函数接受大字符串然后返回元组(numVowels,numConsonants) 计算以辅音/元音开头的单词数 返回元组并打印函数调用的结果。 我不需要它来计算字符数,只需要每个单词的第一个字符。我创建了一个for循环,将所有内容切换为小写。但现在我被卡住了。
func count(string: String) -> (Vowels:Int, Consonants:Int) {
var Vowels = 0, Consonants = 0
for character in string {
switch String(character).lowercaseString {
case "a","e","i","o","u":
++Vowels
case "b","c","d","e","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
++Consonants
default: break
}
}
return (Vowels, Consonants)
}
答案 0 :(得分:1)
如果您使用的是swift 2:
只需更改此行
for character in string {
到
for character in string.characters {
它可以正常工作
count("hello") // (.0 2, .1 3)
答案 1 :(得分:0)
问题在于您为方法选择的名称。尝试除计数以外的任何名称,你应该没事。
编辑/更新: Swift 4
func countCharacteres(_ string: String) -> (vowels: Int, consonants: Int) {
var vowels = 0, consonants = 0
for character in string {
switch String(character).lowercased() {
case "a","e","i","o","u":
vowels += 1
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
consonants += 1
default:
break
}
}
return (vowels, consonants)
}
let name = "Dromel"
countCharacteres(name).vowels // 2
countCharacteres(name).consonants // 4
如果你只想计算每个单词的第一个字母,你需要将你的字符串分成一个单词数组并提取每个首字母的小写如下:
extension StringProtocol where Index == String.Index {
var range: Range<Index> { return startIndex..<endIndex }
var words: [String] {
var words: [String] = []
enumerateSubstrings(in: range, options: .byWords) { word, _, _, _ in words.append(word!) }
return words
}
}
extension StringProtocol where Index == String.Index {
var firstCharacterCount: (vowels: Int, consonants: Int) {
var vowels = 0, consonants = 0
for first in words.compactMap({$0.lowercased().first}) {
switch first {
case "a","e","i","o","u":
vowels += 1
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
consonants += 1
default: break
}
}
return (vowels, consonants)
}
}
let sentence = "Hello World. Apple."
let counter = sentence.firstCharacterCount
counter.vowels // 1
counter.consonants // 2
请注意,扩展名应放在项目内的新Swift文件中。
答案 2 :(得分:0)
另一种选择是使用正则表达式来检索每个单词的第一个字母。 \b
选项用于查找字边界,\w
是单字字符。因此,一旦你逃避反斜杠,\\b\\w
将捕获紧跟在单词边界之后的第一个字母(a.k.a。,每个单词的第一个字母):
func countLetters(string: String) -> (vowels: Int, consonants: Int) {
var vowels = 0, consonants = 0
let regex = NSRegularExpression(pattern: "\\b\\w", options: nil, error: nil)
regex?.enumerateMatchesInString(string, options: nil, range: NSMakeRange(0, count(string))) { matches, flags, stop in
switch (string as NSString).substringWithRange(matches.range).lowercaseString {
case "a","e","i","o","u":
++vowels
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
++consonants
default: break
}
}
return (vowels, consonants)
}
-
如果你想处理重音字符(例如“étude”),你可能想先删除那些变音符号:
func countLetters(string: String) -> (vowels: Int, consonants: Int) {
let convertedString = NSMutableString(string: string)
CFStringTransform(convertedString, nil, kCFStringTransformStripCombiningMarks, Boolean(0))
var vowels = 0, consonants = 0
let regex = NSRegularExpression(pattern: "\\b\\w", options: nil, error: nil)
regex?.enumerateMatchesInString(convertedString as String, options: nil, range: NSMakeRange(0, convertedString.length)) { matches, flags, stop in
switch convertedString.substringWithRange(matches.range).lowercaseString {
case "a","e","i","o","u":
++vowels
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
++consonants
default: break
}
}
return (vowels, consonants)
}