Translating Objective-C to C#: Instance Members, Properties, and Accessors

In the last installment, I showed you how Objective-C methods are created and called as compared to how the same is done in C#. In this installment, I will cover other elements of a class, the instance members, properties, and accessors.

As I showed in the first post in this series, this is a basic class declaration in Objective-C:

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
}

A member, or instance, variable in Objective-C will only be available to an instance of the class similar to a private member variable in C#. Once declared in the header file, you do not need to do anything in the implementation, .m, file to access that member in your class methods. The below shows a member variable declaration:

@interface MyViewController : UIViewController
{
    // member variables here
    NSString *_title;
    float index;
}
@end

Notice the asterisk before _title. This is because NSString is an object so _title is a pointer to that object. There is no asterisk for index since this is a basic type and thus the variable holds the value rather than a pointer to the memory location that has the value.

Now on to the slightly more complex properties and accessors. In Objective-C, you can create a property, member variable, and accessors in the same way that you can in C#, with different syntax of course. In C#:

public class MyClass
{
    public string Title {get; set}
}

does all that for you. Creates a member variable that can be retrieved and set with the property. And of course you can expand what is done in the getter and setter accessor in the {get;set;} block. The same thing can be achieved with the @property and @synthesize keywords in Objective-C. The following:

@interface MyViewController : UIViewController
{
}
@property (nonatomic, strong) NSString *title;
@end

in your header, .h, file will declare the property, and then in your implementation file, .m:

@implementation MyViewController : UIViewController

@synthesize title;

@end

the @synthesize keyword will create a member variable and a setter and getter for you. The selector name for the setter and getter will be setTitle: and title respectively and these would be the messages you would send to the object to get or set the property:

[object title];

[object setTitle:@"Title String"]

These setters and getters that are generated by the @synthesize keyword can be overridden simply by implementing methods with the same selector:

@implementation MyViewController : UIViewController

@synthesize title;

- (NSString *)title
{
    // code to retrieve the title property
    return title;
}

- (void)setTitle:(NSString *)t
{
   // code to set the title property
   title = t;
}

@end

The above is identical to the default setter and getter created by the @synthesize keyword in the implementation. If you don’t use the @synthesize keyword, then you will also have to create a member variable to hold the value of the property. Usually this would take the  name _title as in the first example above for declaring a member variable.

This all seems very similar to how C# behaves with members, properties, and accessors. Just remember that when you see something like [object setTitle:@”new title”]  or [object title] in Objective-C, you will likely use something like object.Title = “new title” or object.Title in C# once wrapped in Xamarin.iOS goodness.

Next up: Objective C blocks vs. C# delegates.

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.