辅音和元音斯威夫特
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了辅音和元音斯威夫特相关的知识,希望对你有一定的参考价值。
我是Swift的新手,有人可以向我解释为什么我会继续遇到这个问题。我正在使用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)
}
答案
如果你使用的是swift 2:
只是改变这一行
for character in string {
至
for character in string.characters {
它会工作得很好
count("hello") // (.0 2, .1 3)
另一答案
另一种选择是使用正则表达式来检索每个单词的第一个字母。 选项用于查找单词边界,
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)
}
另一答案
问题在于您为方法选择的名称。尝试除了伯爵以外的任何名字,你应该没事。
编辑/更新: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文件中。
以上是关于辅音和元音斯威夫特的主要内容,如果未能解决你的问题,请参考以下文章