Skip to content

Commit

Permalink
Merge pull request #29 from kivy/protocol_docs
Browse files Browse the repository at this point in the history
Add protocol docs
  • Loading branch information
Zen-CODE committed Apr 21, 2016
2 parents ecc9ab8 + 1171c05 commit 3fd502d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/source/core_tutorials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,37 @@ If you want get the SEL for ``initWithUTF8String:`` you can use::

Other cases are the same for all methods.

Using @protocol
---------------

Objective C protocols provide what other languages call interfaces. They specify
a list of methods which should be implemented in order to support that protocol.

Protocols define the interface which is then usually implemented by a delegate.
Pyobjus provides us with the protocol decorator to handle this, enabling
us to use Python objects as delegates::

@protocol('<protocol_name>')

The list of protocols supported by pyobjus is contained in the
`pyobjus/protocols.py` file in your pyobjus checkout folder. Of course, many
libraries define their own protocols, so cannot be included by default. For
a complete list of protocols available on you system, run the
`tools/build_protocols.py` file and then rebuild pyobjus (as per the install).

So, how do we use this decorator? We add functions with names that correspond
to the protocol method names, then decorate these functions with the required
protocol::

@protocol('NSURLConnectionDelegate')
def connection_didFailWithError_(self, connection, error):

Here, we specify that our object method `connection_didFailWithError_` handles
the `connection:didFailWithError:` delegation of the `NSURLConnectionDelegate`
protocol. Pyobjus then redirects this Objective-C message to our method.

For a complete example, please see the `examples/delegate.py` file.

Using enum types
----------------

Expand Down

0 comments on commit 3fd502d

Please sign in to comment.