如果元素符合给定协议,则扩展数组以符合协议
Extend Array to conform to protocol if Element conforms to given protocol
我想做这样的事情,但无法获得正确的语法,也无法在网络上找到提供正确编写方法的任何地方:
protocol JSONDecodeable {
static func withJSON(json: NSDictionary) -> Self?
}
protocol JSONCollectionElement: JSONDecodeable {
static var key: String { get }
}
extension Array: JSONDecodeable where Element: JSONCollectionElement {
static func withJSON(json: NSDictionary) -> Array? {
var array: [Element]?
if let elementJSON = json[Element.key] as? [NSDictionary] {
array = [Element]()
for dict in elementJSON {
if let element = Element.withJSON(dict) {
array?.append(element)
}
}
}
return array
}
}
所以我想 Array
仅当此数组的元素符合 JSONCollectionElement
.
时才符合我的协议 JSONDecodeable
这可能吗?如果是这样,语法是什么?
这在 Swift 中还不可能。您可以在标准库中看到同样的事情发生:Array
在使用 Equatable
元素声明时不会获得 Equatable
一致性。
我建议使用包装器。例如
struct ArrayContainer<T: Decodable>: Container {
let array: [T]
}
Swift 4.2
在 Swift 4.2 中,我能够使用符合如下协议的元素扩展数组:
public extension Array where Element: CustomStringConvertible{
public var customDescription: String{
var description = ""
for element in self{
description += element.description + "\n"
}
return description
}
}
我不知道这是否是最好的方法,或者苹果是否打算以这种方式使用它。我用过一次,对我来说效果很好:
假设您有以下协议
protocol MyProtocol {
var test: Bool { get }
}
您可以对数组执行此操作
extension Array: MyProtocol where Element: MyProtocol {
var test: Bool {
return self.allSatisfy({ [=11=].test })
}
}
这是字典
extension Dictionary: MyProtocol where Value: MyProtocol {
var test: Bool {
return self.values.allSatisfy({ [=12=].test })
}
}
我想做这样的事情,但无法获得正确的语法,也无法在网络上找到提供正确编写方法的任何地方:
protocol JSONDecodeable {
static func withJSON(json: NSDictionary) -> Self?
}
protocol JSONCollectionElement: JSONDecodeable {
static var key: String { get }
}
extension Array: JSONDecodeable where Element: JSONCollectionElement {
static func withJSON(json: NSDictionary) -> Array? {
var array: [Element]?
if let elementJSON = json[Element.key] as? [NSDictionary] {
array = [Element]()
for dict in elementJSON {
if let element = Element.withJSON(dict) {
array?.append(element)
}
}
}
return array
}
}
所以我想 Array
仅当此数组的元素符合 JSONCollectionElement
.
JSONDecodeable
这可能吗?如果是这样,语法是什么?
这在 Swift 中还不可能。您可以在标准库中看到同样的事情发生:Array
在使用 Equatable
元素声明时不会获得 Equatable
一致性。
我建议使用包装器。例如
struct ArrayContainer<T: Decodable>: Container {
let array: [T]
}
Swift 4.2
在 Swift 4.2 中,我能够使用符合如下协议的元素扩展数组:
public extension Array where Element: CustomStringConvertible{
public var customDescription: String{
var description = ""
for element in self{
description += element.description + "\n"
}
return description
}
}
我不知道这是否是最好的方法,或者苹果是否打算以这种方式使用它。我用过一次,对我来说效果很好:
假设您有以下协议
protocol MyProtocol {
var test: Bool { get }
}
您可以对数组执行此操作
extension Array: MyProtocol where Element: MyProtocol {
var test: Bool {
return self.allSatisfy({ [=11=].test })
}
}
这是字典
extension Dictionary: MyProtocol where Value: MyProtocol {
var test: Bool {
return self.values.allSatisfy({ [=12=].test })
}
}