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.
help about return type
-
i created a new class and add a function to methods.
but it showed such error.
how can i return cgsize correctly?
thanks in advcance.TypeError: invalid result type for callback function
def collectionView_layout_sizeForItemAtIndexPath_(_self, _cmd, _cv, _la, _ip):
return CGSize(340,300)methods = [collectionView_layout_sizeForItemAtIndexPath_]
DataSource = create_objc_class('DataSource', methods=methods, protocols=['UICollectionViewDelegateFlowLayout', 'UICollectionViewDataSource', 'UICollectionViewDelegate'])
-
You need to specify the superclass that your custom class inherits from. This will let you override the method you have and infer the correct return type
-
-
@ryubai
Yes it’s default is NSObject but if you’re trying to overwrite a class method you need to specify that class, or your controller won’t reference your function.Is this the only bit of code you have? You must have a controller? You need to overwrite the controller itself.
-
@mcriley821, defining delegates based on NSObject is normal, and
UICollectionViewDelegateFlowLayout
protocol should be enough to define the return type forcollectionview:layout:sizeForItemAtIndexPath:
.I agree there must be a lot of code that we are not seeing, e.g. covering the other protocols listed, but I get the error with this isolated piece of code:
import objc_util def collectionView_layout_sizeForItemAtIndexPath_(_self, _cmd, _cv, _la, _ip): return objc_util.CGSize(349,300) methods = [collectionView_layout_sizeForItemAtIndexPath_] AnotherName = objc_util.create_objc_class( 'AnotherName', methods=methods, protocols=[ 'UICollectionViewDelegateFlowLayout' ] )
-
@ryubai, let’s see if we can wake the big bears (@JonB, @dgelessus).
-
@ryubai CGSize is not an object, it is a structure. You need to make your return type a CGSize Structure, not a c_void_p
-
I think there is a way to specify argtypes and restype when you create the class/method?
-
thank you all
when i overwrite a method requested a return type in a new class.
number and string can be returned directly.
cell object in tableview is returned in cell.ptr
but how to return a CGSize structure?
objc's CGSize does't work -
-
anyone can help? please
-
@ryubai, I tried a few things:
- confirming that defining another method of the same protocol that returns a float works fine
- looking a the definition of CGSize in objc_util
- fiddling with the retype of the function
- using CGSizeMake function, but could not get it defined
- checking if ctypes.Structure would have some additional capabilities
No help nowhere.
-
okay, the issue here is that ctypes does not allow complex return types, such as Unions or Structures for callbacks.
see
https://bugs.python.org/issue5710However, @dgelessus actually came up with a solution.
https://github.com/beeware/rubicon-objc/pull/85/filesyou need to download ctypes_patch.py and place in site-packages (or, you can use this executable link
Then, you just need to call
import ctypes_patch ... ctypes_patch.make_callback_returnable(CGSize) DataSource = create_objc_class('DataSource', methods=methods, protocols=['UICollectionViewDelegateFlowLayout', 'UICollectionViewDataSource', 'UICollectionViewDelegate'])
-
Sincerely, what could we do without @dgelessus and @JonB
Thanks to be here. -
i updated the exec link, to download and install the patch into site-packages. click the link, then click run.
-
-
run more than once will raise such errs,then how to solve?
ctypes_patch.make_callback_returnable(CGSize)
File "/private/var/mobile/Containers/Shared/AppGroup/02422888-CC52-45FC-BB53-72013D3F4AFA/Pythonista3/Documents/site-packages/ctypes_patch.py", line 128, in make_callback_returnable
raise ValueError("The ctype {} already has a getfunc")
ValueError: The ctype {} already has a getfunc -
Probably you can just use a try/except around that, or explicitly check for the existence of a getfunc.
-
Sorry for the late reply - I forgot to check the forum recently... The version of ctypes_patch that @JonB linked to is the original version from the pull request that added it. There have been some updates to it that fix a few problems, including the "The ctype {} already has a getfunc" error that @ryubai posted above. I would recommend using the current version of ctypes_patch from the repo and not the old one from the pull request.
-
This post is deleted!