Saturday, February 4, 2017

Objective-C Quick Guide

Objective-C Overview

Objective-C is general-purpose language that is developed on top of C Programming language by adding features of Small Talk programming language making it an object-oriented language. It is primarily used in developing iOS and Mac OS X operating systems as well as its applications.

Initially, Objective-C was developed by NeXT for its NeXTSTEP OS from whom it was taken over by Apple for its iOS and Mac OS X.

Object-Oriented Programming

Fully supports object-oriented programming, including the four pillars of object-oriented development:
  • Encapsulation
  • Data hiding
  • Inheritance
  • Polymorphism

Example Code

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NSLog (@"hello world");
   [pool drain];
   return 0;
}

Foundation Framework

Foundation Framework provides large set of features and they are listed below.
  • It includes a list of extended datatypes like NSArray, NSDictionary, NSSet and so on.
  • It consists of a rich set of functions manipulating files, strings, etc.
  • It provides features for URL handling, utilities like date formatting, data handling, error handling, etc.

Learning Objective-C

The most important thing to do when learning Objective-C is to focus on concepts and not get lost in language technical details.
The purpose of learning a programming language is to become a better programmer; that is, to become more effective at designing and implementing new systems and at maintaining old ones.

Use of Objective-C

Objective-C, as mentioned earlier, is used in iOS and Mac OS X. It has large base of iOS users and largely increasing Mac OS X users. And since Apple focuses on quality first and its wonderful for those who started learning Objective-C.

Objective-C Environment Setup

Try it Option Online

You really do not need to set up your own environment to start learning Objective-C programming language. Reason is very simple, we already have set up Objective-C Programming environment online, so that you can compile and execute all the available examples online at the same time when you are doing your theory work. This gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.
Try the following example using Try it option available at the top right corner of the below sample code box:
#import <Foundation/Foundation.h>

int main()
{
   /* my first program in Objective-C */
   NSLog(@"Hello, World! \n");
   
   return 0;
}
For most of the examples given in this tutorial, you will find Try it option, so just make use of it and enjoy your learning.

Local Environment Setup

If you are still willing to set up your environment for Objective-C programming language, you need the following two softwares available on your computer, (a) Text Editor and (b) The GCC Compiler.

Text Editor

This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.
Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.
The files you create with your editor are called source files and contain program source code. The source files for Objective-C programs are typically named with the extension ".m".
Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, compile it and finally execute it.

The GCC Compiler

The source code written in source file is the human readable source for your program. It needs to be "compiled" to turn into machine language, so that your CPU can actually execute the program as per instructions given.
This GCC compiler will be used to compile your source code into final executable program. I assume you have basic knowledge about a programming language compiler.
GCC compiler is available for free on various platforms and the procedure to set up on various platforms is explained below.

Installation on UNIX/Linux

The initial step is install gcc along with gcc Objective-C package. This is done by:
$ su - 
$ yum install gcc
$ yum install gcc-objc
The next step is to set up package dependencies using following command:
$ yum install make libpng libpng-devel libtiff libtiff-devel libobjc libxml2 libxml2-devel libX11-devel libXt-devel libjpeg libjpeg-devel
In order to get full features of Objective-C, download and install GNUStep. This can be done by downloading the package from http://main.gnustep.org/resources/downloads.php.
Now, we need to switch to the downloaded folder and unpack the file by:
$ tar xvfz gnustep-startup-.tar.gz
Now, we need to switch to the folder gnustep-startup that gets created using:
$ cd gnustep-startup-
Next, we need to configure the build process:
$ ./configure
Then, we can build by:
$ make
We need to finally set up the environment by:
$ . /usr/GNUstep/System/Library/Makefiles/GNUstep.sh
We have a helloWorld.m Objective-C as follows:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog (@"hello world");
    [pool drain];
    return 0;
}
Now, we can compile and run a Objective-C file say helloWorld.m by switching to folder containing the file using cd and then using the following steps:
$ gcc `gnustep-config --objc-flags` -L/usr/GNUstep/Local/Library/Libraries -lgnustep-base helloWorld.m -o helloWorld
$ ./helloWorld
We can see the following output:
2013-09-07 10:48:39.772 tutorialsPoint[12906] hello world

Installation on Mac OS

If you use Mac OS X, the easiest way to obtain GCC is to download the Xcode development environment from Apple's web site and follow the simple installation instructions. Once you have Xcode set up, you will be able to use GNU compiler for C/C++.
Xcode is currently available at developer.apple.com/technologies/tools/.

No comments:

Post a Comment