• Object 1
import Foundation
class ObjectAdaptee1 {
    var IntCount = 0
}

Copy the code
  • Object 2
Import Foundation class ObjectAdaptee {var URAMoney = 0.0}Copy the code
  • A protocol has only one method as an example, but there may be multiple methods.
Import Foundation protocol ObjectTarget {func getRMB() -> Double func getInt() -> Int} extension ObjectTarget { func getInt()->Int { return 10 } }Copy the code
  • Let the match each have a method
Import Foundation // Role 3: adapter // Implement target interface // hold a reference to the adaptee class ObjectAdapter: ObjectTarget {var adaptee: ObjectAdaptee var adaptee1: ObjectAdaptee1 init(adaptee: ObjectAdaptee,addaptee1:ObjectAdaptee1) { self.adaptee = adaptee self.adaptee1 = addaptee1 } func getRMB() -> Double { Return self.adaptee.uramoney * 6.5} func getInt()->Int {return self.adaptee1.intcount * 2}}Copy the code
  • call
import Foundation print("Hello, World!" ) let adaptee = ObjectAdaptee() adaptee.URAMoney = 100 let adaptee1 = ObjectAdaptee1() adaptee1.IntCount = 10 let adapter = ObjectAdapter(adaptee: adaptee, addaptee1: adaptee1) let rmb = adapter.getRMB() let intC = adapter.getInt() print("rmb = \(rmb) int = \(intC)")Copy the code
  • Result RMB = 650.0 int = 20