1.

Please look at the code below. If we run method ., what will be the output and why?

Answer»

Key is to use associated typeswhich is a powerful way of making protocols generic.

protocol STORE {     associatedtype DataType     VAR ids: [DataType] { get set}     mutating func add(id: DataType) } extension Store {     mutating func add(id: DataType) {         ids.append(id)     } } STRUCT SystemOne: Store {     var ids = [String]() } struct SystemTwo: Store {     var ids = [INT]() } var sysOne = SystemOne() sysOne.add(id: "One") sysOne.add(id: "Two") var sysTwo = SystemTwo() sysTwo.add(id: 1) sysTwo.add(id: 2) print(sysOne.ids) print(sysTwo.ids)


Discussion

No Comment Found

Related InterviewSolutions