Package pycocoa :: Module runtime :: Class ObjCSubclass
[frames] | no frames]

Class ObjCSubclass

object --+
         |
        ObjCSubclass

Use this to create a subclass of an existing Objective-C class.

It consists primarily of function decorators which you use to add methods to the subclass.

ObjCSubclass is used to define an Objective-C subclass of an existing class registered with the runtime. When you create an instance of ObjCSubclass, it registers the new subclass with the Objective-C runtime and creates a set of function decorators that you can use to add instance methods or class methods to the subclass.

Typical usage would be to first create and register the subclass:

>>> MySubclass = ObjCSubclass('NSObject', 'MySubclassName')

then add methods with:

>>> @MySubclass.method('v')
>>> def methodThatReturnsVoid(self):
>>>     pass
>>> @MySubclass.method('Bi')
>>> def boolReturningMethodWithInt_(self, x):
>>>     return True
>>> @MySubclass.classmethod('@')
>>> def classMethodThatReturnsId(self):
>>>     return self

It is probably a good idea to organize the code related to a single subclass by either putting it in its own module (note that you don't actually need to expose any of the method names or the ObjCSubclass) or by bundling it all up inside a Python class definition, perhaps called MySubclassImplementation.

It is also possible to add Objective-C ivars to the subclass, however if you do so, you must call the __init__ method with register=False, and then call the register method after the ivars have been added. But rather than creating the ivars in Objective-C land, it is easier to just define Python-based instance variables in your subclass's init method.

This class is used only to *define* the interface and implementation of an Objective-C subclass from Python. It should not be used in any other way. If you want a Python representation of the resulting class, create it with ObjCClass.

Instances are created as a pointer to the objc object by using:

>>> myinstance = send_message('MySubclassName', 'alloc')
>>> myinstance = send_message(myinstance, 'init')

or wrapped inside an ObjCInstance object by using:

>>> myclass = ObjCClass('MySubclassName')
>>> myinstance = myclass.alloc().init()
Instance Methods
 
__init__(self, supercls, name, register=True, **ivars)
New subclass of the given (super)class.
 
add_ivar(self, name, ctype)
Add an instance variable to the subclass.
 
classmethod(self, encoding)
Decorator for class methods.
 
method(self, encoding)
Decorator for instance methods.
 
rawmethod(self, encoding)
Decorator for instance methods without any fancy shenanigans.
 
register(self)
Register the new class with the Objective-C runtime.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  objc_cls = None
  objc_metaclass = None
Properties

Inherited from object: __class__

Method Details

__init__(self, supercls, name, register=True, **ivars)
(Constructor)

 

New subclass of the given (super)class.

Optionally, specify any number of instance variables to be added before registering the new class with a keyword argument ivarname=ctype the specify the name and ctype of each instance variable.

Overrides: object.__init__

add_ivar(self, name, ctype)

 

Add an instance variable to the subclass.

name should be a string. ctype is a ctypes type.

The class must be registered AFTER adding instance variables.

rawmethod(self, encoding)

 

Decorator for instance methods without any fancy shenanigans.

The method must have signature m(self, cmd, *args) where both self and cmd are just pointers to Objective-C objects.