Sobes.tech
Junior — Middle

How can you ensure that a class works simultaneously with multiple interfaces or implementations?

sobes.tech AI

Answer from AI

In iOS (Swift or Objective-C), a class can implement multiple protocols (interfaces), allowing it to work with different sets of methods.

Example in Swift:

protocol Printable {
    func printInfo()
}

protocol Identifiable {
    var id: String { get }
}

class User: Printable, Identifiable {
    var id: String
    var name: String

    init(id: String, name: String) {
        self.id = id
        self.name = name
    }

    func printInfo() {
        print("User: \(name), ID: \(id)")
    }
}

let user = User(id: "123", name: "Alice")
user.printInfo()

Thus, the User class simultaneously implements two interfaces — Printable and Identifiable. This allows using the object in different contexts where one of these interfaces is required.

If you need to work with different implementations, you can use composition or delegation to switch between them.

How can you ensure that a class works simultaneously… - sobes.tech