如何比较值可选([:])

问题描述 投票:0回答:1

我正在注销(好吧,print()-ing)一些变量,并且作为输出我有太多次有价值:

Optional([:])

我想忽略这个值,所以我尝试了

if a == Optional([:]) {
    // ignore
} else {
    print(a)
}

但这不起作用 - 我立即(在构建和运行之前)从 XCode 中收到错误

"Binary operator '==' cannot be applied to two '[AnyHashable : Any]?' operands"
我如何检测该值
Optional([:])

swift debugging printing output optional-parameters
1个回答
1
投票

解决此问题的一种方法是将计算属性添加到

Optional

extension Optional where Wrapped: Collection {
    var isEmptyOrNil: Bool {
        guard let self else { return true }
        return self.isEmpty
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.