First Met Objective-C

After understanding the differences between different mobile apps, I decided to go for iphone native app development for numerous reasons. There is only a small problem – I don’t know Objective-C …yet. I took a course in C many years ago and I used C in my database class two years ago. Other than those two encounters with C, I have been using Java for back-end support.

Learning a new programming language is part of the professional growth as a developer and it’s interesting since you are learning another way to present the same logic. When I first looked at Objective-C code, it looked like a new animal, but after reading up some fundamentals online, I actually found a lot of similarities. After all, the main thing of programming is just problem solving regardless of the programming language.

Even though Xcode is not the only IDE available, I would suggest you to start with it. It generates the required ios certificate for development, provides templates for different applications and easy-to-use simulator. It’s the most common used of IDE for ios development so in the learning materials.

Objective-C is a superset of C language so it complies C code. However, C is not object-oriented but Objective-C is with Smalltalk-style messaging. Even if you are new to C programming, you probably heard of these two main specialties of it: memory allocation and pointer.

For those who have C programming experience, the object-oriented Smalltalk-style messaging syntax is probably what makes it look unfamiliar to you. Object-oriented programming is a big topic but an object can be considered as a struct with methods in C. Every object inherits NSObject.

To syntax for initializing an object is:

ClassName *obj = [[ClassName alloc] init];

Objective-C uses square brackets for method call and method call is considered as sending messages to the class. “[ClassName alloc]” allocates memory for the new object from the heap and returns a pointer to the new object then it’s passed in method init for initialization.

Let’s look at these two ways to initialize a new NSDate object:

N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​


N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[[​N​S​D​a​t​e​ ​alloc] init]​;​

The first way is using a NSDate method named date to return a new NSDate object. The second way is the standard way of initializing a new object. The other interesting thing of Objective-C is that parameters are considered as part of the method name in Objective-C, for example, ordinalityOfUnit:inUnit:forDate.

​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​[​N​S​D​a​t​e​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
N​S​C​a​l​e​n​d​a​r​ ​*​c​a​lendar​ ​=​ ​[​N​S​C​a​l​e​n​d​a​r​ ​c​u​r​r​e​n​t​C​a​l​e​n​d​a​r​]​;​
N​S​U​I​n​t​e​g​e​r​ ​d​a​y​ ​=​ ​[​c​a​lendar​ ​o​r​d​i​n​a​l​i​t​y​O​f​U​n​i​t​:​N​S​D​a​y​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​  ​      ​i​n​U​n​i​t​:​N​S​M​o​n​t​h​C​a​l​e​n​d​a​r​U​n​i​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​​     f​o​r​D​a​t​e​:​n​o​w​]​;​ 

That’s all for the first blog post on Objective-C and I will post more about it as I continue to learn more:)

Leave a Reply

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