An Overview of Objective C Basics for App Development - FREE Guru Level Training For Beginners (2014)

FREE Guru Level Training For Beginners (2014)

Chapter 5: An Overview of Objective C Basics for App Development

“If you, or any public-spirited programmer, wanted to figure out what the software on your machine is really doing, tough luck. It's illegal to reverse engineer the source code of commercial software to find out how it works.”- Clive Thompson

In this chapter, you will learn about an overview of objective C basics for app development, including:

· Object oriented programming

· Why go for OOP

· Comments

· Variables

· Operators

· How methods, functions and functions work

· Method body

· Declaring methods

· Calling the method

· Function arguments

· While loop

· For loop

· Do…while loop

· Nested loops

· Control statements in loops

· Break statement

· Continue statement

· The infinite loop

Objective C is the programming language that app developers use to create app for the iOS (or the Apple Software). So far you have learned how to make an app using the programming language. This chapter will help you understand the basics of the programming language. This way, you will be capable of making your own apps, knowing the basics of Objective C and with ease.

Object Oriented Programming

If you talk to a programmer about Java, Objective C and other programming languages they will tell you that these are object-oriented programming. What does this mean?

Object-oriented programming languages are those that will combine functions with data structures in order to create objects that can be used over and over again. Where OOP is concerned, the programmer will want to manipulate the objects rather than consider the logic behind manipulating them in any way. So, the ‘how’ and not the ‘why’ factor is taken into account. Anything can be an object- cats, dogs, human beings, your house address or a city/country that you might be living in and such like.

Why Go for OOP

Surely, at this point you must be wondering why you should go for OOP. For starters, it is easy to develop codes and to maintain these with object-oriented programs. Here are few other reasons:

Encapsulation: implementation of the code can be changed and this will not affect other parts of the code.

Polymorphism: functions, with different names can be used to handle different data.

Generics: there is no need for you to rewrite the code over and over again.

Inheritance: you can write and utilize various functions without changing them.

This makes OOPs very convenient, user-friendly and comprehendible for app developers.

Comments

Comments have no impact on your code. They simply enable you to make notes and keep track of your progress or actions. There are 2 types of comments- one line comments and multiline comments.

One line comments are used for short comments. Here’s an example of one:

// This is a single line comment

Everything that comes after the double slash (//) is ignored by the complier. Objective C also allows you to write longer comments and this is done by utilizing multiline comments. Here’s an example of such a comment:

/*

Here is a multiline comment
and I am using this as an example.

*/

Variables

In programming languages variables are used to store values and objects. You can assign any value to a variable- this can be a number, string or any other object in most other languages. However, where Objective C is concerned, you need to declare the variable but you won’t be able to modify this later on. So, this is what the variable looks like in Objective C:

Float variable 1 = 2.89;

char variable 2 = ‘i’;

You can include numbers in a variable but not before the variable. Also, capital letters can be used in the variable but you cannot put in any whitespaces. You must end every instruction with the semi-colon sign (;).

Here are some of the most common variables in Objective C:

Type Description Examples

int negative and positive integer numbers 0, 110, -28
unsigned int only positive integers 0, 34, 340
float, double floating point decimal numbers 1.32, 3.45
bool boolean values true, false
char single text character 'b', 'u', '!'

Operators


These are used with numeric values and include the multiplication (*), subtraction (-), addition (+), division (/) and module (%) signs. In addition to this there are increment (++) and decrement (--) operators as well. You can use these before (++myVariable) or after (myVariable++) the variable.

See how increment and decrement operators work here: http://www.techotopia.com/index.php/Objective-C_Operators_and_Expressions#Increment_and_Decrement_Operators.

Operators in Objective C, unlike operators in other programming languages, are not capable of working with objects.

How Methods, Functions and Functions Work

Methods are called from other parts of your code. They are invoked to perform some sort of action and to return a value. These help save a lot of time. For example, the NSString length is one of several methods in Objective C.

As far as Objective C is concerned, every method (in this case length) begins with a – or +. These signs are attached to instance methods. The + sign is attached to class methods and one can access class methods by referencing the class. In Objective C there’s the method header and the method body. The different parts of any method are as follows:

Return type: a method might return a value. However, in some cases this might not happen. The keyword void is used for such methods.

Method name: As is obvious, this is the name of the method.

Arguments: Values are passed to the argument every time a function is invoked. Methods can or cannot consist of arguments

Method body

The method body is simply a set of statements, put together to define what the method will do or how it will function.

Let’s see how this works in case of a function called max():

/* function max of two numbers */

- (int) max:(int) 10 secondNumber:(int) 3

{

/* local variable declaration */

int result;

if (10 > 3)

{

result = 10;

}

else

{

result = 3;

}

return result;

}

10 and 3 are parameters and the method, max(), is used so you can get the maximum between the two integers.

Declaring Methods
Declaring the method will enable you to tell the function’s name. In addition to this it will also tell the compiler how it can call the method. In some instances you might have defined the method is one source file while wanting to call it in another file. In such instances it is important that you declare the method. So, if you want to declare a method, remember that it has the following parts:

- (return_type) function_name:( argumentType1 )argumentName1
joiningArgument2:( argumentType2 )argumentName2 ...
joiningArgumentn:( argumentTypen )argumentNamen;

Here’s how you can declare the max() method:

(int) max:(int)10 and3:(int)3;


Here, you’re declaring the values and integers (10 and 3) for the method so it can choose the higher integer of the two.

Calling the Method

When you create methods in Objective C you need to define what the function will do. The function is then, called so that it can perform this defined task. In order to call a method the required parameters first have to be passed, with the method name. The returned value (presuming there is one) can then, be stored.

Look at the following example:

#import <Foundation/Foundation.h>

@interface SampleClass:NSObject

/* declaring a method */

- (int)max:(int)10 and3:(int)3;

@end

@implementation SampleClass

/* method returning the max between two numbers */

- (int)max:(int)10 and3:(int)3{

/* local variable declaration */

int result;

if (10 > 3)

{

result = 10;

}

else

{

result = 3;

}

return result;

}

@end

int main ()

{

/* definition of local variable*/

int a = 10;

int b = 20;

int ret;

SampleClass *sampleClass = [[SampleClass alloc]init];

/* calling a method to get max value */

ret = [sampleClass max:10 and20:20];

NSLog(@"Max value is : %d\n", ret );

return 0;

}

In the end you’ll get a max value of 20. As you can see, two functions were kept together while compiling the source code: the max() and main() functions.

Function Arguments


A function needs to declare variables. These variables are called formal parameters and must accept an argument’s variables. Formal parameters behave and act just as any other variable would behave. You create these by entering the variables in the function but they are destroyed as soon as you exit the function.

You can call a function by value (the call type for this is call by value) or by reference (call by reference).

Call by value will copy the argument’s value and enter it into the formal parameter. If you change the parameters within the function, this will not change or impact the argument in any way.

Where call by reference is concerned, the argument’s address will be copied into the formal parameter and this can be used for accessing the argument within the call. So, if you change the parameters within the function, this certainly will affect the argument.

Objective C utilizes call by value by default.

Loops

No programmer wants to have to write the same code over and over again. This is time-consuming and it requires that the same code be utilized again and again. Loops help get rid of this issue. All programming languages use loops to avoid this issue and Objective C does too.


There are 4 types of loops: while, for, do…while and nested loops.


While loop

So long as a condition is true, the loop will continue the execution of the target statement. In other words, as long as the condition set is true, the code will be run over and over again.

Here’s an example:

int main ()

{

/* local variable definition */

int a = 1;

/* while loop execution */

while( a < 10

)

{

NSLog(@"value of a: %d\n", a);

a++;

}

return 0;

}


Run this code and you will get the following result:

2014

11

11 03:01:42.259 demo[19562] value of a: 1

2014

11

11 03:01:42.259 demo[19562] value of a: 2

2014

11

11 03:01:42.259 demo[19562] value of a: 3

2014

11

11 03:01:42.260 demo[19562] value of a: 4

2014

11

11 03:01:42.260 demo[19562] value of a: 5

2014

11

11 03:01:42.260 demo[19562] value of a: 6

2014

11

11 03:01:42.260 demo[19562] value of a: 7

2014

11

11 03:01:42.260 demo[19562] value of a: 8

2014

11

11 03:01:42.260 demo[19562] value of a: 9


Note: Try the following link for more example: http://www.tutorialspoint.com/objective_c/objective_c_while_loop.htm.


So what happened here? Well, the condition set was that if a=1 while the value of a is less than 10, you must get a return value of 0. The condition was true here and hence, I got a=1, 2,3 and so on and so forth.


For Loop


Here is the syntax for the ‘for’ loop in Objective C:

for ( init; condition; increment )

{

statement(s);

}

init is executed once so that variables in the loop are initialized. The loop will then, evaluate the condition. So long as it is true, the loop can be executed. With the aid of the increment statement it is possible for you to enter other loop control variables. You don’t have to enter this but be sure to enter the semi-colon sign after the condition.

After this, the loop will continue to execute the code so long as the condition is true.

Do….While Loop

The syntax for this loop is as follows:

do

{

statement(s);

}while( condition );

This loop will check whether the condition is true at the end. It will execute your code at least once. The loop will continue to run until the condition is false.

Look at the following example:

int main ()

{

/* defining local variable */

int x = 10;

/* loop execution */

do

{

NSLog(@"value of a: %d\n", a);

x = x + 2;

}while( x < 20 );

return 0;

}

In this case, the condition set is that so long as x= 10, x= x+2. In other words, the value of x will be added to 2 so long as x is less than 20.

The output is as follows:

2014-11-11 14:40:30.974 demo[14864] value of a: 10

2014-11-11 14:40:30.974 demo[14864] value of a: 12

2014-11-11 22:40:30.974 demo[14864] value of a: 14

2014-11-11 22:40:30.974 demo[14864] value of a: 16

2014-11-11 22:40:30.974 demo[14864] value of a: 18


Beyond this point x will be equal to 20 and so, the condition is no longer true. Hence, the loop is terminated.

Nested Loops


With Objective C you can place one loop inside the other. These are referred to as nested loops. You can read about the various types of nested loops here: http://www.tutorialspoint.com/objective_c/objective_c_nested_loops.htm.

Control Statements in Loops

In some instances you might need to control the execution of the loop. This enables you to divert the loop from the normal sequence. There are two types of control statements where Objective C is concerned: the break statement and the continue statement.

Break Statement

If the break statement is encountered by the loop, it will be terminated and then, resumed at the next statement.

Continue Statement

This statement is used so the remaining part of the code is skipped and the loop will start all over again.

The Infinite Loop

The infinite loop is one in which the condition will always be true. This is what the ‘for’ loop is usually used for. These can cause an app to ‘freeze’ or become ‘deadlocked’

Summary


This chapter tells you everything you need to know about the various types of loops, setting conditions and how to utilize control statements as well.