Understanding @Protocols in Objective-C

前端 未结 5 1054
一整个雨季
一整个雨季 2020-12-11 12:38

I am a beginner to programming, and a beginner to Objective-C. I learned basic C and decided to start learning Objective-C. I am reading \"Programming in Objective C 2.0\" b

5条回答
  •  春和景丽
    2020-12-11 13:03

    If you've done any kind of object-oriented programming, you probably know protocols as interfaces (they're not identical, but the concept is similar). If not, think of protocols as blueprints.

    The main reason why you'd use protocols is so you can use objects without knowing everything about them; all you need to know is that they implement a set of methods. For example, if the classes Business and Person conform to the protocol Contact, which defines the method - (NSString *)phoneNumber, the class AddressBook can call -(NSString *)phoneNumber without knowing whether or not the object is of type Business or Person.

    Once you start to learn about Cocoa and delegates, you'll see how powerful and important protocols are.

提交回复
热议问题