Skip to main content

A bridge between an Objective C runtime environment and Python.

Project description

Rubicon-ObjC is a bridge between Objective C and Python. It enables you to:

  • Use Python to instantiate objects defined in Objective C

  • Use Python to invoke methods on objects defined in Objective C

  • Subclass and extend Objective C classes in Python

It also includes wrappers of the some key data types from the Core Foundation framework (e.g., NSString).

Quickstart

Rubicon uses a combination of ctypes, plus Objective-C’s own reflection APIs, to enable Objective C objects to be referenced in a Python process.

To install Rubicon, use pip:

$ pip install rubicon-objc

Then, in a Python shell:

>>> from ctypes import cdll
>>> from ctypes import util
>>> from rubicon.objc import ObjCClass, ObjCSubclass, at, to_str

# Use ctypes to import a framework into the Python process
>>> cdll.LoadLibrary(util.find_library('Foundation'))

# Wrap an Objective C class contained in the framework
>>> NSURL = ObjCClass("NSURL")

# Then instantiate the Objective C class, using the API
# that is exposed through Objective C. The Python method name
# is the concatenated version of the Objective C method descriptor,
# with colons replaced with underscores. So, the equivalent of
# [NSURL URLWithString:@"http://pybee.org"];
# would be:
>>> NSURL.URLWithString_("http://pybee.org/")

# To create a new Objective C class, define a Python class that
# has the methods you want to define:
>>> class Handler_impl(object):
...     Handler = ObjCSubclass('NSObject', 'Handler')
...
...     @Handler.method('@i')
...     def initWithValue_(self, value):
...         # You can't store attributes directly on the object -
...         # you need to put them manually on the Python object
...         self.__dict__['value'] = value
...         return self
...
...     @Handler.method('vi')
...     def pokeWithValue_(self, value):
...         print ("Poking with", value)

# Then, create a wrapper for that class...
>>> Handler = ObjCClass('Handler')

# ...and use it:
>>> my_handler = Handler.alloc().initWithValue_(42)
>>> my_handler.pokeWithValue_(37)

Community

Rubicon is part of the BeeWare suite. You can talk to the community through:

Contributing

If you experience problems with this backend, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rubicon-objc-0.1.0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rubicon_objc-0.1.0-py2.py3-none-any.whl (21.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rubicon-objc-0.1.0.tar.gz.

File metadata

  • Download URL: rubicon-objc-0.1.0.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rubicon-objc-0.1.0.tar.gz
Algorithm Hash digest
SHA256 24744d2e24b2ea4a25da2905dd64f8ce88c8970651a459bfb95910acf4707954
MD5 c94c75bbaa803b8834b56a8ea39a907f
BLAKE2b-256 c945fdbebd86701eadb156183e332f0fffead992f8b5a5f19238c184b3dcdafd

See more details on using hashes here.

File details

Details for the file rubicon_objc-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for rubicon_objc-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1f7cc1d2f8176ed55a37076aece27df8da09777ffc701aa2ae587a2d97e6db42
MD5 bdca7f6e31587756bdcb489db5f72cb0
BLAKE2b-256 e1c328ff8e94bc627c0263ca2f00806fbe02c26192f8e46e5b6ea2927b4543b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page