swift - 测试字典中是否存在 nil 值

标签 swift validation dictionary

我有以下字典:

["oct": nil, 
 "jan": Optional(3666.0), 
 "nov": nil, 
 "apr": nil, 
 "sep": nil, 
 "feb": nil, 
 "jul": nil, 
 "mar": nil, 
 "dec": nil, 
 "may": nil, 
 "jun": nil, 
 "aug": nil]

我只想在任何键的任何值不为零时启用按钮。是否有任何功能性“魔法”可以在没有传统循环的情况下做到这一点?

最佳答案

使用contains(where:)关于字典值:

// Enable button if at least one value is not nil:
button.isEnabled = dict.values.contains(where: { $0 != nil })

或者

// Enable button if no value is nil:
button.isEnabled = !dict.values.contains(where: { $0 == nil })

关于swift - 测试字典中是否存在 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48761660/

相关文章:

ios - UIResponder 不识别选择器

iOS Swift 如何告诉 UIView Controller 我们通过 NSURLProtocol 对象接收到信息

ios - 如何重用 View 对象?

c# - 带有 DateTime 参数的 .NET 标准程序集属性

validation - 如何在 CQRS 中处理基于集合的一致性验证?

c# - 如何在 C# 中从 TKey 获取字典 TValue?

python - 使用保留字 "class"定义数据类的属性并将其序列化

swift - Swift 中的单引号字符

javascript - jquery 输入字段验证

python - 如何在循环中从字典填充数据框