开发者

conformance vs. inheritance in Cocoa/Objective-C classes

开发者 https://www.devze.com 2023-01-09 11:17 出处:网络
I noticed that Cocoa/Objective-C classes inherit and conform from other classes. I understand what inheritance is, but no开发者_开发问答t conformance. What\'s the difference?

I noticed that Cocoa/Objective-C classes inherit and conform from other classes. I understand what inheritance is, but no开发者_开发问答t conformance. What's the difference?

Also, is a class like UIView a Cocoa class or an Objective-C class?


Classes don't conform to classes, they conform to protocols, which are basically lists of messages that a class can respond to. If a class conforms to a protocol, it must respond to all the non-optional messages listed in the protocol interface.

Also, there isn't really a difference between a Cocoa class and an Objective-C class. Cocoa is an Objective-C framework, so all of its classes are Objective-C classes. If you're asking whether classes like UIView are part of the language or the framework: Every class you normally interact with in a Cocoa/Cocoa Touch app comes from the framework. The native classes the standard Objective-C runtime provides are:

  • Object
  • Protocol
  • List

You can go to /usr/include/objc if you want to see the headers for yourself. None of these are used in a normal Cocoa or Cocoa Touch program. You do use Protocols (as mentioned above), but don't normally refer to the class directly.


Conformance or more accurately conforming to a protocol is simply Objective C's way of specifying behavior that the class should implement, similar to a java interface.

UIView is part of the UIKit framework so it is a Cocoa class.


Conformance is more or less the same as assignability. But i never saw this second term used used for it.

If you can assign an object to a variable b then a conforms to b.

0

精彩评论

暂无评论...
验证码 换一张
取 消