gpt4 book ai didi

Swift 从泛型到 CustomStringConvertible

转载 作者:可可西里 更新时间:2023-11-01 02:18:55 26 4
gpt4 key购买 nike

我整理了以下类(class)(在 Swift variable comparison where type is not known 中其他人的帮助下)。

我想要实现的是将一个闭包传递给 SearchOption,当调用 getSelectedItemAsString 时,它将运行将 searchOption 作为参数传递的闭包并返回结果。

但是因为 selectedOption 是一个泛型类型,它提示 Cannot convert value of type '[T]' to expected argument type '[CustomStringConvertible]' in getSelectedItemAsString ()

public func getSelectedItemAsString() -> String {
if self.searchOptionToString != nil && self.selectedOption != nil {
return self.searchOptionToString!(selectedOption!)
}
return ""
}

我不确定在这里做什么。将 searchOption 转换为字符串的实现对于每个 SearchOption 都是不同的,这就是我需要闭包的原因。

全类

public typealias searchOptionToCloudSearchQuery = ([CustomStringConvertible]) -> String

public class SearchOption<T: Equatable> {

private var title: String
private var allowAny: Bool
private var allowMultiple: Bool
private var dependencies: [SearchOption]?

private var selectedOption: [T]?

private var searchOptionToString: searchOptionToCloudSearchQuery?

init(title: String, allowAny: Bool, allowMultiple: Bool, dependencies: [SearchOption]?) {
self.title = title
self.allowAny = allowAny
self.allowMultiple = allowMultiple
self.dependencies = dependencies
}

public func setSelectedItem(selectedOption: T) -> Void {
if self.selectedOption == nil || !self.allowMultiple{
self.selectedOption = [T]()
}
self.selectedOption?.append(selectedOption)
}

public func getSelectedItem() -> [T]? {
return self.selectedOption
}

public func setSearchOptionToCloudSearchQueryClosure(closure: searchOptionToCloudSearchQuery) -> Void {
self.searchOptionToString = closure
}

public func getSelectedItemAsString() -> String {
if self.searchOptionToString != nil && self.selectedOption != nil {
return self.searchOptionToString!(selectedOption!)
}
return ""
}

}

实现

var make: SearchOption = SearchOption<String>(title: "Make", allowAny: true, allowMultiple: true, dependencies: nil)

make.setSelectedItem("Vauxhall")

var closure: searchOptionToCloudSearchQuery = {(selectedOptions) in

var stringBuilder = ""
for item in selectedOptions {
if item is String {
stringBuilder += item as! String
}
}
return stringBuilder

}

make.setSearchOptionToCloudSearchQueryClosure(closure)
make.getSelectedItemAsString()

错误

Playground execution failed: /var/folders/b5/3jvkp2jd5q5fmps2hrw91781cmf7f_/T/./lldb/570/playground442.swift:38:61: error: cannot convert value of type '[T]' to expected argument type '[CustomStringConvertible]'
return self.searchOptionToString!(selectedOption!)
~~~~~~~~~~~~~~^

最佳答案

我已经设法解决了我的问题。

我没有在实现中使用类型别名,而是在定义闭包的同时定义了类型:

var closure: ([String]) -> String = {(selectedOptions) in

var stringBuilder = ""
for item in selectedOptions {
stringBuilder += item
}
return stringBuilder

}

然后将类型别名移动到具有泛型参数的类本身,这确保了闭包类型和 searchOption 类型匹配:

public class SearchOption<T: Equatable> {

public typealias searchOptionToCloudSearchQuery = ([T]) -> String

/.../

public func getSelectedItemAsString() -> String {
if self.searchOptionToString != nil && self.selectedOption != nil {
return self.searchOptionToString!(selectedOption!)
}
return ""
}

}

关于Swift 从泛型到 CustomStringConvertible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com