Returned BOOL values from Objc to Python are ints

Issue #69 resolved
Patrick Robertson
created an issue

Hi,

I'm currently creating a PyObjc app which uses a mixture of PyObjc and Plain Objc (mainly for view stuff). I have a method in one of my (Objective-C) view controllers as follows:

@implementation MyVC
- (BOOL)isVisible {
    return [self.mySpecialView isVisible]
}

in Python, I call this:

MyVC = NSClassFromString("MyVC")
# myviewcontroller =  MyVC.alloc().init()
# …stuff

isVisible = myviewcontroller.isVisible()
if isVisible is False:
    # do something special

Now, I'd expect this call through to my Objective-C method to return a Python bool value of True or False. However, it seems to return an int (1 or 0) I've checked this with repr(isVisible) and what have you. This subsequently causes the line:

if isVisible is False:

to fail (since 0 is not False) Of course I can do

if isVisible == False:

to get round this, but I feel like if I'm declaring a BOOL in Obj-C I should get one pack in Python

P.S. I've tried casting the return value of my Obj-C method to a BOOL just incase that'd make a difference. It doesn't.

Thanks for any help

Comments (4)

  1. Ronald Oussoren repo owner

    This is expected behavior, "BOOL" is an alias (typedef) for "char" in Objective-C and because of that it is bridged as a small integer. It is possible to teach PyObjC about specific method signatures (see http://pythonhosted.org/pyobjc/metadata/manual.html), but I don't think it is worth doing for your usecase.

    Anyway, the code samples you list are not idiomatic Python code, its better to use "if not isVisible" then either of the alternatives you list.

  2. Patrick Robertson reporter

    Makes sense. I thought my situation was probably an edge case that didn't really need fixing. Being more Pythonic and using "if not isVisible" is probably the right answer.

    Thanks for your time

  3. Log in to comment