What is the difference between a protocol and a interface in Objective-c?

后端 未结 5 875
猫巷女王i
猫巷女王i 2021-01-03 22:49

I am confused about what the difference is between a protocol and an interface? They both seem to be doing the same thing?

Is it like abstract in C# in that you are

相关标签:
5条回答
  • 2021-01-03 23:04

    A protocol is a group of related properties and methods that can be implemented by any class. They are more flexible than a normal class interface, since they let you reuse a single API declaration in completely unrelated classes. This makes it possible to represent horizontal relationships on top of an existing class hierarchy.

    A class interface declares the methods and properties associated with that class.

    A protocol, by contrast, is used to declare methods and properties that are independent of any specific class.

    0 讨论(0)
  • 2021-01-03 23:11

    Objective-C: protocol.

    Java: interface.

    Otherwise, no difference.

    0 讨论(0)
  • 2021-01-03 23:21
    In Java - you implement an Interface
    In Swift/Objective C - you conform to a Protocol
    
    "Program to an Interface, not an Implementation"
    - Design Patterns 1995
    
    0 讨论(0)
  • 2021-01-03 23:23

    In Objective C an interface is equivalent to a C++ class declaration. And a protocol is equivalent to a Java interface.

    Edit: In Objective C the class definition is separated into two components called the interface and implementation, which allows you to shrink the header files. This is similar to C++. Java doesn't have an equivalent, because you implement your class functions within the class definition. C# is similar to Java in this respect.

    0 讨论(0)
  • 2021-01-03 23:25

    a protocol in Objective-C is the same as an interface in java, if thats what you mean

    0 讨论(0)
提交回复
热议问题