Declaring Cocoa functions

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

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

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

[quote=338302:@shao sean]Structure NSRect
X As Single
Y As Single
Width As Single
Height As Single
End Structure[/quote]
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

Thanks for the answers.

I added NSMinX to my NSRectMBS class.

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?

@Ulrich Bogun 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

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

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.

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…

@Sam Rowlands — 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.

Thats what mislead me into believing they should be available as usual declares. Real macros have a Macro tag in Apples documentation, these dont. Thanks for clearing that up, Stphane! Time for the second green checkmark in this thread. :wink:

If it’s simple, it’s not C