Sei sulla pagina 1di 4

####################### #### Objective - C #### ## John D Riselvato ## #### Oct 5th, 2011 #### ####################### Who this is for?

People who don't know a thing about xcode or Objective-c or even programming. I' ll make this real simple. Part I: Intro to Objective-C ---------------------------Used xcode for development environment for this coding. Xcode is IDE by Apple for free and supports c, objective-c, c++, java and other native languages but this is an objective-c document. ..::Starting a project::.. open up xcode and create a new project. -if you have iPhone SDK installed, there are various templates because we are learning the objective-c language, choose the project that is the simplest of all of these: 1. From the Mac OS X group, select Application and then Command Line Tool. 2. Choose foundation from the type drop-down list. 3. Click the choose button, choose a location to save your project and click fin ish. Due to writing this from a terminal I can't show figure. So we will have to skip understanding the xcode frameworkds. I suggest finding a good tutorial for that and then coming back. ..::Compiling::.. Before we can get into writing our first code, we need to understand the idea of compiling. Compiling is what allows our code we write to be understood by the c omputer or platform you want to run your application on. To better understand this lets write a little code. ..::Writing some Code::.. Object-C is an object-oriented programming Language. This means that the program mer can divide a program into different components such as objects and classes. A class is a collection of data and mmethods. An object is an individual instance of a class. The extension of Objective-C source code is usually .m in case of implementation and .h for interface files. ==Lets get into an example of .h and .m usage== ~listing 1.0: interfaceFile.h @interface MyClass : NSObject { int foo; } @property (nonatomic) int foo; -(void)someMethod; @end ~listing 1.1: implementationFile.m #include 'interfaceFile.m' @implementation MyClass @synthesize foo;

-(void)someMethod { NSLog(@"Some method got called"); } @end

Understand? Well no worries lets go over this line from line! The interface states the fact that it is an interface by including the @interfac e directive. this is a @interface until the @end directive. Within these lines there are subs ections. The {}s in the interfaceFile is the first subsection. Member variables store dat a related to your module. The second subsection is were @property and someMethod is called. Methods are se t instructions for the computer to operate on data from property. The above is a typical object oriented module. We will come back to this later. ..::Back to compiling::.. After source code is created it needs to be compiled. Without compiling your cod e is useless, luckily compiling is easy. With xcode all you have to do is hit the build and run button and your app will be compiled. Lets try it with our example project above. This will display it's output. Now during compiling if there is errors in your code you will see youthe process halt and a display of the error. Thus you will need to correct your code before you can compile. More often then not, you will run into compiling issues. In a given day i'll meet 100 to 1000 compiling issues, its just part of program. We will get into knowing when to use which as we get into this book. No worries. Part II: Basic Syntax --------------------We will no get to understandinh how to write objective-c projects. Lets start wi th List 2.0 with xcode. listing 2.0: your first program #import <Foundation/Foundation.h> int main(int argc, const char *argv[]){ NSLog(@"Hello from Objective-C"); return 0; } Line 2: is the declaration of the main function. All objective-c applications ha ve a main function. the main function is where your program begins and ends. You r program begins executing by calling the main function from the operating syste m. the two arguments shown (argc) and (argv) contain the parameters given on the command line to your application. Line 3: contains a call to a function called 'NSlog'. This function causes whate ver string is passed to it to be printed to the console when the application is run. Line 2 and 5: the { and } indicate the scope of the main function. the semicolon s );) tell that its the end of those statements. They seperate one statement fro

m another. Line 1: import allows for you to load code from another file in your current fil e. in this case we need to use the foundation framework. This code is required t o allow us to use NSlog statement in line 3. the @"Hello..." is known as a string. Strings are literal text in your code. the y store in a variable in your program to be accessed later. In this case, its wh at we are printing to the terminal. Lets run this application! ..::Statements and Expressons::.. Programs are made up of statements and expressions in Objective-C. Statements are line of code for the purpose of executing an action. Statements do not have return values. So they do not change the state of the cur rent line of execution. Now expressions do return a vlue to the calling code and can change the flow of a program. Listing 2.1: Program with Variables #import <Foundation/Foundation.h> int main(it argc, const char #argv[]){ int aVar = 1234; NSlog(@"%ld", aVariable); return 0; } This includes a new concept variable (aVar). we have assigned 1234 to aVar and then passing the variable to NSLog function. Listing 2.2: Lets do stacks #import <Foundation/Foundation.h> int main(int argc, const char *argv[]){ int aVar1 = 2; if (aVar1 > 1){ int aVar2 = 5; NSLog(@"aVar1: %ld", aVar1); // True NSLog(@"aVar2: %ld", aVar2); // True } NSLog(@"aVar1: %ld", aVar1); // True NSLog(@"aVar2: %ld", aVar2); // False there is no aVar2 return 0; } Lets put this in english: List 2.3: Lets do Stacks (English) import Foundation.h Run main function aVar1 is 2 if aVar1 is greater then 1 then aVar2 is 5 print aVar1 is 2 print aVar2 is 5 end print aVar1 is 2 print aVar2 if no error end app end "if no error end app" or "return 0;", well because at "NSLOG(@"aVar2: %ld", aVar

2); there is no aVar2 there is going to be a return Error. due to the the way me mory is handeled the variable aVar2 no longer exists after the if statement scop e ends.