Tuesday, February 26, 2013

If.....Else...if.

If you have done any programming, you must have used conditional coding.

The basic conditional codes include:
1. "if" statement
2. "Switch" statement.

int a = 20;

if ( a == 20 )
{
     NSLog ( @" Value is %i :" , a );
}

This is a classic "if" statement used to provide a choice based on the condition that is between the parenthesis after the word "if".

This small piece of code remains same in Objective-C as well.

"else" can also be used in the same manner as you might have known them in any other language.

Example:

if ( a != 20 )
{
     NSLog ( @ "This is not 20" );
}
else
{
    NSLog ( @ "This is 20" );
}


The result will be: This is 20

You can nest multiple "if" satements like you have done before.

if ( condition 1 )
{
   // something happens here
}
else
{
    if ( condition 2 )
  {
     //some code comes here
  }
}

NOTE:
Do remember to take care of your curly braces "}".
If you open a curly brace, always close one.
Xcode will show an error if you don't.

This ain't so DIfferent.


One of the most important thing is printing out a message.
In C++, we use:
std::cout.

In Objective-C we use:
NSLog(@"hello");

NSLog - This is used to tell compiler that what follows goes to the output window.
@ sign is an indicator to the compiler that this code is not regular C but Objective-C.


Variable declaration:

This is quite same as C.

int iVar;
float fVar;
double dVar;

Calculations are done in the same way that you will do in any language:
int iResult = 10 * 5;

When outputting this iResult, we will use:

NSLog (@"the result is : %i ", iResult);

Sound familiar??
The use of "%i" as a placeholder is used in C as well.

Thursday, February 21, 2013

Hello World - objective-C style


Superset of C (C + Smalltalk) = Objective-C

It comes as quite a surprise when you learn that Objective-C is pretty old. It is from the days of C. At the time C++ was emerging Objective-C also paved a way for itself.

What do you mean by superset?
It is basically C with new functionalities added to it.

I tried my hand on writing the very first objective-c code. Obviously it had to be "Hello World" program. This has been simplified by objective-C. As you make a new project it by default is Hello World. You don't even have to type in the words.

Looking at the code you will notice certain things.
First of all the "main" function is from the good, old C language.

int main(int argc, char *argv[])

I will not go into this as it is used for passing inline parameters to the main function.

So when you look at the code how do tell if a piece of code is from C or from Objective-C.
There are few things if you notice, such as square brackets [], @ symbol and "NS". These indicate the parts that were added to C to make it Objective-C.

Tuesday, February 5, 2013

Where to start Objective-C from?



The first step in learning is to identify the source and for me it is www.lynda,com

As a Carnegie Mellon student I have free access to the huge library at this website. It has a course for beginners in Objective-C and Cocos2D. I will be watching videos here and referencing my book called "Learning Objective-C on the Mac".

I watched the Introduction and chapter 1 - Getting Started.
They introduce you to the history of Objective-C. It is pretty interesting to note that Objective-C is quite old.
It is a super set of C. The author also throws a caution in the wind that if you know C or C++, it still won't be easy.
The logic remains the same for any language but the syntax is an important part that cannot be ignored and quite often gives trouble.

Xcode
The next video told me about Xcode that is the Integrated Development Environment (IDE).
This is the most important aspect as all the code development will take place here. This is the equivalent of Visual Studio.
I was given an overview of the Xcode on how to start a new project and the various dock bars that you could see.

First Blog


I have started this blog as a way to keep track of my progress in learning Objective-C and Cocos2D.
For my last semester I have taken an independent study to teach myself how to program in Objective-C.
I will also use Cocos2D to make sample games which will make concepts easier to understand.
The overall goal of this exercise is to get back to my programming roots while learning a new language and at the same time use it to make simple games for iOS.

Here's to happy learning !!


P.S - This blog is based on the fact that you have done programming in C, C++ or Java. Basically any programming experience is needed.