Objective-C Programming Language Help For Students

What is Objective-C?

Objective-C Programming Logo

Objective-C is extension to C and purely Object-Oriented (oo) based programming language. It was developed in 1980s by Brad Cox. The language was based on SmallTalk-80 which is extremely pure in OO sense and everything is done with the help of objects and communication is done via objects only. There is no support for primitive data type like (no int, no Boolean etc) no support for control structure as well (no for, while , if etc). Apple use Objective-C programming language for ioS operating system which is derived from NeXTSTEP operating system as a main language. It also uses respective APIs like Cocoa and Cocoa Touch. Apple added number of features to Objective C language which extends its functionality parallel with the other languages and major update was to be done in Objective-C 2.0.Objective C source code files usually have .m extension and for header or interface files have .h extension. 

Classes in Objective-C

Creating a className and creating objects of that className

Main purpose it serve is to add object orientation to the C Programming language in which classes are the central part which support OO and often called user defined data-types. Basically a className combines data representation and manipulation into one neat package.

Objective-C is Superset of C

Characteristics of a className in Objective-C

className is defined in two different sections @interface and @implementation and hide the object’s implementation. Everything is to be done in form of objects which contain instance variables and receive messages often called as receivers. Properties are used to provide to access className instance variable in other classes.

className Definition

Definition start with the @interface keyword which name of the className and then className body which is enclosed by curly braces. Example define the Box data type using className keyword:

{`
  @interface cuboid:NSObject
  {
  //Instance variables
  double length;   // Length of a cuboid
  double breadth;  // Breadth of a cuboid
  }
  @property(nonatomic, readwrite) double height; // Property
  @end
  `}

Objects Initializing and allocation in Objective-C

A className provide blue prints for objects so an object is an instance of a className. object Declaration of className cuboid:

{`
  Box cuboid1 = [[Box alloc]init];     // Create box1 object of type cuboid
  Box cuboid2 = [[Box alloc]init];     // Create box2 object of type cuboid
  `}

Most common objects in Objective-C

  • NSSet: NSSet represents static, unordered and distinct collection of objects. It is optimized way for membership checking so if you using a lot of group of object parts then you should use Set not Array. You cannot add or remove element from a set because NSSet is immutable but can alter mutable objects.
  • NSArray: NSArray is general-purpose array type in Objective-C which represents an ordered collection of objects and provide high-level interface for sorting manipulating list of data. It records the order of their elements but still they are not efficient as sets. NSArray is immutable like NSSet so dynamically addition and removing of elements is not possible.
  • NSDictionary: This is the way to easy lookups for objects in the dictionary by using key of some sort. Stores the data in “key value pair”. Associate with the each value of key which acts like a label for that value. Why we use because it is useful for modelling relationships between pairs of objects. To store dynamically you can use NSImmutableDictionary.
  • NSString: NSString is the basic tool to represent any text in Objective-C. It is also provide powerful methods for searching a specific string and manipulate it contents. This is immutable type you cannot change it after being fixed it once. It also support mutable counterpart with NSMutablestring.
Objects In Objective-C

Categories in Objective-C

Our goal is to maintaining a large code for that what we can do is modularizing a className so categories is the way to split a single className file into multiple files. By applying this you can prevent your code from becoming monolithic that is impossible to navigate and easy to assign to a specific and well defined portion of a className that can be understand by any developer. By using categories you can extend an existing className without touching its original source file. Later after you can see how its functionality is used to emulate protected methods. Extensions are closely relate to categories.

Protocol in Objective-C

Objective-C allow to define protocol which declare methods that are expected to be used for a particular situation. It implements in the classes with some protocol standards. Protocols are more flexible than any className interface since use single API declaration in completely unrelated classes.

Hello World program in Objective-C

Objective-C basic Program contain Preprocessor Directives, Interface, Implementation, Methods, variables, Statements & expressions and comments.

{`
  #import 
  /* it is a pre-processor command which tells th compiler to include foundation.h file before going to actual compilation */
  @interface SampleClass:NSObject
  /* This shows how to create interface. Inherits NSObject object which is base className of all objects. */
  - (void)sampleMethod; /* It show how to declare a method */
  @end /* End of Interface */
  @implementation SampleClass
  - (void)sampleMethod{
  NSLog(@"Hello, World! \n");
  }
  /* it show how to implement sample baseclass*/
  @end
  int main() /*main function from where program execution begin */
  {
  /* my first program in Objective-C */
  SampleClass *sampleClass = [[SampleClass alloc]init];
  [sampleClass sampleMethod];
  return 0;
  }

  `}
  

What is Xcode

Xcode id Apple’s IDE for Mac, iPhone and iPad app development. It include source code editor, interface builder, a device simulator, a comprehensive testing and debugging suite. In this environment you can easily compile your Objective-C code. It provide elegant and powerful user interface to creating software development projects.

Difference Between Swift and Objective-C

Learn about Object-Oriented programming language

To get help in your Objective-C projects Contact Us