API

This part of the documentation covers all the interfaces of Pyobjus.

Reflection functions

pyobjus.autoclass(name[, copy_properties=None, load_class_methods=None, load_instance_methods=None, reset_autoclass=None])

Get and load an Objective C class

Parameters:
  • name – Name of the Objective C class which you want to load.
  • copy_properties (None or Boolean) – Denotes whether to copy the properties of the Objective C class or not. The default is to copy all properties.
  • load_class_methods (None or List) – If this argument is omitted or None, all class methods will be loaded. You can use it to force only certain class methods to be loaded eg. load_class_methods=[‘alloc’].
  • load_instance_methods (None or List) – If this argument is omitted or set to None, all instance methods will be loaded. You can use it force only instance methods to be loaded, eg. load_instance_methods=[‘init’].
  • reset_autoclass (None or Boolean) – If this argument is set to True and the class was previously loaded with a restricted subset of methods, when you call the autoclass function again with this argument for the same class, all the methods will be loaded.
Return type:

Return a ObjClass that represents the class passed from name.

>>> from pyobjus import autoclass
>>> autoclass('NSString')
<class '__main__.NSString'>

Utility functions

pyobjus.selector(objc_method)

Get the selector for the method specified by the objc_method parameter

Parameters:objc_method (String) – Name of the Objective C method for which we want to get the SEL.
Return type:ObjcSelector, which is a Python representation for the Objective C SEL type.
pyobjus.dereference(objc_reference[, of_type=None, return_count=None, partition=None])

Dereference the C pointer to get the actual values

Parameters:
  • objc_referenceObjcReferenceToType Python representation of the C pointer.
  • of_type – If the function which you call returns a value, for example an int, float, etc., pyobjus can determine the type which to convert it to. But if you return a void pointer for eg. then you need to specify the type to which you want to convert it. An example of this is: dereference(someObjcReferenceToType, of_type=ObjcInt).
  • return_count (Integer) – When you are returning a C array, you can/need specify number of returned values with this argument.
  • partition – When you want to dereference a multidimensional array, you need to specify it’s dimensions. Provide a list with numbers which denote it’s dimensions. For example, with int array[10][10], you need to specify partition=[10, 10].
Return type:

Actual value for some ObjcReferenceToType type.

pyobjus.objc_c(some_char)

Initialize NSNumber with a Char type.

Parameters:some_char – Char parameter
Return type:NSNumber.numberWithChar: Python representation
pyobjus.objc_i(some_int)

Initialize NSNumber with an Int type.

Parameters:some_int – Int parameter
Return type:NSNumber.numberWithInt: Python representation
pyobjus.objc_ui(some_unsigned_int)

Initialize NSNumber with an Unsigned Int type.

Parameters:some_unsigned_int – Unsigned Int parameter
Return type:NSNumber.numberWithUnsignedInt: Python representation
pyobjus.objc_l(some_long)

Initialize NSNumber with a Long type.

Parameters:some_char – Long parameter
Return type:NSNumber.numberWithLong: Python representation
pyobjus.objc_ll(some_long_long)

Initialize NSNumber with a Long Long type.

Parameters:some_long_long – Long Long parameter
Return type:NSNumber.numberWithLongLong: Python representation
pyobjus.objc_f(some_float)

Initialize NSNumber with a Float type.

Parameters:some_float – Float parameter
Return type:NSNumber.numberWithFloat: Python representation
pyobjus.objc_d(some_double)

Initialize NSNumber with a Double type.

Parameters:some_double – Double parameter
Return type:NSNumber.numberWithDouble: Python representation
pyobjus.objc_b(some_bool)

Initialize NSNumber with a Bool type.

Parameters:some_char – Bool parameter
Return type:NSNumber.numberWithBool: Python representation
pyobjus.objc_str(some_string)

Initialize NSNumber with a NSString type.

Parameters:some_float – String parameter
Return type:NSString.stringWithUTF8String: Python representation
pyobjus.objc_arr(some_array)

Initialize a NSArray type

Parameters:some_array – List of parameters. For eg:
objc_arr(objc_str('Hello'), objc_str('some str'), objc_i(42))
Return type:NSArray Python representation
pyobjus.objc_dict(some_dict)

Initialize a NSDictionary type

Parameters:some_dict – Dict parameter. For eg:
objc_dict({
    'name': objc_str('User name'),
    'date': autoclass('NSDate').date(),
    'processInfo': autoclass('NSProcessInfo').processInfo()
})
Return type:NSDictionary Python representation

Global variables

pyobjus.dev_platform

Platform for which pyobjus is compiled

Pyobjus Objective C types

class pyobjus.ObjcChar

Objective C char representation

class pyobjus.ObjcInt

Objective C int representation

class pyobjus.ObjcShort

Objective C short representation

class pyobjus.ObjcLong

Objective C long representation

