Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Needs documentation on how to pass an pointer-to-pointer “out parameter” #3

Open
uranusjr opened this issue May 7, 2015 · 1 comment
Labels
enhancement New features, or improvements to existing features.

Comments

@uranusjr
Copy link
Contributor

uranusjr commented May 7, 2015

It is common in Cocoa API for a method to have an “out parameter”, e.g. an NSError ** parameter that is used to return an error when a call fails.

Taking the deserialisation method in NSJSONSerialization for example, this method

+ (id)JSONObjectWithData:(NSData *)data
                 options:(NSJSONReadingOptions)opt
                   error:(NSError **)error

can be called like this in Objective-C:

NSError *err = nil;
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];

And the equivalent with Rubicon-objc would be

from ctypes import byref, c_void_p

err = c_void_p(0)
obj = NSJSONSerialization.JSONObjectWithData_options_error_(
    data, 0, byref(err),
)

NB: err = None will not work since byref expects a ctypes instance, and does not automatically convert None to a NULL pointer.

@freakboy3742
Copy link
Member

Agreed that this should be in the docs.

One additional detail - once you've got your err object, you can turn it into an actual ObjCInstance of whatever type you want by using:

error = NSError(err)

That is - if all you have is a pointer to an ObjC instance, you can create a wrapped object using the Python constructor for the relevant class. You can then invoke methods on the ObjC instance as you would one that you had instantiated using NSError.alloc().init() or NSError.new().

@dgelessus dgelessus added the enhancement New features, or improvements to existing features. label Mar 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features, or improvements to existing features.
Projects
None yet
Development

No branches or pull requests

3 participants