这是我的代码:
enum SymptomPeriod { case Day case Night } enum SymptomType { case Breathing(SymptomPeriod) case Breathlessness(SymptomPeriod) case Opression(SymptomPeriod) case Cough(SymptomPeriod) case ActivityLimited() case SecureTreatment() } struct Symptom { let type: SymptomType let date: NSDate }
我有一系列的症状.
let symptomList: [Symptom] = ...
我需要使用SymptomPerion标准过滤症状列表,我尝试这样做:
let daySymtoms = symptomList.filter { (symptom) -> Bool in return symptom.type = ??? }
我的问题在于过滤功能.
(我的目标是使用过滤功能而不是循环)
解决方法
一些建议
使用struct作为命名空间
您应该将您的枚举放入您的症状结构中,而不是重复症状一词(例如SymptomPeriod,SymptomType)
将SymptomType重命名为Kind
将SymptomType移动到Symptom后,可以删除名称的Symptom部分.但是,使用Type作为名称会产生冲突,因此您应该将其重命名为Kind.
这将使过滤更容易
这是代码
struct Symptom { enum Period { case Day case Night } enum Kind { case Breathing(Period) case Breathlessness(Period) case Opression(Period) case Cough(Period) case ActivityLimited() case SecureTreatment() var period: Period? { switch self { case Breathing(let period): return period case Breathlessness(let period): return period case Opression(let period): return period case Cough(let period): return period default: return nil } } } let kind: Kind let date: NSDate }
解决方案
现在过滤变得非常容易
let symptoms: [Symptom] = ... let filtered = symptoms.filter { $0.kind.period == .Day }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。