Understanding Mac APIs
- PDF for offline use
Let us know how you feel about this
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
- (BOOL)canDragRowsWithIndexes:(NSIndexSet *)rowIndexes atPoint:(NSPoint)mouseDownPoint
The declaration can be read left to right:
-prefix means it is an instance (non-static) method. + means it is a class (static) method
(BOOL)is the return type (bool in C#)
canDragRowsWithIndexesis the first part of the name.
(NSIndexSet *)rowIndexesis the first param and with it’s type. The first parameter is in the format:
atPoint:(NSPoint)mouseDownPointis the second param and its type. Every parameter after the first is is the format:
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:atPointselector 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):
Use the auto completion list to quickly scan for something of the same name. Since we know it is an instance of
NSTableViewyou can type:
x.[ctrl+space if the list does not appear).
- Right click method, go to declaration to open the Assembly Browser where you can compare the Export attribute to the selector in question
Search the entire class binding. Since we know it is an instance of
NSTableViewyou can type:
- Right click
NSTableView, go to declaration to Assembly Browser
- Search for the selector in question
You can use the Xamarin.Mac API online documentation .
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.