This is part of an ELC Tech Network

Objective C 2.0: An Intro – Part 1

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.

Objective C 2.0: An Intro – Part 1

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.
  6. xcodefirstscreen

  7. 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.
  8. groupsandfilescloseup

    Advertisement

  9. 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:
  10. applicationdidfinishlaunchingcloseup

    The code here is:

    for(int i = 0; i < 100; i++)
    {
         NSLog(@"The current number is: %d", i);
    }
    
  11. 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
  7. 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);
    
  8. 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:
  3. NSLog(@"The string from the internet is: %@", [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt"]]);
    
  4. Running the application again should have the same output.
  5. Here is a a breakdown of out new line of code.

picture-30

picture-31

This entry was posted in Uncategorized, iphone. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

47 Comments

  1. Martin
    Posted June 18, 2009 at 1:09 pm | Permalink

    Movie link seems to be broken.

  2. Posted June 18, 2009 at 1:09 pm | Permalink

    The link of the screencast is wrong m8. It leeds to a page not found page :(

    Although keep up the good work. Thanks for the tutorial.

  3. Tometoyou
    Posted June 18, 2009 at 2:32 pm | Permalink
  4. Posted June 18, 2009 at 2:56 pm | Permalink

    Hey everyone. Sorry for the mix up. All fixed. Enjoy,

  5. Matt
    Posted June 18, 2009 at 3:16 pm | Permalink

    When building the 2nd tutorial in 3.0 the app crashes.

    NSLog needs @ added in front of the string, and stringWithContentsofURL needs to be changed the initWithContentsofURL

    NSString *myURLString = @”http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/iCodeBlog.txt”;
    NSURL *myURL = [NSURL URLWithString:myURLString];
    NSString *myString = [[NSString alloc] initWithContentsOfURL:myURL];

    NSLog(@”The string from the internet is: %@”, myString);

  6. Posted June 18, 2009 at 5:14 pm | Permalink

    Amazing tutorial.

    Bravo

  7. Posted June 18, 2009 at 5:46 pm | Permalink

    Matt,

    You are totally correct, thanks for pointing that out. I will update the post. Thanks

  8. Posted June 18, 2009 at 7:40 pm | Permalink

    I really liked the post. I learned a bit. I think you should do more tutorials and explain them like. I know you do some but like yea and I am really gonna enjoy these tuts.

  9. Matt
    Posted June 19, 2009 at 8:22 am | Permalink

    Colin,

    No problem. It actually helped me cause i had to troubleshoot without very much help.

    Again, great tutorial!

  10. Posted June 19, 2009 at 8:51 pm | Permalink

    Colin,

    Really terrific tutorial. Well paced, very clear and utterly appreciated by this ‘code-impaired’ 3D artist. (I teach thesis animation in NYC and I say without hesitation, You are a natural born teacher). I look forward to the next section, and any and all of your future tuts.

    If you’re ever in the NY area, shoot me a note. I owe you a couple of cold beers.

  11. Posted June 21, 2009 at 3:24 am | Permalink

    Wow! Coming from Ruby (with a bit of Java and C# awhile back), ObjC appears totally mystifying. It’s still pretty challenging, but I really appreciate this introduction. Very helpful.

    I know making a screencast like this can take a lot of time, so I definitely want to thank you for the effort you out into this.

  12. Posted June 21, 2009 at 7:34 am | Permalink

    Ohhh! I was always looking for a tutorial that compared Java and Objective-C. Thank you!!!

  13. brandon
    Posted June 21, 2009 at 12:15 pm | Permalink

    Nice job! Definitely worth the time to watch all the way through, can’t wait to see what else you have coming up.

  14. Aaron Berk
    Posted June 21, 2009 at 5:16 pm | Permalink

    I think your tutorials and method of presentation are some of the best I’ve seen. I like how you go beyond the typical “Hello World” examples and present real-world examples instead. Thank you SO much!

  15. Brian Kendig
    Posted June 22, 2009 at 11:42 am | Permalink

    Collin,

    Just an FYI – in your post, you changed one use of “stringWithContentsofURL” to “initWithContentsOfURL”, but you missed the other place that “stringWithContentsofURL” appears, so the app still won’t work under 3.0. Good thing I looked in the comments before I got frustrated. :)

  16. Fernando
    Posted June 22, 2009 at 11:51 am | Permalink

    Amazing tutorial that was very helpful. The only question I have is how do you go about finding useful parts of the Cocoa frameworks. For example, you are using NSString and NSUrl pretty fluidly here, which is great. But how would an inexperienced programmer find out about how to use these? Is there a simple manner of which you can search through the framework so that you can identify what parts of it you will need to accomplish tasks?

  17. Posted June 22, 2009 at 12:31 pm | Permalink

    Hey Fernando,

    Great question. I use the documentation in xCode. This can be reached by going to Help -> Documentation and then searching. OR you can hold option and Double Click on a Class name such as NSString to bring up that specific class and its related methods and attributes. I will make sure to include this in the next screencast

    Thanks for reading!

  18. Chad
    Posted June 22, 2009 at 5:43 pm | Permalink

    Awesome tutorial! Thanks for that very informative introduction to Objective-C.

  19. Graham
    Posted June 23, 2009 at 5:09 pm | Permalink

    Great post. BTW, the change to initWithContentsOfURL hasn’t been put into the code for example 2 yet ;)

    Keep up the good work!

  20. rob
    Posted June 25, 2009 at 5:24 am | Permalink

    hey collin,
    im coming from a vb6! background and trying to immerse myself in objective-c with the hope some of it will stick.
    really enjoyed your tutorial, i think you did a good job pitching to newbies (where nearly everyone else falls over at the starting block)
    cheers

  21. Linda
    Posted June 25, 2009 at 10:51 am | Permalink

    THANK you Colin. Keep up the helpful work.

  22. Slim
    Posted June 26, 2009 at 2:01 pm | Permalink

    Thanks a lot, that’s so useful !

    I hope more similar tutorials will be posted going more complicated in a gradual pace.

    Thanks again

  23. Andrew
    Posted June 29, 2009 at 12:40 pm | Permalink

    This tutorial really helped me understand the stuff from the other tutorials on this site. I can’t wait for more tutorials.

  24. Posted June 30, 2009 at 7:18 pm | Permalink

    Hello! It’s very good tutorial and very easy for start. I have a question about books, what do you recommend for good start? which books? Thank you a lot

  25. Posted July 4, 2009 at 9:05 am | Permalink

    Perfect timing on this. I started playing around with iPhone development and realized that I didn’t know enough ObjC to get by. So I’ve been out scrounging around the internet looking for good tutorials.

  26. Dennis
    Posted July 7, 2009 at 10:55 am | Permalink

    Thanks for the tutorials! Great job. You’ve really helped me a lot. Keep up the good work.

  27. Posted July 7, 2009 at 9:55 pm | Permalink

    Fantastic tutorial and glad I finally found it after stumbling on a lot of halfway tutorials out there. I was feeling pretty dumb with a decent background in C++ and Java and absolutely NO clue what was going on in some of the sample code I was trying to decipher.

    This tutorial has been a Rosetta’s stone for me and I look forward to checking out the follow up I saw before.

    Thanks Collin!

  28. Posted July 9, 2009 at 2:09 pm | Permalink

    The link appears to be broken again, could you post the screencast on Vimeo instead?

  29. Contrite
    Posted July 9, 2009 at 6:40 pm | Permalink

    I was looking for the screencast as well. Would appreciate it you could let me know when it’s back up! Thanks!

  30. Richard
    Posted July 9, 2009 at 8:10 pm | Permalink

    Hi, this tutorial is exactly what I have been looking for – please get the link to the screencast working again!

    Great site BTW.

  31. Simon
    Posted July 9, 2009 at 9:16 pm | Permalink

    Link to the video seems to be broken again :-(

    Please update as I would really like to watch it.

  32. Posted July 9, 2009 at 11:22 pm | Permalink

    Um, wow. Absolutely perfect. I’m a management consultant and this is an amazingly great educational report. Things were structured, clear, and are set up from the perspective of the reader. You have my vote of confidence and just found yourself a loyal reader/subscriber (your feed has been added!) Thanks a ton.

  33. artaxerxes
    Posted July 10, 2009 at 8:05 am | Permalink

    Is nesting broken/deprecated in the latest SDK?

  34. Posted July 11, 2009 at 8:35 am | Permalink

    Well, I think you might just have a new reader, finaly an easy beginning of cocoa and objective C, not that I can’t program (I’m pretty advanced with PHP, (OOP, MVC and other design patterns)) but somehow couldnt seem to get the grasp of Objective-C.

    Read lots of documentation from Apple, and looked at some PDF books, but none where really clear, this is :)

  35. Tylor
    Posted July 11, 2009 at 12:38 pm | Permalink

    Links not working…. Please fix! Thanks!!!

  36. JD
    Posted July 12, 2009 at 11:09 am | Permalink

    Excellent tutorial! I appreciate you taking the time and effort to produce the examples. I am coming from programming Windows Mobile (C#) and you are making the switch very pleasant. Happy coding!

  37. Wes
    Posted July 12, 2009 at 8:54 pm | Permalink

    The link is broken. Please update the link, i’d lovw to check out this video.

  38. Jon
    Posted July 13, 2009 at 5:01 am | Permalink

    Video link broken for me too… can it be posted on Vimeo? Thanks!

  39. Shishir
    Posted July 14, 2009 at 3:25 am | Permalink

    Yes, someone please fix the link!

  40. Kinara
    Posted July 16, 2009 at 3:08 pm | Permalink

    Would really appreciate if you could fix the link to the Video.

    Thanx

  41. Posted July 18, 2009 at 2:08 pm | Permalink

    Video fixed. Sorry about that.

  42. dave
    Posted July 18, 2009 at 3:08 pm | Permalink

    initWithContentsofURL should be initWithContentsOfURL

  43. dave
    Posted July 18, 2009 at 3:20 pm | Permalink

    Collin, please correct this lovely tutorial so it will be a great
    tutorial.

    // right
    NSString *myString = [NSString stringWithContentsOfURL:myURL];

    // right
    NSString *myString = [[NSString alloc] initWithContentsOfURL:myURL];

    //wrong
    NSString *myString = [[NSString alloc] stringWithContentsOfURL:myURL];

  44. Ben
    Posted July 22, 2009 at 12:25 pm | Permalink

    Great Tutorial – really helpful for a newbie like me… this site is well and truly bookmarked…

    I was wondering why my test was returning (null)… looks like the URL for the text file is offline… Whoops..

  45. nFieldFlyGuy
    Posted September 22, 2009 at 10:00 pm | Permalink

    Phenomenal tutorial. You can’t get too basic for newbies like myself. I really appreciate the screencasts. thanks for your work…Mark

  46. josh
    Posted September 30, 2009 at 2:48 pm | Permalink

    I think the URL from tutorial 2 is missing. Code still works, but comes up nil. I’m guessing it is because you changed servers. Also, corrections are in the comments for Tutorial 2 (initWithContentsOfURL), but would be nice to have them in the body of the article.

    Thanks so much for doing these though. They help tremendously!

  47. Posted June 2, 2010 at 8:59 am | Permalink

    At last I can really get started! Programmed with C++ in the distant past and needed some cobwebs blown away as well as learning the new stuff for Objective-C. I hope you can find the time to continue.
    Regards, Mike.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">