views:

50

answers:

3

I noticed that Cocoa/Objective-C classes inherit and conform from other classes. I understand what inheritance is, but not conformance. What's the difference?

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

+1  A: 

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.

ennuikiller
No, UIView is part of the UIKit framework, so it is a *Cocoa Touch* class. UIKit is not part of Cocoa.
Peter Hosey
Yes, I stand corrected....
ennuikiller
A: 

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.

Lothar
Except that in Objective-C, you can assign a pointer to an instance of any class to any pointer variable, including a variable declared as holding a pointer to an instance of a completely unrelated class.
Peter Hosey
+1  A: 

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.

Chuck
I've seen things conform to NSObject. Isn't that an object and not a protcol?
awakeFromNib
@awakeFromNib: It's both. There is an NSObject class and a protocol named NSObject to which the class conforms. Class: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html Protocol: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intf/NSObject
Chuck