Showing posts with label Intro. Show all posts
Showing posts with label Intro. Show all posts

Monday, September 3, 2012

Introduction to Objective-C

Refer to the first part of the tutorial from this link: http://iosmadesimple.blogspot.com/2012/07/introduction-to-xcode.html

Objectives:

  • To understand the class structure and its implementation in Objective-C
  • To get acquainted with Objective-C syntax
Objective-C uses Model-View-Controller design pattern. If you want to know more about MVC design pattern in Objective-C, refer to this link from Cocoa Lab. http://www.cocoalab.com/?q=node/24

Class Structure

  1. Interface Section - where your data components and function headers are located; found in .h files
  2. Implementation Section - where your implementation for your functions are located; found in .m files
  3. Program Section - these are your codes; where your instatiate objects to carry out your tasks

Class Definition and Instatiation Syntax

Interface (.h files)

@interface NameOfClass:ParentClass {
//instance variables
returnType nameOfVariable;
}

//instance methods
- (returnType)methodName1; //No Parameters
- (returnType)methodName2:(returnType)parameter; //With One Parameter
- (returnType)methodName3:(returnType)parameter1 ThisIsPartOfTheMethodNameWithData:(returnType)parameter2; //With MultipleParameters
@end

Implementation (.m files)

@implementation NameOfClass
- (returnType)methodName1 {
//Put your codes here
}

- (returnType)methodName2:(returnType)parameter {
//Put your codes here
}

- (returnType)methodName3:(returnType)parameter1 ThisIsPartOfTheMethodNameWithData:(returnType)parameter2 {
//Put your codes here
}
@end

Program

main.m file //Remeber to import the class you created
int main(int argc, char *argv[]) {
//Create instance of the class
NameOfClass *instanceName1 = [[NameOfClass alloc] init];

//Or you may do it this way
NameOfClass *instanceName2;
instanceName2 = [NameOfClass alloc];
instanceName2 = [NameOfClass init];

//Calling a class function
[instanceName2 methodName];
[instanceName2 methodName2:100]; //assume that the dataType of the parameter is int, let's pass integer 100

//If you no longer need the objects, release their memory
[instanceName1 release];
[instanceName2 release];
}



Monday, July 30, 2012

Introduction to XCode

Hi! For our first lesson, let's get to know the Xcode IDE.

Assumptions:
  1. You have no idea about the primary language in creating iOS applications, which is Objective-C.
  2. At least, you know how to code in C language.
  3. You are very familiar with Object Oriented Programming.


Objectives:
  1. To know the IDE that we will be using
  2. To be able to make simple applications
  3. To build and run our programs on our console.

You may download Xcode from http://developer.apple.com/ and go to iOS Dev Center.

Xcode is an Integrated Development Environment that lets you write down your code, compile, debug and execute your program.
I'll assume that you have finished installing Xcode on your computer.

To create projects in Xcode, let us run the application first.

1. Choose "Create a new Xcode project."

2. Choose Command Line Tool (on the right panel) under Mac OS X (from the left panel).

Since this is going to be our first time, let us be familiar with running our programs in Command Line Tool first.


3. Name your project under "Product Name"
4. Add a Company Identifier
5. Choose Foundation type
6. Uncheck "Use Automatic Referencing Counting"
7. Choose a directory to save your project.

We will discuss what Foundation is once we'll start our lesson on Frameworks, and we will also discuss what Automatic Reference Counting (ARC) is once we got hold of the Memory Management of Objective-C. For now, uncheck "Use Automatic Referencing Counting."


Tadaaa! Here's your project!


Look at the upper right corner of your IDE, you'll see that the left most button under the "View" category has been selected, which in turn, shows the "Navigator" of our program.


The left most icon inside the red box is the "Project Navigator." It lets us navigate to the different folders and files that were created automatically by the IDE when we create our project. We will discuss the use of other icons on the Navigator Pane once we "need" to use them.


The left and the middle button under the "View" category is now selected. The orange one is the Debugging Area. On the upper right corner of the Debugging Area, you will see three buttons, the currently selected button is the one at the middle. The middle button shows both the Variables (on the left) and the Console area (on the right). The left button shows only the Variable Area and the right buttons shows only the Console Area.

The right pane inside the green box is the Utilities Area. The upper part of the Utilities Area has the "File Inspector" which gives you information about the selected file from the "Project Navigator" (in our case, the main.m file). The lower part shows the different libraries, the currently selected library is the Object Library. The Object Library is very useful when we start creating UI for our iOS Applications. For now, don't mind it yet.

Let's Build and Run the application!
To build: 
Command + B //this will compile your code
To run: 
Command + R //this will run your code

As we can see from our main.m file, there's an NSLog(@"Hello World"); code. We can also put codes on that area inside @autoreleasepool{}. We will explain on our next lesson the reason of putting our codes inside the scope of autoreleasepool.

We shall see this result on our Console Area when we run our program(you know where it is, right?):



I do hope some of you learned something. And thank you for your time reading this. :)

Where to go from here:
http://iosmadesimple.blogspot.com/2012/09/introduction-to-objective-c.html

Blocks From one Class to Another

One popular scenario in using blocks is when you need to update your view after a web service call is finished.