Pages

Tuesday, December 31, 2013

Objective-C for Beginner (Part 1)

Tools Requirements :
  • Mac OSX (Leophard, Mountain Lion, Lion, Mavericks, etc)
  • XCode (get from http://developer.apple.com)
Skill Requirements :
  • Other Basic Computer Programming (Java, PHP, Pascal, Delphi, etc)
  • Typing with 10 fingers (just kidding

Introducing Objective-C and XCode

For the most part, all computer languages perform the typical tasks that any computer needs to do—store information, compare information, make decisions about that information, and perform some action based on that decision. Objective-C is a language to make these tasks easier to accomplish and understand. The real trick with Objective- C (actually the trick with any C language) is understanding the symbols and keywords used to accomplish those tasks. This chapter introduces you Objective-C and Xcode— from Objective-C’s humble beginnings as an extension to the C language to using Xcode (a tool to build programs using Objective-C) to build programs. By the end of this chapter, you will understand what Objective-C is and know how to write a simple application using Xcode.


A Brief History of Objective-C
Objective-C is really a combination of two languages: the C language and a lesser- known language called Smalltalk. Back in the 1970s, several very bright engineers from Bell Labs created a language they named C that made it easy to port their pet project, the UNIX operating system, from one machine to another. Prior to C, people had to write programs in assembly languages. The problem with assembly languages is that each is specific to its machine, so moving software from one machine to another was nearly impossible. The C language, created by Brian Kernighan and Dennis Ritchie, solved this problem by providing a language that wrote out the assembly language for whatever machine it supported, a kind of Rosetta Stone for early computer languages. Because of its portability, C quickly became the de facto language for many types of computers, early PCs especially.
Fast-forward to the early 1980s, and the C language is on its way to becoming one of the most popular languages of the decade. Right around this time, an engineer from a company called Stepstone was mixing the C language with another up-and-coming language called Smalltalk. The C Language is typically referred to as a procedural language, that is, a language that uses procedures to divide up processing steps. Smalltalk, on the other hand, was something entirely different. It was an object-oriented programming language. Instead of processing things procedurally, it used programming objects to get its work done. This new superset of the C language became known as “C with Objects” or more commonly, Objective-C.
In 1985, Brad Cox sold the Objective-C language and trademark to NeXT Computer, Inc. NeXT was the brainchild of Steve Jobs, who had been fired from his own company, Apple Computer, that very same year. NeXT used the Objective-C language to build the NeXTSTEP operating system and its suite of development tools. In fact, the Objective-C language gave NeXT a competitive advantage with all of its software. Programmers using NeXTSTEP and Objective-C could write more-functional programs faster than those writing in the traditional C language. While the hardware part of NeXT computers never really took off, the operating system and tools did. Quite interestingly, NeXT was purchased by Apple Computer in late 1996 with the intention of replacing its aging operating system, which had been in existence since the first Macintosh was developed in 1984. Four years after the acquisition, what had been NeXTSTEP reemerged as Mac OS X—with Objective-C still at the heart of the system. 

Understanding C Language Basics
Even though Objective-C integrates a great object-oriented language, at the heart of Objective-C is C. Here is the most basic “Hello World” program written in the C language:
int main(void)
{

        if (printf(“Hello World”) == 0)
        {
         return 0;
         


        } else { 
          return 1;
        } 
}
Let’s dissect this a bit. Every program must start somewhere, right? Well, for Objective- C and C, main is the name of the procedure (which is often called a function in C) that is called first.
        int main(void)
It must be called “main”, not “Main,” “MAIN,” or anything else. C and Objective-C are case-sensitive languages meaning that main and Main are entirely different names.
Functions (and main is a function) all share the following syntax when they are declared: 
 return-type functionName ( argument-list ) 


Our first function, main, has the following:
  •  A return type of int: int is just shorthand for “integer.” An integer is a 32-bit value that has a range from –2,147,483,648 to 2,147,483,647. That’s a pretty large range of values! So, the function, main, must return an integer value to the function that called it. 
  • A function name of main: As mentioned previously, main is the starting point of any C or Objective-C program. If we were writing a different function, it could be named pretty much anything, as long as it starts with a letter. 
  • An argument list of void:void is a special type : it represents, in this case, the fact that there are no arguments.

The next line contains the character {. This is the opening brace symbol and is used to represent the beginning of series of steps, which is commonly referred to as a block of code. Every language has something like this, but in other languages, the opening of the block may either be implied or called BEGIN. In any case, the opening brace means that we are defining a block.

The third line: 
     if (printf(“Hello World”) != -1)  
actually has two parts. First, is the if keyword is a special command in the C and Objective-C languages that performs a test on something. To know what the if is testing, the next part is important: 
      printf(“Hello World”) 
This is a standard C function that prints formatted information to the screen. Now, the printf function returns a value after it completes its job. Basically, the result from the printf is whether the function actually worked or not. If printf returns a value greater than 0, the procedure worked; otherwise, it didn’t. The printf function is being passed a single argument, that is, the string “Hello World”. A string is nothing more that a series of characters grouped together. 
Therefore, printf is called and returns a value, and the if statement compares that value with another.  
The second part of the statement is the not-equal sign (!=): != -1) 
In C and Objective-C, an exclamation point is a logical not operator, so != means “not equal.” In this case, the if statement is comparing the return value from the printf procedure with the integer constant of –1. 

The forth, fifth, and sixth lines are as follows:
 {
     return 0;
 } 
This section starts off with another brace symbol, which means that another block is starting. In this case,
the first block that occurs after the if keyword represents the things for the program to do if the result of the if test is true. If the printf function call returns 0, the test for 0 will be true. If we replace the printf function with a return value of 0, if (0 == 0) is a true statement. Last, the closing brace (}) represents the end of this block.
However, if the test is not true, this block is completely ignored. Also notice that the line ends with a semicolon (;). This character is used in C and Objective-C to indicate that the end of a command. Why isn’t there one on the if statement? Well, the if statement is not finished being defined; the return statement represents the end of the true part of the if statement.
To sum it up, in the function main, if the printf statement returns a value of 0, the block after the if is used and this function will return a value of 0. In the C Language, a returned integer value of 0 typically represents a good thing. No news is good news.
Here’s the last part of the program:
  else
      {
            return 1;
       }

The else keyword is optionally used along with any if keyword. The block that appears right after the else keyword represents the things to do if the test in the if statement is not true. If the return value from printf is something other than 0, the else block will be executed.
Okay, that’s enough C language for now. Although the C Language is at the heart of Objective-C and very important, the “objective” part of Objective-C is used much more prevalently.
 


No comments: