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
Instructions
- Open xCode
- File -> New Project
- 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.
- Call the project iCodeBlogCounter
- After saving the project you will be confronted with a screen looking something like this.
- 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.
- 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:
- 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.


The code here is:
for(int i = 0; i < 100; i++)
{
NSLog(@"The current number is: %d", i);
}

Instructions
- Open xCode
- File -> New Project
- 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.
- Call the project iCodeBlogGetURLText
- Once the project is open go into the iCodeBlogURLTextAppDelegate.m file.
- Add this code to the - (void)applicationDidFinishLaunching:(UIApplication *)application method
- If you bring up the terminal window and Build and Run the App. You should see:
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);

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



























47 Comments
Movie link seems to be broken.
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.
The link is actually:
http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/Objective%20C2.0AnIntroPart1.mov
Hey everyone. Sorry for the mix up. All fixed. Enjoy,
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);
Amazing tutorial.
Bravo
Matt,
You are totally correct, thanks for pointing that out. I will update the post. Thanks
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.
Colin,
No problem. It actually helped me cause i had to troubleshoot without very much help.
Again, great tutorial!
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.
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.
Ohhh! I was always looking for a tutorial that compared Java and Objective-C. Thank you!!!
Nice job! Definitely worth the time to watch all the way through, can’t wait to see what else you have coming up.
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!
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.
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?
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!
Awesome tutorial! Thanks for that very informative introduction to Objective-C.
Great post. BTW, the change to initWithContentsOfURL hasn’t been put into the code for example 2 yet
Keep up the good work!
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
THANK you Colin. Keep up the helpful work.
Thanks a lot, that’s so useful !
I hope more similar tutorials will be posted going more complicated in a gradual pace.
Thanks again
This tutorial really helped me understand the stuff from the other tutorials on this site. I can’t wait for more tutorials.
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
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.
Thanks for the tutorials! Great job. You’ve really helped me a lot. Keep up the good work.
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!
The link appears to be broken again, could you post the screencast on Vimeo instead?
I was looking for the screencast as well. Would appreciate it you could let me know when it’s back up! Thanks!
Hi, this tutorial is exactly what I have been looking for – please get the link to the screencast working again!
Great site BTW.
Link to the video seems to be broken again
Please update as I would really like to watch it.
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.
Is nesting broken/deprecated in the latest SDK?
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
Links not working…. Please fix! Thanks!!!
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!
The link is broken. Please update the link, i’d lovw to check out this video.
Video link broken for me too… can it be posted on Vimeo? Thanks!
Yes, someone please fix the link!
Would really appreciate if you could fix the link to the Video.
Thanx
Video fixed. Sorry about that.
initWithContentsofURL should be initWithContentsOfURL
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];
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..
Phenomenal tutorial. You can’t get too basic for newbies like myself. I really appreciate the screencasts. thanks for your work…Mark
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!
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.