快速错误:无法将“字符”类型的值转换为预期的参数类型“ Unicode.Scalar”

布莱恩·霍格

我是Swift的新手,下面的功能对此有问题。我正在使用SWIFT 5 / Xcode 11.3。该功能旨在删除元音之前的所有字母。例如,“ Brian”将返回“ ian”,“ Bill”将返回“ ill”,等等。我在下面的2行中得到了错误。

import Foundation

func shortNameFromName(_ name: String) -> String {

    // Definition of characters
    let vowels = CharacterSet(charactersIn: "aeiou")
    var shortName = ""
    let start = name.startIndex

    // Loop through each character in name
    for number in 0..<name.count {

        // If the character is a vowel, remove all characters before current index position
        if vowels.contains(name[name.index(start, offsetBy: number)]) == true { //**ERROR: Cannot convert value of type 'Character' to expected argument type 'Unicode.Scalar'**
            var shortName = name.remove(at: shortName.index(before: shortName.number)) //**ERROR: Cannot use mutating member on immutable value: 'name' is a 'let' constant**
        }
    }

    //convert returned value to lowercase
    return name.lowercased()
}

var str = "Brian" // Expected result is "ian"

shortNameFromName(str)
里奥·达布斯

func drop(while predicate: (Character) throws -> Bool) rethrows -> Substring当字符串“ aeiou”不包含字符时,可以使用collection的方法并返回子字符串:

func shortName(from name: String) -> String { name.drop{ !"aeiou".contains($0) }.lowercased() }

shortName(from: "Brian")  // "ian"    
shortName(from: "Bill")   // "ill"

关于代码中的问题,请通过以下代码检查注释:

func shortName(from name: String) -> String {
    // you can use a string instead of a CharacterSet to fix your first error
    let vowels =  "aeiou"
    // to fix your second error you can create a variable from your first parameter name
    var name = name
    // you can iterate through each character using `for character in name`
    for character in name {
        // check if the string with the vowels contain the current character
        if vowels.contains(character) {
            // and remove the first character from your name using `removeFirst` method
            name.removeFirst()
        }
    }
    // return the resulting name lowercased
    return name.lowercased()
} 

shortName(from: "Brian")  // "ian"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Swift错误:无法将“ ArraySlice”类型的值转换为预期的参数类型

无法将类型的值转换为预期的参数类型NSURLSessionDelegate

无法将类型'()'的值转换为预期的参数类型'()->()'

快速关闭:无法将类型'(_)-> Bool'的值转换为预期的参数类型

Xcode 8.1 Swift 3错误:无法将类型“字符串”的值转换为预期的参数类型“ UnsafeMutableRawPointer”

无法将类型“ [T]”的值转换为预期的参数类型“ [_]”

Swift泛型错误:无法将类型'Type <T>'的值转换为预期的参数类型'Type <_>'

错误:无法将类型“ URL”的值转换为预期的参数类型“字符串”

无法将“字符串”类型的值转换为预期的参数类型“布尔”

无法将类型“()”的值转换为预期的参数类型“字符串”

无法将类型“ A <T>”的值转换为预期的参数类型“ A <_>”

无法将类型的值转换为预期的参数类型“布尔”

无法将类型'(_)->()'的值转换为预期的参数类型'(()-> Void)?

无法将“字符串”类型的值转换为预期的参数类型“ _?”

无法将“字符串”类型的值转换为预期的参数类型“绑定<字符串>”

Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode

将unicode转换为字符

无法将类型“ Int”的值转换为预期的参数类型“ _?”

SWIFT错误“无法将类型“ UIImageView”的值转换为预期的参数类型“ UIImage”

错误“无法将类型为“ int”的值转换为预期的参数类型为“ UInt”

无法将类型[()]的值转换为预期的参数类型()

无法将字符串类型的值转换为预期的参数类型NSManagedObject

无法将字符串类型的值转换为预期的参数类型 Int

Swift 将“字符”转换为“Unicode.Scalar”

无法将“()”类型的值转换为预期的参数类型“(() -> Void)?”

如何快速将 Range<Unicode.Scalar> 的赋值运算符重载为 String

无法将 '(ViewController) -> () -> ()' 类型的值转换为预期的参数类型 '() -> ()'

无法将“字符串”类型的值转换为预期的参数类型“类别”

无法将“Void”类型的值转换为预期的参数类型“(字符串)-> Void”