[XCode 8.3-Swift 3.2-Mac OS X 10.11]
我想在我的switch语句中删除冗余和重复的代码。我已经阅读了有关Swift keyPath的文章,但我不明白如何将它们应用于switch语句。我想恢复此代码:
let realm = try! Realm()
let patient = realm.object(ofType: Patient.self, forPrimaryKey: superPatient.data.UUID)
try! realm.write {
switch Type {
case .inicialExtraoralFrontalReposo:
patient?.inicialExtraoralFrontalReposo = String(describing: destURL)
case .inicialExtraoralFrontalSonrisa:
patient?.inicialExtraoralFrontalSonrisa = String(describing: destURL)
case .inicialExtraoralPerfilReposo:
patient?.inicialExtraoralPerfilReposo = String(describing: destURL)
case .inicialExtraoralPerfilSonrisa:
patient?.inicialExtraoralPerfilReposo = String(describing: destURL)
case .inicialExtraoralTrescuartosReposo:
patient?.inicialExtraoralTrescuartosReposo = String(describing: destURL)
case .inicialExtraoralTrescuartosSonrisa:
patient?.inicialExtraoralTrescuartosSonrisa = String(describing: destURL)
case ...
...
...
default
break
}
}
无论如何,有没有像...这样的表达式来恢复该代码:
patient?.{Type} = String(describing: destURL)
?
类型枚举与我要保存的患者属性相同。