[英]Swift: create a infix operator for Optional values which provide default values when empty
In relation to my last question (over here: Swift where condition to check if a property is implemented ) where I thought I was very clever by creating a protocol extension to provide a default value for nil or empty strings by using the following code, I now have another question. 关于我的最后一个问题(在这里: Swift检查条件是否实现了属性的条件 ),在这里我以为我很聪明,通过使用以下代码创建协议扩展来为nil或空字符串提供默认值,我现在还有另一个问题。 But first, heres the code:
但是首先,这是代码:
protocol Emptyable {
var isEmpty: Bool { get }
}
extension Optional where Wrapped: Emptyable {
func orWhenNilOrEmpty<T: Emptyable>(_ defaultValue: T) -> T {
switch(self) {
case .none:
return defaultValue
case .some(let value) where value.isEmpty:
return defaultValue
case .some(let value):
return value as! T
}
}
}
extension String: Emptyable {}
As you see i can now provide default values for optionals implementing the Emptyable protocol I defined. 如您所见,我现在可以为实现我定义的Emptyable协议的可选提供默认值。 I did this to save the ever repeating code of this style (more about why I do this in the blog post mentioned in the other question):
我这样做是为了保存这种样式不断重复的代码(更多关于为什么我在另一个问题中提到的博客文章中这样做):
if let unwrapped = optional, !unwrapped.isEmpty {
myLabel.text = unwrapped
} else {
myLabel.text = "Some Default Text"
}
Now for the purpose of learning and becoming a better developer, I thought it could be fun to create a new operator to call the orWhenNilOrEmpty function for me like so: 现在,出于学习和成为更好的开发人员的目的,我认为创建一个新的运算符来像我这样调用orWhenNilOrEmpty函数可能会很有趣:
let optionalString: String? = nil
let actualString: String = some ??? "Hello World"
So I tried this: 所以我尝试了这个:
infix operator ???: DefaultPrecedence
extension Optional where Wrapped: Emptyable {
static func ???<T>(left: T?, right: T) -> T {
return left.orWhenNilAndEmpty(right)
}
}
But I get this error and I don't really understand why. 但是我得到了这个错误,我也不是很清楚为什么。 I hope you can clarify the issue and explain what I'm doing wrong:
希望您能澄清问题并解释我做错了什么:
generic parameter 'Wrapped' could not be inferred
Turns out the problem was trying to use a generic T
in the function declaration. 原来问题出在试图在函数声明中使用泛型
T
Also DefaultPrecedence
wasn't the best choice. 同样,
DefaultPrecedence
也不是最佳选择。 This is how I now solved it and how it works: 这就是我现在解决它的方法及其工作方式:
protocol Emptyable { var isEmpty: Bool { get } }
infix operator ???: NilCoalescingPrecedence
extension Optional where Wrapped: Emptyable {
func orWhenNilOrEmpty(_ defaultValue: Wrapped) -> Wrapped {
switch(self) {
case .none:
return defaultValue
case .some(let value) where value.isEmpty:
return defaultValue
case .some(let value):
return value
}
}
static func ???(left: Wrapped?, right: Wrapped) -> Wrapped {
return left.orWhenNilOrEmpty(right)
}
}
Now all I have to do is implement Emptyable
where ever I want and then I can use ???
现在,我所要做的就是在需要的地方实现
Emptyable
,然后可以使用???
to get a not empty value from any optional: 从任何可选的获取非空值:
extension String: Emptyable {}
let notEmptyString = nilOrEmptyOptionalString ??? "Meaningful default value"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.