Understanding Mac APIs

PDF for offline use

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-03

For much of your time developing with Xamarin.Mac, you can think, read, and write in C# without much concern with the underlying Objective-C APIs. However, sometimes you’ll need to read the API documentation from Apple, translate an answer from Stack Overflow to a solution for your problem, or compare to an existing sample.

Reading enough Objective-C to be dangerous

Sometimes it’ll be necessary to read an Objective-C definition or method call and translate that to the equivalent C# method. Let’s take a look at an Objective-C function definition and break down the pieces. This method (a selector in Objective-C) can be found on NSTableView:

- (BOOL)canDragRowsWithIndexes:(NSIndexSet *)rowIndexes atPoint:(NSPoint)mouseDownPoint

The declaration can be read left to right:

  • The - prefix means it is an instance (non-static) method. + means it is a class (static) method

  • (BOOL) is the return type (bool in C#)

  • canDragRowsWithIndexes is the first part of the name.

  • (NSIndexSet *)rowIndexes is the first param and with it’s type. The first parameter is in the format: (Type) pararmName

  • atPoint:(NSPoint)mouseDownPoint is the second param and its type. Every parameter after the first is is the format: selectorPart:(Type) pararmName

  • The full name of this message selector is: canDragRowsWithIndexes:atPoint:. Note the : at the end - it is important.

  • The actual Xamarin.Mac C# binding is: bool CanDragRows (NSIndexSet rowIndexes, PointF mouseDownPoint)

In this selector invocation you can read the same way:

[v canDragRowsWithIndexes:set atPoint:point];
  • The instance v is having the canDragRowsWithIndexes:atPoint selector called with two parameters set and point passed in.

  • The method invocation looks like x.CanDragRows (set, point); in C#.

Finding the C# member for a given selector

Now that you’ve found the Objective-C selector you need to invoke, the next step is mapping that to the equivalent C# member. There are four approaches you can try (continuing with the NSTableView CanDragRows example):

  1. Use the auto completion list to quickly scan for something of the same name. Since we know it is an instance of NSTableView you can type:

    • NSTableView x;
    • x. [ctrl+space if the list does not appear).
    • CanDrag [enter]
    • Right click method, go to declaration to open the Assembly Browser where you can compare the Export attribute to the selector in question
  2. Search the entire class binding. Since we know it is an instance of NSTableView you can type:

    • NSTableView x;
    • Right click NSTableView, go to declaration to Assembly Browser
    • Search for the selector in question
  3. You can use the Xamarin.Mac API online documentation .

  4. Miguel provides a "Rosetta Stone" view of the Xamarin.Mac APIs here that you can search through for a given API. If your API is not AppKit or macOS specific, you may find it there.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.