omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    7
    21
    6877
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ryubai
      ryubai last edited by

      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'])

      JonB 1 Reply Last reply Reply Quote 0
      • mcriley821
        mcriley821 last edited by

        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 1 Reply Last reply Reply Quote 0
        • ryubai
          ryubai @mcriley821 last edited by

          @mcriley821

          it still can’t work.

          superclass=NSObject is default when new class is created.

          mcriley821 mikael 2 Replies Last reply Reply Quote 0
          • mcriley821
            mcriley821 @ryubai last edited by

            @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.

            mikael 1 Reply Last reply Reply Quote 0
            • mikael
              mikael @mcriley821 last edited by mikael

              @mcriley821, defining delegates based on NSObject is normal, and UICollectionViewDelegateFlowLayout protocol should be enough to define the return type for collectionview: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'
                  ]
              )
              
              
              1 Reply Last reply Reply Quote 0
              • mikael
                mikael @ryubai last edited by

                @ryubai, let’s see if we can wake the big bears (@JonB, @dgelessus).

                1 Reply Last reply Reply Quote 1
                • JonB
                  JonB @ryubai last edited by

                  @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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @JonB last edited by

                    I think there is a way to specify argtypes and restype when you create the class/method?

                    ryubai 1 Reply Last reply Reply Quote 0
                    • ryubai
                      ryubai @JonB last edited by

                      @JonB

                      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

                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @ryubai last edited by

                        @ryubai did you read this , it is to complex for me 😢

                        1 Reply Last reply Reply Quote 0
                        • ryubai
                          ryubai last edited by

                          anyone can help? please

                          mikael 1 Reply Last reply Reply Quote 0
                          • mikael
                            mikael @ryubai last edited by

                            @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.

                            1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by JonB

                              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/issue5710

                              However, @dgelessus actually came up with a solution.
                              https://github.com/beeware/rubicon-objc/pull/85/files

                              you 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'])
                              
                              ryubai 1 Reply Last reply Reply Quote 3
                              • cvp
                                cvp last edited by cvp

                                Sincerely, what could we do without @dgelessus and @JonB
                                Thanks to be here.

                                1 Reply Last reply Reply Quote 0
                                • JonB
                                  JonB last edited by

                                  i updated the exec link, to download and install the patch into site-packages. click the link, then click run.

                                  1 Reply Last reply Reply Quote 0
                                  • ryubai
                                    ryubai last edited by

                                    @JonB @mikael @cvp and other dear friends

                                    Sincerely,many thanks to you all!
                                    i love to stay here.

                                    1 Reply Last reply Reply Quote 0
                                    • ryubai
                                      ryubai @JonB last edited by

                                      @JonB

                                      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

                                      1 Reply Last reply Reply Quote 0
                                      • JonB
                                        JonB last edited by

                                        Probably you can just use a try/except around that, or explicitly check for the existence of a getfunc.

                                        1 Reply Last reply Reply Quote 0
                                        • dgelessus
                                          dgelessus last edited by

                                          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.

                                          1 Reply Last reply Reply Quote 1
                                          • eliotradisa
                                            eliotradisa last edited by

                                            This post is deleted!
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors