Objective C 2.0: An Intro – Part 1

    June 18th, 2009 Posted by: - posted under:Tutorials

    Introduction

    Hello everyone, welcome to my second screeencast. This is going to be the first in a series of screencasts that are focused at people just beginning to work with Objective C and Cocoa. For many reasons the beginnings of learning cocoa development can be frusterating and lonely to a point. Only now is Objective C and Cocoa development gaining the kind of momentum to drive the creation of resources such as iCodeBlog and others.

    Skill Level Beginner

    This is not going to be a tutorial for someone who has never had any experience with programming. I aviod getting into the Object Oriented Methodology side of things. All you will need to know for this tutorail is generally the purpose of Classes, Methods and Objects. If you have done any work with Java, C, C++ or C# you should be able to follow the content no problem.

    Screencast

    I film myself coding out the entire sample project for each post. I personally think going through the Screencast is the best way to learn. But feel free to look through the slides and text if that suites you better.

    Tutorial

    picture-3

    picture-4

    picture-6

    picture-7

    picture-9

    picture-10

    picture-11

    picture-12

    picture-13

    picture-14

    picture-15

    picture-16

    Instructions

    1. Open xCode
    2. File -> New Project
    3. Start a new View based iPhone Project. The type of project you create really doesn’t matter for this exercise since we will only be programming a for loop to print and not creating any User Interface.
    4. Call the project iCodeBlogCounter
    5. After saving the project you will be confronted with a screen looking something like this.

      xcodefirstscreen

    6. If you look in the top left hand corner you will see a folder called Classes. If you click the little black triangle to the left of the folder you will see what is included in the classes folder. In there you should see a class called iCodeBlogCounterAppDelegate.m. This is the file we will be working with. Click on it and you will see its contents appear in the editor window.

      groupsandfilescloseup

    7. We will be working with the – (void)applicationDidFinishLaunching:(UIApplication *)application method. This method is called when the application finished launching. We will be entering some very simple code that will simple count from 0 to 99 and print the numbers in the terminal window. Here is what the method should look like:

      applicationdidfinishlaunchingcloseup

      The code here is:

      for(int i = 0; i < 100; i++)
      {
           NSLog(@"The current number is: %d", i);
      }
      
    8. That is all we need to do for this app. Now time to see it in action. To bring up the terminal window hit SHIFT + APPLE + R, this should bring up a blank window with maybe a line of text in it. Now click Build and Run or hit Apple + R. The terminal windows should say “The current number is 0” all the way to “The current is 99”. Here is a screenshot of my window.

    counterterminalwindow

    picture-17

    picture-18

    picture-19

    picture-20

    picture-21

    picture-22

    picture-24

    picture-25

    picture-26

    picture-28

    Instructions

    1. Open xCode
    2. File -> New Project
    3. Start a new View based iPhone Project. The type of project you create really doesn’t matter for this exercise since we will only be programming a for loop to print and not creating any User Interface.
    4. Call the project iCodeBlogGetURLText
    5. Once the project is open go into the iCodeBlogURLTextAppDelegate.m file.
    6. Add this code to the – (void)applicationDidFinishLaunching:(UIApplication *)application method
      NSString *myURLString = @"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt";
      NSURL *myURL = [NSURL URLWithString:myURLString];
      NSString *myString = [[NSString alloc] stringWithContentsofURL:myURL];
      
      NSLog(@"The string from the internet is: %@", myString);
      
    7. If you bring up the terminal window and Build and Run the App. You should see:

    urltextfileterminalwindow

    picture-29

    Instructions

    1. Go back to the code we just wrote.
    2. Erase the 4 lines of code we wrote and replace it with this line:
      NSLog(@"The string from the internet is: %@", [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt"]]);
      
    3. Running the application again should have the same output.
    4. Here is a a breakdown of out new line of code.

    picture-30

    picture-31