Wednesday, August 27, 2014

Objective C for C# programmer - when you get started

First of all, I would like to set the background context right for this serials of writing. I am an expert level C# programmer with in depth knowledge of OOP concepts and how these concepts are implemented in C#. On the other end, I am relatively new to Objective C, other than worded on C/C++ about 20 years ago. Like many .net programmers, I started working on Windows Store app and Windows Phone App development, but these apps do not generate expected revenue  for me. so I jumped to iOS platform start to migrate my apps  from windows platforms to iOS platforms.

If you are in the similar situation like I am, I hope this serials of writing could help you one way or another. Enjoy.

Similar to any modern programming language, the first thing to do is to get the  IDE. unlike the earlier time of JAVA language, Sun Micros System published Java compiler, language specification and JDK and expected programmer to start using the language with a text editor like notepad in Windows and VI in Unix. 

If you want to do iOS app development, you must to have a Mac OS computer,  and that is all you need. After having your Mac Book, the rest will be given to you for free. you get the best and the only IDE, XCode from apple app store for free. Come along with Xcode, you get the best and the only programming language from Apple, Objective C. Unlike in Microsoft platform, there are many editions of Visual Studio, Express Edition, Personal edition, Professional Edition, Ultimate Edition. there are many programming language you can choose from, VB.NET, C# or F#. 

Unlike Windows operating System which is based on DOS OS, Mac OS is based on Unix OS. you could say, Mac OS is a nice designed UI layer on top of the UNIX Kerner.

Unlike C#, objective C is not a brand new programming language with no legacy to carried with. Objective C is the original C language plus apple’s OOP implementation. so if you copy the C source code you did 20 year ago  in BSD Unix computer and put them in Xcode, your program will still work.

now, let’s get to some programming language elements. if you are working on resonabilty current version of .Net programming language, you most likely find the following language features are missing in Objective C: ( but this would not stop iOS to be the number one mobile platform with the most number of apps on it)

1. Generic
2. Delegate
3. Abstract
3. Name Space
4. Class library project and referencing class library project 
5.LINQ and Lambda
6. Async / Await
7. Method overloading
8. access modifiers like protected, internal

Above all, you will find you miss something you take for granted for so long, “Dot notation”. On  “Dot Notation” apple is doing something towards this direction. at least you can use dot notation on properties. Same as traditional C program language, objective C separate a class into header file and implementation file, in short it calls h file and m file. but in C# you put them in one file. you will find these differences in the first 10 hours when you start working on objective C.


let’s get to some code sample
  
//  SCbaseEntity.h
@interface SCBaseEntity : NSObject

@property (nonatomicint UniqueId;
@property (nonatomicstrongNSString *title;
@property (nonatomicstrongNSString *description;
@property BOOL IsFavorite ;



-(id) initWithUniqueId :(int) UniqueId
        Title : (NSString *) Title
        Description : (NSString *) Description
        IsFavorite : (BOOL) IsFavorite;

@end



//
//  SCbaseEntity.m

#import "SCBaseEntity.h"

@implementation SCBaseEntity


-(id)initWithUniqueId: (int) UniqueId
       Title : (NSString *)Title
       Description:(NSString *)Description
       IsFavorite : (BOOL) IsFavorite;
{
    
    self = [super init];
    if (self)
    {
        self.UniqueId = UniqueId;
        self.description = Description;
        self.title = Title;
        self.IsFavorite = IsFavorite;
    }
   
    return self;
}

@end


The above are the h file and the m file of a simple base class. if I would write them in C#, it would be like the following:


 public abstract  class SCBaseEntity
    {
        public int UniqueId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public  bool IsFavorite { get; set; }


      public SCBaseEntity (int aUniqueId, string aTitle, string aDescription,  bool aIsFavorite )  : base()
      {
          this.UniqueId = aUniqueId;
          this.Title = aTitle;
          this.Description = aDescription;
          this.IsFavorite = aIsFavorite;
      }      
    }

a few notes about the class above:

1. I wanted to make it an abstract class, but found out that in objective C there is no such thing call abstract class, I got to watch the way I used the class
2. @property is like automatic property in .net.  in objective C it is called synthesize. you could synthesize your property to different private variable by using @synthesize statement.   @synthesize UniqueId=_UniqueId;
3. NSString is part of “Objective” implementation, the prefix of “NS” tells that. NSString is String class offered by “Objective” implementation, which is different from the string data type in C.  when you assign string value to NSString you need to use @“string value" 
4. you can defined private property by adding @property statement in m file. but if you want to define protected property or internal property, tyne language does not support that.


let’s look at how this class is instantiated. 

SCModule * module = [[SCModule allocinitWithUniqueId:0
                                                 Title:@"Title"
                                           Description:@"Description"
                                            IsFavorite:YES];

the respective C# code would be like :

 var result = new SCModule(0, "Title", "Description", true);


a few notes here about this line of code:

1. Objective C is a bit wordy, but it has value in readability
2. Objective C separate alloc and init into 2 different methods while in C# it is only one, we call it constructor
3.different from C# using Dot notation, in Objective C, you call a method of a class using [] notation, [objective-name method-name : p1 parameter-name: p2…] and the alignment is very interesting, it align around “:”  I have to say it has its point for doing that, but if you just jumped to Objective C from C# , you would like to go back to Dot Notation in .Net. but as you stay longer in Xcode, you will start to appreciate it more and hopefully eventually fall in love with it.

No comments:

Post a Comment