Translating Objective-C to C#: Methods

In this second installment, I am going to go over methods in Objective-C and how they “translate” to methods in C#.

Something you will note is that in there are no “private” or “public” keywords in Objective-C. All methods are public if declared in the header file. You can define a method in the implementation file only, but the compiler will complain a bit about that. You can avoid it by using what is called a class extension in Objective-C, but that is off topic for now and does not affect how to understand an Objective-C method.

You may have seen the term “selector” when reading about Objective-C, or even had an exception thrown by the runtime that says “invalid selector sent to …” or similar message. So what is a selector? A selector is like a method signature. In Objective-C you send objects messages. These messages are selectors. For instance, in C# you might have a method:

public void Bar() {...}

In Objective-C that would be:

- (void)bar {...}

If you wanted to invoke the “bar” method, you would send the message “bar” (that’s the selector) to an instance of the above class. If I had an instance of that class named “foo”, to invoke that method I would use:

[foo bar];

Now let’s add an argument. The C# way:

public void Bar(string stringArg) {...}

And in Objective-C:

- (void)bar:(NSString *)stringArg {...}

Now if I want to call the above method, I have to pass an argument. The selector is “bar:” (note the colon, it means there is an argument required), so to send that message to an object named “foo”:

[foo bar:@"someNSstring"];

Which passes the NSString literal “someNSstring” to foo’s bar method as the parameter  “stringArg”. The @  sign is a shortcut and basically says “make a new NSString with the following.” This is another “false cognate” of sorts. The @ in C# means “escape the following string” and so this can confuse the C# developer reading Objective-C.

The same method call above in C#, using a C# string of course, would be:

foo.Bar("someC#string");

When you add additional arguments, you add to the selector. Consider:

- (void)bar:(NSString *)bar withFloat:(NSFloat *)floatNumber {...}

Now the selector is “bar:withFloat:”, so calling the method on an object named foo becomes:

[foo bar:@"someNSstring" withFloat:100];

Objective-C seems to be more lenient about number types and not having to use the “f”, as in 100.0f, to represent a float.

The above in C#:

foo.Bar("someC#string", 100.0f);

Now you may be wondering what that “-” sign is in front of the Objective-C method. Simply, “-” means it is an instance method, “+” means it is a static method, so:

+ (void)bar:(NSString *)bar withFloat:(NSFloat *)floatNumber {...}

could be called on the class itself rather than on an instance of the class, just like a C# static method.

To break an Objective-C method down into its parts:

+ (void)bar:(NSString *)bar withFloat:(NSFloat *)floatNumber
1   2    3      4        5      6         7          8

1. Static or instance method (+ is static, – is instance)
2. Return type.
3. Start of selector name. Won’t have a colon if there are no arguments/parameters.
4. Type of first argument/parameter.
5. Argument/parameter name. This is the name you would use in your method to access that parameter.
6. Continuation of selector name. This gives an indication of what is expected for that argument.
7. Type of second argument/parameter.
8. Argument/parameter name.

As for those asterisks, will you likely know that that means a “pointer,” meaning “bar” and “floatNumber” are pointers to the NSString and NSFloat objects respectively. You wouldn’t use the asterisk for basic types or structures.

Well that’s it for this installment. Of course this is a basic overview, but it should help to decipher some of what you see when you look at Objective-C code. Next up, instance members, properties, and accessors.

 

Leave a Reply

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