在 class 扩展中包装通用方法
Wrapping a generic method in a class extension
我正在尝试将通用 Array 方法 compactMap
包装在 Array 扩展中,以提供更多 meaning/readability 方法的用途。我只是想获取一个 Optionals 数组并从中删除任何和所有 nil
值。
extension Array {
public func removeNilElements() -> [Element] {
let noNils = self.compactMap { [=11=] }
return noNils // nil values still exist
}
}
我遇到的问题是 compactMap
这里不工作。 nil
值仍在结果数组 noNils
中。当我直接使用 compactMap
方法而不使用此包装器时,我得到了没有 nil
值的数组的期望结果。
let buttons = [actionMenuButton, createButton] // [UIBarButtonItem?]
let nonNilButtons = buttons.compactMap { [=12=] } // works correctly
let nonNilButtons2 = buttons.removeNilElements() // not working
我没有正确设计我的扩展方法吗?
您必须为 optional 元素数组定义方法,并将 return 类型定义为相应的非可选数组。这可以通过通用函数完成:
extension Array {
public func removingNilElements<T>() -> [T] where Element == T? {
let noNils = self.compactMap { [=10=] }
return noNils
}
}
示例:
let a = [1, 2, nil, 3, nil, 4] // The type of a is [Int?]
let b = a.removingNilElements() // The type of b is [Int]
print(b) // [1, 2, 3, 4]
在您的代码中,[=12=]
具有(非可选)类型 Element
,它只是被编译器包装成一个可选的,以便匹配 [=14= 的参数类型].
我正在尝试将通用 Array 方法 compactMap
包装在 Array 扩展中,以提供更多 meaning/readability 方法的用途。我只是想获取一个 Optionals 数组并从中删除任何和所有 nil
值。
extension Array {
public func removeNilElements() -> [Element] {
let noNils = self.compactMap { [=11=] }
return noNils // nil values still exist
}
}
我遇到的问题是 compactMap
这里不工作。 nil
值仍在结果数组 noNils
中。当我直接使用 compactMap
方法而不使用此包装器时,我得到了没有 nil
值的数组的期望结果。
let buttons = [actionMenuButton, createButton] // [UIBarButtonItem?]
let nonNilButtons = buttons.compactMap { [=12=] } // works correctly
let nonNilButtons2 = buttons.removeNilElements() // not working
我没有正确设计我的扩展方法吗?
您必须为 optional 元素数组定义方法,并将 return 类型定义为相应的非可选数组。这可以通过通用函数完成:
extension Array {
public func removingNilElements<T>() -> [T] where Element == T? {
let noNils = self.compactMap { [=10=] }
return noNils
}
}
示例:
let a = [1, 2, nil, 3, nil, 4] // The type of a is [Int?]
let b = a.removingNilElements() // The type of b is [Int]
print(b) // [1, 2, 3, 4]
在您的代码中,[=12=]
具有(非可选)类型 Element
,它只是被编译器包装成一个可选的,以便匹配 [=14= 的参数类型].