1. Dwayne Litzenberger
  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>Informal protocols on the other hand are purely documentation, the Objective-C 
compiler doesn't know about them. For end users the main difference between
formal and informal protocols is that you have to declare that you implement a
formal protocol and that formal protocols do not have optional methods.</p>
<h2><a name="informal-protocols-and-pyobjc">Informal protocols and PyObjC</a></h2>
<p>PyObjC does have 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,
therefore it is not necessary to declare that you implement an informal 
<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 you have to declare that you implement a formal 
protocol to get all features of 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>You can declare that you implement a formal protocol by using the protocol as
a mix-in:</p>
class MyLockingObject (NSObject, NSLocking):
        def lock(self): pass
        def unlock(self): pass
<p>The class now formally implements the <code><span>NSLocking</span></code> protocol, you can check this
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>