简体   繁体   English

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

[英]Swift error: Cannot convert value of type 'Character' to expected argument type 'Unicode.Scalar'

I'm new to Swift and having problems with this function below.我是 Swift 的新手,并且在使用下面的这个函数时遇到了问题。 I'm Using SWIFT 5/Xcode 11.3.我正在使用 SWIFT 5/Xcode 11.3。 The function is designed to remove any letters prior to a vowel.该函数旨在删除元音之前的任何字母。 For instance, "Brian" would return "ian", "Bill" would return "ill", etc. I'm getting the errors on the 2 lines below.例如,“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)

You can use collection's method func drop(while predicate: (Character) throws -> Bool) rethrows -> Substring while the string "aeiou" does not contain character and return a Substring:您可以使用集合的方法func drop(while predicate: (Character) throws -> Bool) rethrows -> Substring而字符串“aeiou”不包含字符并返回一个子字符串:

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

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

Regarding the issues in your code check the comments through the code bellow:关于代码中的问题,请通过下面的代码检查注释:

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 将“字符”转换为“Unicode.Scalar” - Swift convert 'Character' to 'Unicode.Scalar' 错误:无法将“字符串”类型的值转换为预期的参数类型“字符” - error: cannot convert value of type 'String' to expected argument type 'Character' Swift 错误无法转换“[String.Element]”类型的值(又名“Array<character> ') 到预期的参数类型 '[String]'</character> - Swift Error Cannot convert value of type '[String.Element]' (aka 'Array<Character>') to expected argument type '[String]' Swift错误:无法将“ArraySlice”类型的值转换为预期的参数类型 - Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type Swift泛型错误:无法转换“类型”类型的值 <T> &#39;到预期的参数类型&#39;类型&lt;_&gt;&#39; - Swift generics error: Cannot convert value of type 'Type<T>' to expected argument type 'Type<_>' Swift 4:无法将 type() 的值转换为预期的参数类型 - Swift 4: Cannot convert value of type() to expected argument type SWIFT 4-无法转换&#39;UITextField!&#39;类型的值 到预期的参数类型“ Double” - SWIFT 4 - Cannot convert value of type 'UITextField!' to expected argument type 'Double' Swift闭包:无法将类型&#39;(_) - &gt; Bool&#39;的值转换为预期的参数类型 - Swift closure: Cannot convert value of type '(_) -> Bool' to expected argument type Swift MessageKit-无法转换类型&#39;_?&#39;的值 预期的参数类型“ URL?” - Swift MessageKit - Cannot Convert value of type '_?' to expected argument type 'URL?' 迅速-无法将类型&#39;[Int]&#39;的值转换为预期的参数类型&#39;[_]&#39; - swift- Cannot convert value of type '[Int]' to expected argument type '[_]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM