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