1. Ronald Oussoren
  2. pyobjc


pyobjc / Doc / protocols.html

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
PyObjC protocol support</title>
<h2>PyObjC protocol support</h2>
<h2><a name="introduction">Introduction</a></h2>
<p>Apple makes use of both formal and informal protocols in the Cocoa framework.
Formal protocols are those protocols that are implemented using Objective-C
@protocol NSFoo &lt;NSSomeProtocol&gt;
<p>Conforming to a formal protocol requires the interface of a class to explicitly
declare that it implements that protocol, and the implementation must implement
all methods of the protocol.</p>
<p>Informal protocols are defined as categories on NSObject with no implementation:</p>
@interface NSObject(FooDelegate)
<p>Conforming to an informal protocol is much like conforming to a protocol in Python,
the class simply implements the methods as documented.  In most cases, informal
protocols are comprised entirely of optional methods (i.e. NSApplicationDelegate)
and a check is made (i.e. <code><span>-[NSObject</span> <span>respondsToSelector:]</span></code>) before sending
the message to the target.</p>
<h2><a name="informal-protocols-and-pyobjc">Informal protocols and PyObjC</a></h2>
<p>PyObjC has an explicit representation for informal protocols.  This makes
it possible to use the protocol description to provide better error messages and
to automaticly deduce the method signatures for classes that implement an
informal protocol.</p>
<p>Informal protocols are represented using instances of 
<code><span>objc.informal_protocol</span></code>.  These instances are automaticly added to a
registry used by the class builder, so it is not necessary to explicitly
state that a class implements an informal protocol.</p>
<h2><a name="formal-protocols-and-pyobjc">Formal protocols and PyObjC</a></h2>
<p>PyObjC also has an explicit representation for formal protocols.</p>
<p>Formal protocols are represented as instances of <code><span>objc.formal_protocol</span></code>. 
Unlike informal protocols, it is necessary to explicitly declare
conformance to formal protocols.  However, all formal protocols in Cocoa
are also described using <code><span>objc.informal_protocol</span></code> objects.</p>
<p>XXX: is this necessary? we could also use the same strategy as for informal
protocols, and drop the informal_protocol wrappers for formal protocols.</p>
<p>Declaring conformance to a formal protocol is done by using the formal protocol
as a mix-in, and by implementing its methods:</p>
NSLocking = objc.protocolNamed('NSLocking')

class MyLockingObject(NSObject, NSLocking):
        def lock(self):

        def unlock(self):
<p>XXX: Change this example when the pyobjc_classMethods hack is no longer necessary.</p>
<p>The class now formally implements the <code><span>NSLocking</span></code> protocol, this can be
verified using the Objective-C introspection methods:</p>
&gt;&gt;&gt; MyLockingObject.pyobjc_classMethods.conformsToProtocol_(NSLocking)
<p>This is useful for API's that require (and check) the implementation of formal
<p>XXX: might also be useful for Distributed Objects, create an example</p>