Toolroom Tech Blog

Devlopers Digest

Objective-C versus C# Syntax

A few major differences between C# and Objective-C

You are .net developer and think about starting iDevice development? Ok, have fun ... It's different. It's nearly impossible to show all differences between C# and Objective-C. I will just provide you with a very few important things to know. 

  • Basically, Objective-C is unmanaged, so the developer is responsible for memory management. Why basically? There is a garbage collector is available since 2010, but not for older versions of OS X and iOS and it seems the GC dramatically decreases the app performance.
  • Objective-C is a superset of C, therefore it requires a header file 'LotteryEntry.h' (aka interface file) to declare variables and methods. Those methods are then implemented in the implementation file 'LotteryEntry.m'. 
  • The syntax is in the style of Smalltalk... 
  • OC has no properties like C#, instead apple has a accessor guidlance. I.e. to provide accessors for the variable firstNumber, we need to provide a get-method called firstNumber and a set-method called setFirstNumber.
  • The Cocoa API is very simple and easy. It is really not what we can call voluminous.

In the following example I will create a simple .NET class with two properties and one overridden method. Then, to give you an idea how Objective-C Looks like, I'll show the same class translated to Objective-C.

Due to the smalltalk syntax, Objective-C looks crazy for C# or Java developers. But here a few things to know before you start reading the code.

Syntax

Objective-C

C#

- (NSString *)getString:(NSString *)myString
{
    return myString;
}
public string GetMyString(string myString)
{
    return myString;
}
[self getString:anyDefinedString];
this.GetMyString(anyDefinedString);
NSDate *now = [[NSDate alloc] init];
var now = new DateTime();

C#

using System;

namespace Dweedo.Web.Common
{
    public class LotteryEntry
    {
        readonly Random _rand = new Random();

        public LotteryEntry(DateTime? entryDate = null)
        {
            EntryDate = entryDate jQuery15203358672992216089_1349763670826 DateTime.Now;

            FirstNumber = _rand.Next(1, 101);
            SecondNumber = _rand.Next(1, 101);
        }

        public int FirstNumber { get; private set; }

        public int SecondNumber { get; private set; }

        public DateTime EntryDate { get; set; }

        public override string ToString()
        {
            return string.Format("{0} = {1} and {2}",
                                 EntryDate.ToShortDateString(),
                                 FirstNumber,
                                 SecondNumber);
        }
    }
} 

Objective-C

Header

#import <Foundation/Foundation.h>

@interface LotteryEntry : NSObject {
    NSDate *entryDate;
    int firstNumber;
    int secondNumber;
}

- (id)initWithEntryDate:(NSDate *)date;
- (void)setEntryDate:(NSDate *)date;
- (NSDate *)entryDate;
- (int)firstNumber;
- (int)secondNumber;

@end

Implementation 

#import "LotteryEntry.h"

@implementation LotteryEntry

- (id)init{
    return [self initWithEntryDate:[NSDate date]];
}

- (id)initWithEntryDate:(NSDate *)date{
    if(![super init]) return nil;
    
    [self setEntryDate:date];
    firstNumber = random() % 100 +1;
    secondNumber = random() % 100 +1;
    
    return self;
}

- (void)setEntryDate:(NSDate *)date{
    entryDate = date;
}

- (NSDate *)entryDate{
    return entryDate;
}

- (int)firstNumber{
    return firstNumber;
}

- (int)secondNumber{
    return secondNumber;
}

- (NSString *)description{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    return [[NSString alloc] initWithFormat:@"%@ = %d and %d",
              [dateFormatter stringFromDate:entryDate],
              firstNumber,
              secondNumber];
}

@end
Loading