Forum Archive

rubicon.objc’s problem

ryubai

latest when i try rubicon_objc.
the code below, run once is normal. run anther time will raise a err.
how can i solve it? thanks in advance

RuntimeError: An Objective-C class named b'Handler' already exists

code:

from rubicon.objc import NSObject, objc_method

class Handler(NSObject):
@objc_method
def initWithValue_(self, v: int):
self.value = v
return self

@objc_method
def pokeWithValue_andName_(self, v: int, name) -> float:
    print("My name is", name)
    return v / 2.0

my_handler = Handler.alloc().initWithValue(42)

JonB

I'm not sure how Rubicon handles things -- for objc_util, the way it handles that is by renaming the objc class -- so if you define Handler again, it will create a class called Handler1. There is a debug flag that allows this. The problem is that objc runtime does not allow deletion of classes or creating a new class with the same name.

mikael

@ryubai, do this:

try:
    Handler
except NameError:
    class Handler(NSObject):
        ...
JonB

That only works if your class is stable -- if you need to change it, you will have to force quit the app.

ryubai

@JonB

thanks.
it seems not convenient for debug, and i still use objc_util as OC bridge.

dgelessus

For reference, @JonB has opened an issue for this feature on the rubicon.objc issue tracker: https://github.com/beeware/rubicon-objc/issues/181