Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Calling non-instance/class objc functions?
-
Really dumb question here: I understand pretty well at this point how to set-up and call objective-C class methods or instance methods via objc_util, but how do we call straight functions defined somewhere in the various objective-c libraries? Things like:
CGRectMake()
or
UIEdgeInsetsMake()Oddly, I can't find anything in the way of examples of people doing this. Do I have to create the call definition with the descriptor and encoding? Or is there a straightforward way to just call defined functions via ctypes?
-
@shinyformica Never found a standard way but always found a workaround like
CGRect(CGPoint(x,y), CGSize(w, h)) is similar to CGRectMake(x,y,w,h)
-
@cvp yeah...that's what I'm doing...but some functions get pretty messy and ugly, or just aren't possible without defining the necessary structures. I think there must be a way to do this...just a bit more in-depth knowledge of ctypes and objc_util.
-
CGRectMake is not actually a function in the symbol table, I think it is more of a preprocessor thing.
For things like CGRect, which are structures, not objects, you have to define the ctypes structure, then use the ctypes constructor methods, or manually set the fields. You could create your own wrapper functions that set the fields that you want to use, since sometimes there are fields that you can leave set to 0.
-
@JonB I think that @shinyformica hopes to find a way to use functions like without be obliged to define them entirely.
As we can use any method of a class/instance (found by dir). -
def CGRectMake(x,y,w,h): return CGRect((x,y),(w,h))
Basically, you have to define your own wrappers, stick them in a library that you import, along with the various constants that you need, etc, and just import it.See for example
https://github.com/pybee/rubicon-objc/blob/master/rubicon/objc/types.pyNear the end, they define many useful structs, constant structures, and non-exported utility functions. Mostly this is just pure ctypes.
-
@JonB That' what I do when I need such a function but he hoped a better way...
-
The difference is that things like CGRect and the like are structures, not full fledged objects. CGRectMake is not a real function, but an inlined function that gets optimized away by the obj compiler, and so there is nothing to call.
You either have to find someone who has done the work and converted the library to python ctypes lib that you can import, or else do it yourself. Rubicon is a good place to look for much of the the core stuff
-
@JonB got it...thanks for the reply! I figured something like that was the reason...that these were not functions in any sense which could be "called". I can use one of a few ways I've found to do what I need in the cases I've seen.