Leveraging extensions

In Swift, any class, struct, or protocol can be extended. In this case, we can improve implementation of the adapter pattern for MixpanelInstance by using an extension, as follows:

extension MixpanelInstance: Tracking {
func record(event: String, properties: [String : String]?) {
track(event: event, properties: properties)
}
}

With this pattern, there is no need to create a wrapper instance and forward implementation calls in the wrapper. Depending on your code base, use case, and particularities, you may find it easier to use extensions over wrapping as a method:

let tracker: Tracking

if USE_FIREBASE {
tracker = FirebaseTrackingAdapter()
} else {
tracker = Mixpanel.mainInstance()
}

Tracker.shared.set(trackingAdapter: tracker)

In this particular use case, it is to be noted that leveraging extensions does not work for the static implementation of the Firebase Analytics class. It is a limitation to consider when deciding how to implement the adapter pattern.