Translating Objective-C to C#: Introduction

In my limited experience with mobile development using Xamarin.iOS it has often occurred that searching for sample code to implement some feature produced pages of results in Objective-C but few, if any, using C#/Xamarin.iOS. The Xamarin guides are great, but they can not possibly cover all of the possible scenarios that have come up for Objective-C/CocoaTouch developers since the first iPhone was introduced. In other words, there is a vast library of information out there, including the ultimate reference, Apple’s docs, that is sometimes just out of reach for the experienced C#/.NET developer because of the language barrier. We know that fundamentally both languages and frameworks should be capable of doing the same things. The three basic control structures, which formed the basis of programming, are present in every language, and use very similar, often identical, syntax in all languages. Similarly in spoken language many of our most basic words, like “no,” are extremely similar, sometimes identical, in different languages. Spoken language has been around for millennia, programming languages only about 70 years or so, thus far greater variety in spoken language is to be expected. Everything evolves over time.


There is a concept in spoken language that is called the false cognate. This describes when a word in one language sounds and/or looks very much like a word in another language, but they mean very different things. As an example I recall when my brother-in-law was visiting from Spain. He spoke virtually no English and I just a little Spanish, enough for me to find my way around and not get punched when in Spain. We were sitting in my living room, communicating as best we could and doing a decent job. He looks at a piece of art I have on the wall, a print of a charcoal sketch by Picasso. Nothing fancy, not a lithograph or anything like that, just a nice, textured print. But he points to it and says, “Es original.” I assumed that he thought it was an original Picasso. The ‘g’ is pronounced more like an ‘h,’ but it was close enough and I knew it was spelled the same as “original.” I assured him it wasn’t. “No es original,” I replied. He refuted me, insisting that it was indeed “original.” My sister in the other room overheard and finally explained that “original” in Spanish means “cool,” or “hip.” He laughed when he realized what I thought he meant. He could’t understand why I was telling him what to think about my facsimile of a piece of art.

There are some of these false cognates between Objective-C and C#. Most notably “delegates,” “protocols,” and “interfaces.” What the C# world would call an interface is loosely analogous to a protocol in Objective-C. What the C# world would call a delegate, well, there is no direct correlation but similar functionality can be achieved with  blocks. Likewise there is not a direct correlation in C# for an Objective-C delegate, or rather they are just referred to as event handlers. So what are all these things?

In C# a delegate is a variable of sorts that can hold a reference to a function or method thus allowing you to pass the delegate to another function as an argument, often to provide a callback function or event handler to the receiving function/method. This can be done with blocks in Objective-C, though they seem to be not quite as flexible as C# delegates.

In Objective-C a delegate is nothing more than a class that will handle events coming from an object. If you assign objectA to be the delegate of objectB, any events generated by objectB will be handled by objectA. So obviously objectB must be some sort of event generating object, like a UI control or scrollable view.  C# does not have a fancy name for the equivalent of Objective-C delegates, or at least I have not run across one yet, they tend to just be called event handlers.

Objective-C delegates conform to protocols. The protocol defines the required and optional methods that a particular delegate can implement. You implement the protocol methods that you need to handle the events that you need to handle. A protocol in Objective-C is similar to an interface in C#, but in C# interfaces there are no optional methods. The class implementing the interface must implement all of the methods of the interface.

In Objective-C an interface is, well, it isn’t really anything. @interface is the keyword used to declare a class in the Objective-C header file. In objective-C, as in C/C++, a class is declared in one file, the header, and implemented in another, the implementation file. These have the extensions .h and .m respectively. The closest C# relative of the Objective-C keyword “@interface” is the C# keyword “class.” Of course in C# we don’t typically split the declaration and the implementation of a class, so the keyword pair “partial class” is actually closer to “@interface” than just “class.”

Keep these “false cognates” in mind as I progress through this series of posts.

With that, I give you my first, tiny, code snippet for this series, declaring and implementing a class in Objective-C. This particular class inherits from the base class UIViewController. That syntax should be familiar to you. 🙂

 

The .h header file:

@interface MyViewController : UIViewController
{
    // member variables here
}
    // properties and methods here
@end

 

The .m implementation file:

@implementation MyViewController
    // Implement the accessors and methods of this class.
    // You must implement every property and method declared in the header.
@end

 

And of course, in C#:

 class MyViewController : UIViewController
{
    // Declare and implement all members, properties, and methods here
}

 

Armed with this basic information, I am going to continue this opening blog series with examples of the ways to translate these concepts from one language to the other. My next post is going to be an overview of Objective-C methods and how the syntax of creating and calling an Objective-C method compares to C# methods.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.