1
有没有办法做到这一点作为数组的扩展,而不是一个将增长和增长的switch语句?如何添加扩展到Swift数组以有条件追加?
fileprivate var exteriorColorOptions = [ExteriorColorOption]()
fileprivate var otherOptions = [SomeOtherOption]()
: more options
func add(option:FilteredOption) {
switch(option) {
case let thing as ExteriorColorOption:
exteriorColorOptions.append(thing)
case and on and on
default:
break
}
}
我想能够只是做了与地方右转以下内容:
exteriorColorOptions.appendIfPossible(option)
otherOptions.appendIfPossible(option)
注:开关的方法来自 Swift: Test class type in switch statement