class pyobjus.ObjcLongLong

Objective C long long representation

class pyobjus.ObjcUChar

Objective C unsigned char representation

class pyobjus.ObjcUInt

Objective C unsigned int representation

class pyobjus.ObjcUShort

Objective C unsigned short representation

class pyobjus.ObjcULong

Objective C unsigned long representation

class pyobjus.ObjcULongLong

Objective C unsigned long long representation

class pyobjus.ObjcFloat

Objective C float` representation

class pyobjus.ObjcDouble

Objective C double representation

class pyobjus.ObjcBool

Objective C bool representation

class pyobjus.ObjcBOOL

Objective C BOOL representation

class pyobjus.ObjcVoid

Objective C void representation

class pyobjus.ObjcString

Objective C char* representation

class pyobjus.ObjcClassInstance

Representation of an Objective C class instance

class pyobjus.ObjcClass

Representation of an Objective C Class

class pyobjus.ObjcSelector

Representation of an Objective C SEL

class pyobjus.ObjcMethod

Representation of an Objective C method

class pyobjus.CArray

Representation of an Objective C (C) array

class pyobjus.CArrayCount

Representation of a type which holds outCount* for some C array -> number of received array elements

exception pyobjus.ObjcException

Representation of some Objective C exception

Structure types

class pyobjus.objc_py_types.NSRange
unsigned long long location
unsigned long long length
class pyobjus.objc_py_types.NSPoint
double x
double y
class pyobjus.objc_py_types.NSSize
double width
double height
class pyobjus.objc_py_types.NSRect
NSPoint origin
NSSize size

Enumeration types

class pyobjus.objc_py_types.NSComparisonResult
NSOrderedAscending = -1
NSOrderedSame = 0
NSOrderedDescending = 1
class pyobjus.objc_py_types.NSStringEncoding
NSASCIIStringEncoding = 1
NSNEXTSTEPStringEncoding = 2
NSJapaneseEUCStringEncoding = 3
NSUTF8StringEncoding = 4
NSISOLatin1StringEncoding = 5
NSSymbolStringEncoding = 6
NSNonLossyASCIIStringEncoding = 7
NSShiftJISStringEncoding = 8
NSISOLatin2StringEncoding = 9
NSUnicodeStringEncoding = 10
NSWindowsCP1251StringEncoding = 11
NSWindowsCP1252StringEncoding = 12
NSWindowsCP1253StringEncoding = 13
NSWindowsCP1254StringEncoding = 14
NSWindowsCP1250StringEncoding = 15
NSISO2022JPStringEncoding = 21
NSMacOSRomanStringEncoding = 30
NSUTF16StringEncoding = 10
NSUTF16BigEndianStringEncoding = 0x90000100
NSUTF16LittleEndianStringEncoding = 0x94000100
NSUTF32StringEncoding = 0x8c000100
NSUTF32BigEndianStringEncoding = 0x98000100
NSUTF32LittleEndianStringEncoding = 0x9c000100
NSProprietaryStringEncoding = 65536

Dynamic library manager

pyobjus.dylib_manager.load_dylib(path)

Function for loading a user defined dylib.

Parameters:path – Path to some dylib.
pyobjus.dylib_manager.make_dylib(path[, frameworks=None, out=None, options=None])

Function for making a dylib from Python.

Parameters:
  • path – Path to the file.
  • frameworks (List) – List of frameworks to link with.
  • options (List) – List of options for the compiler
  • out – Out location. The default is to write to the location specified by the path argument.
pyobjus.dylib_manager.load_framework(framework)

Function that loads an Objective C framework via NSBundle.

Parameters:framework (String) – Path to the framework.
Raises:ObjcException if the framework can’t be found.

Objective-C signature format

Objective C signatures have a special format that can be difficult to understand at first. Let’s look into the details. A signature is in the format:

<return type><offset0><argument1><offset1><argument2><offset2><...>

The offset represents how many bytes the previous argument is from the start of the allocated memory.

All the types for any part of the signature can be one of:

  • c = represent a char
  • i = represent an int
  • s = represent a short
  • l = represent a long (l is treated as a 32-bit quantity on 64-bit programs.)
  • q = represent a long long
  • c = represent an unsigned char
  • i = represent an unsigned int
  • s = represent an unsigned short
  • l = represent an unsigned long
  • q = represent an unsigned long long
  • f = represent a float
  • d = represent a double
  • b = represent a c++ bool or a c99 _bool
  • v = represent a void
  • * = represent a character string (char *)
  • @ = represent an object (whether statically typed or typed id)
  • # = represent a class object (class)
  • : = represent a method selector (sel)
  • [array type] = represent an array
  • {name=type...} = represent a structure
  • (name=type...) = represent a union
  • bnum = represent a bit field of num bits
  • ^ = represent type a pointer to type
  • ? = represent an unknown type (among other things, this code is used for function pointers)