User Tools

Site Tools


This is an old revision of the document!

Objective-C Objects

A method of organizing code where methods and variables are organized into discrete, abstract groups called Objects. Allows humans to conceptualize interactivity easily.

Objects are created through Classes, which are blueprints for objects written in source code files.

Objective-C characteristics

  1. The class is defined in two different sections namely @interface and @implementation.
  2. Almost everything is in form of objects.
  3. Objects receive messages (method calls) and objects are often referred as receivers (of method calls).
  4. Objects contain instance variables.
  5. Objects and instance variables have scope.
  6. Classes hide an object's implementation from other objects.

Properties are used to provide access to class variables in other classes.

Each Objective-C object has two source code files each, one suffixed with .m and the other with .h, known as Header and Implementation files. Header files are used to declare public variables and methods to other classes, Implementation files implement the inner workings.

Two things required to create and use objects:

  • Declaration
  • Instantiation

Class Declaration

Declaration- the act of announcing to other objects the presence of a new variable, class, or method. Object declaration occurs in header files.

Header files contain:

  • @interface.. @end- wrap the contents of the header. Declarations are allowed in multiple files: compiler merges all wrapped contents into one header at compile time
  • method declarations
  • variable declarations- private instance variables and public instance variables (properties). Private instance variables must be contained within curly braces, those outside are public.

In order to refer to or use an object, developer must import the header file of the object into the class its being used in. This makes the second class “aware” of the first.

Object Instantiation

Instantiation- the act of creating an object in memory from a class. The object is known as an instance of the class.

Instantiation requires two steps:

  1. Allocation- retrieving sufficient memory to store new object by asking operating system, placing instance of class there
  2. Initialization- calling initialization method of object, sets initial variable values and creates data structures.

Creating an object from a class always has the same basic form:

CLASSTYPE *variable_name = [[CLASSTYPE alloc] init];
  1. CLASSTYPE is the name of the class being instantiated
  2. variable_name is the name you use to refer to the new object. The asterix indicates a pointer and must always be present, when referring to the object again omit the asterix
  3. Equals Sign assigns the newly created object on the right to the newly created variable on the left
  4. [CLASSTYPE alloc] retrieves a section of memory of size CLASSTYPE and places a new object there, returning a pointer to the new object
  5. [… init] calls the constructor of the object, initializing the object and preparing it for use

When we create a variable in Objective-C, we usually specify what kind of data it will hold, known as statically typing the data.

The asterisk (*) can be a little confusing. This is not part of the variable name. It can be placed anywhere between the data type and the variable name, so the following are all equivalent: NSString* title; NSString * title; NSString *title; Pointers are pretty much what they sound like– they “point” to a location in memory where the actual data is stored. That location in memory is the true container that holds the data.

Pointers are a part of regular C and are used because they are more efficient. When we use pointers in our programs, we only need to store and copy a simple pointer, which is really just an address for a space in memory. It’s a relatively small piece of data. If we instead had to store and copy the data being pointed to, we might very quickly run into problems of not having enough memory.

For example, it’s much more efficient to simply point to the location for a large video file and use that pointer multiple times in code than to actually have to use all the data of that large video file every time we access it in code.

Okay, so back to the asterisk: what does it mean to de-reference a pointer? It simply means that we obtain the value stored in the memory where the pointer is pointing to.

ios-labs-s14/basics-objects.1393441171.txt.gz · Last modified: 2014/02/26 12:59 by mbarboi