简体   繁体   English

尝试过滤数组数据时,无法将“String”类型的值转换为预期的参数类型“String.Element”(又名“Character”)

[英]Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character') when trying to filter array data

I'm trying to "search" item from an array and if the item contains searchkey, it should appended the data to filteredArray.我正在尝试从数组中“搜索”项目,如果该项目包含搜索键,则应将数据附加到filteredArray。 But when i'm trying to check if the data contains search key it showed an error "Cannot convert value of type 'String' to expected argument type 'String.Element' (aka 'Character')"但是当我试图检查数据是否包含搜索键时,它显示了一个错误“无法将类型'字符串'的值转换为预期的参数类型'String.Element'(又名'字符')”

Example: let data: [String] = ["Apples", "Apricots", "Bananas", "Cherries", "Grapefruit", "Grapes"] searchKey: "ap" should filter "Apples" and "Appricots".示例:let data: [String] = ["Apples", "Apricots", "Bananas", "Cherries", "Grapefruit", "Grapes"] searchKey: "ap" 应该过滤“Apples”和“Appricots”。 searcKey: "gr" should filter "Grapefruit" and "Grapes". searcKey: "gr" 应该过滤 "Grapefruit" 和 "Grapes"。

My code:我的代码:

let data: [String] = ["Apples", "Apricots", "Bananas", "Cherries", "Grapefruit", "Grapes"]
var filteredData: [String] = []

let searchKey: String = "Ap"

for item in data {
  if item.contains(searchKey) {
    filteredData.append(item)
  }
}

Swift String contains(_:) accepts Character as parameter, instead of String : Swift String contains(_:)接受Character作为参数,而不是String

func contains(_ element: Character) -> Bool

You can use Generic Instance Method contains(_:) from Foundation :您可以使用来自Foundation通用实例方法contains(_:)

func contains<T>(_ other: T) -> Bool where T : StringProtocol

Just by importing Foundation and it should work.只需导入Foundation就可以了。

Seems your code works properly.似乎您的代码工作正常。 You may need to import Foundation.您可能需要导入 Foundation。 Also, there is a shorter way to filter your data:此外,还有一种更短的方法来过滤您的数据:

let data: [String] = ["Apples", "Apricots", "Bananas", "Cherries", "Grapefruit", "Grapes"]
var filteredData: [String] = []
filteredData = data.filter({ $0.contains("Ap") })

暂无
暂无

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

相关问题 尝试在 swift 应用程序的 Firestore 中添加数组元素时出现“无法将 'String' 类型的值转换为预期的参数类型 '[Any]'”错误 - "Cannot convert value of type 'String' to expected argument type '[Any]'" error when trying to add array element in firestore in swift app 无法转换类型[[String:AnyObject] ?. Type”(也称为“可选” <Dictionary<String, AnyObject> &gt; .Type)到期望的参数类型 - Cannot convert value of type '[String : AnyObject]?.Type' (aka 'Optional<Dictionary<String, AnyObject>>.Type) to expected argument type 尝试保存数组时CoreData出错。 &#39;无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;NSManagedObject&#39;&#39; - Error in CoreData when trying to save an array. 'Cannot convert value of type 'String' to expected argument type 'NSManagedObject'' 无法转换&#39;Range类型的值 <String.Index> &#39;(又名&#39;范围 <String.CharacterView.Index> &#39;)预期参数类型&#39;NSRange&#39;(又名&#39;_NSRange&#39;) - Cannot convert value of type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>') to expected argument type 'NSRange' (aka '_NSRange') 无法转换“字符串”类型的值? 到预期的参数类型“CLLocationDegrees”(又名“Double”) - Cannot convert value of type 'String?' to expected argument type 'CLLocationDegrees' (aka 'Double') 使用函数组合时的编译器错误:无法将类型`([[Character])-&gt; String`的值转换为期望的&gt;参数类型`_-&gt; _` - Compiler error when using function composition: Cannot convert value of type `([Character]) -> String` to expected > argument type `_ -> _` 尝试保存数组时出现错误-无法将类型[Data]的值转换为预期的参数类型&#39;[Dictionary <String, AnyObject> ]” - Trying to saveArray, got error - Cannot convert value of type '[Data]' to expected argument type '[Dictionary<String, AnyObject>]' 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 无法将类型(“字符串:字符串”)的值转换为预期的参数类型“ URL” - Cannot convert value of type ('string: String)' to expected argument type 'URL' 无法将类型“ [String:Any]”的值转换为预期的参数类型“ String” - Cannot convert value of type '[String : Any]' to expected argument type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM