Declaring Cocoa functions

  1. 2 years ago

    Hi,

    I have been developing macOS desktop application with Xojo for over a year now. I use different librairies such as MBS, ..

    But I want to use functions that are not in theses librairies but are in the Cocoa framework.

    Is there any documentation out there that explains how to do it ?

    For example, I want to use the function NSMinX from Foundation (doc here: https://developer.apple.com/documentation/foundation/1391134-nsminx ), how can I proceed?

    Thanks

    Julien

    For a general introduction, see this thread where Jim gives a great short introduction into declares:
    https://forum.xojo.com/13244-nssearchfield
    I can send you additional stuff if you like to send me your mail address privately.

    About your question: NSMinX is a function of the framework, not of a class which is a special case Jim did not cover I believe.
    In these cases you need no selector. The declare would rather be

    Declare Function NSMinX lib "Foundation.framework" (aRect as NSRectMBS) As CGFloat
  2. Ulrich B

    29 Jun 2017 Pre-Release Testers, Xojo Pro Answer Europe (Germany, Berlin) · xo...
    Edited 2 years ago

    For a general introduction, see this thread where Jim gives a great short introduction into declares:
    https://forum.xojo.com/13244-nssearchfield
    I can send you additional stuff if you like to send me your mail address privately.

    About your question: NSMinX is a function of the framework, not of a class which is a special case Jim did not cover I believe.
    In these cases you need no selector. The declare would rather be

    Declare Function NSMinX lib "Foundation.framework" (aRect as NSRectMBS) As CGFloat
  3. Paul L

    29 Jun 2017 Xojo Inc http://docs.xojo.com
    Edited 2 years ago

    NSMinX is a simple one to map to Xojo (if you have NSRectMBS), as Ulrich shows. Others that require classes can get more involved.

    This blog post describes using Declares to set the badge number for an iOS app. The Declare technique is similar for macOS apps:
    http://blog.xojo.com/2016/06/17/set-ios-app-icon-badge-number/

    And also see this post:
    http://blog.xojo.com/2014/09/15/using-declares-in-your-multi-platform-desktop-apps/

    Other info:
    http://developer.xojo.com/mac-declare-samples
    http://developer.xojo.com/webinar-creating-ios-declares

  4. shao s

    29 Jun 2017 Pre-Release Testers Sudbury, Ontario, Canada

    And if you don't have the MBS plugins, or want to do everything in Xojo, just use the following structure..

    Structure NSRect
      X As Single
      Y As Single
      Width As Single
      Height As Single
    End Structure
  5. Eli O

    is not verified 29 Jun 2017 Europe (Berlin, Germany)

    @shao s Structure NSRect
    X As Single
    Y As Single
    Width As Single
    Height As Single
    End Structure

    As far as I know one should use CGFloat instead of Single for NSRect to be able to compile for both 32Bit and 64Bit:

    Structure NSRect
      x As CGFloat
      y As CGFloat
      width As CGFloat
      height As CGFloat
    End

    Also note that some very basic functions in Cocoa/iOS (like NSMakeRect) cannot be used through declares, because these functions are "inlined" when Apple compiles the frameworks (meaning the compiler replaces every function call with the code of the function and then strips the function entirely from the frameworks). You can easily create your own version in Xojo:

    Function NSMakeRect(x As CGFloat, y As CGFloat, w As CGFloat, h As CGFloat) As NSRect
      Dim rect As NSRect
      rect.x = x
      rect.y = y
      rect.widh = w
      rect.height = h
      Return rect
    End
  6. Thanks for the answers.

  7. Christian S

    1 Jul 2017 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

    I added NSMinX to my NSRectMBS class.

  8. 5 months ago

    Ulrich B

    Dec 12 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    Oneandahalf years later I wonder: Did that declare ever work?
    I need it currently and Xojo will not link it – function not found.
    Testing back to 2017r2 which is the oldest version I have installed always returned the same error. NSMinX is not a compiler macro – Christian, how did you implement it?

  9. @Ulrich B NSMinX and the like are implemented as inline functions so they are not like other functions. Moreover, they are extremely trivial code so you'd better implement it directly in Xojo. You can find the code for those functions in NSGeometry.h in the Foundation.framework.

    For example, NSMinX is simply defined as:

    NS_INLINE CGFloat NSMinX(NSRect aRect) {
        return (aRect.origin.x);
    }

    so it just returns the x value of a NSRect

  10. Ulrich B

    Dec 14 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...

    Thanks a lot! I even made a full text search on my disk but could not find the right header file.

  11. Sam R

    Dec 14 Pre-Release Testers, Xojo Pro Hengchun, Pingtung, Taiwan

    @Stéphane ;Mons NSMinX and the like are implemented as inline functions so they are not like other functions. Moreover, they are extremely trivial code so you'd better implement it directly in Xojo. You can find the code for those functions in NSGeometry.h in the Foundation.framework.

    I've heard of these "functions" referenced as Macros before and the way I understand them is that they're not part of the Foundation framework. I think (although don't know for sure) they they basically get included in your application automatically when you use them.

  12. Jason P

    Dec 15 Xojo Inc Texas
    Edited 5 months ago

    @Ulrich B Thanks a lot! I even made a full text search on my disk but could not find the right header file.

    Unless it searches right down in to the guts of the Xcode app bundle it might not find it. Norm tells me his BBEdit multi file search set is looking in /Applications/Xcode.app/Developer/Platforms for Xcode 9 and in a similar location for Xcode 10.

  13. Sam R

    Dec 15 Pre-Release Testers, Xojo Pro Hengchun, Pingtung, Taiwan

    @Jason P Unless it searches right down in to the guts of the Xcode app bundle it might not find it. Norm tells me his BBEdit multi file search set is looking in /Applications/Xcode.app/Developer/Platforms for Xcode 9 and in a similar location for Xcode 10.

    I used to use Find Any File, but recently I seem to know where to start looking...

  14. @Sam R — The NSMinX and the like are not macros (though the effects are similar) but inline functions. It means that the code they contain is inserted at compilation time where you use them instead of the normal call...return stuff.

    Because the code is copied each time you use the inline function, they should be very small functions.

  15. Ulrich B

    Dec 16 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...
    Edited 5 months ago

    @Stéphane ;Mons The NSMinX and the like are not macros

    That’s what mislead me into believing they should be available as usual declares. Real macros have a Macro tag in Apple’s documentation, these don’t. Thanks for clearing that up, Stéphane! Time for the second green checkmark in this thread. ;)

  16. If it's simple, it's not C

or Sign Up to reply!