Objective C 2.0: An Intro – Part 1
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
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.


Advertisement

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"]]);
- Posted by Collin on 18 Jun 2009 in Uncategorized, iphone
- Digg |
- Del.icio.us |
- Stumble |
- 46 Comments »
46 Responses
vtsib Says:
June 18th, 2009 at 1:09 pm
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.
Tometoyou Says:
June 18th, 2009 at 2:32 pm
The link is actually:
http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial2/Objective%20C2.0AnIntroPart1.mov
Matt Says:
June 18th, 2009 at 3:16 pm
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);
Collin Says:
June 18th, 2009 at 5:46 pm
Matt,
You are totally correct, thanks for pointing that out. I will update the post. Thanks
AppStoreMod Says:
June 18th, 2009 at 7:40 pm
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.
Matt Says:
June 19th, 2009 at 8:22 am
Colin,
No problem. It actually helped me cause i had to troubleshoot without very much help.
Again, great tutorial!
Brendan Says:
June 19th, 2009 at 8:51 pm
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.
Geoff Says:
June 21st, 2009 at 3:24 am
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.
RobotGrrl Says:
June 21st, 2009 at 7:34 am
Ohhh! I was always looking for a tutorial that compared Java and Objective-C. Thank you!!!
brandon Says:
June 21st, 2009 at 12:15 pm
Nice job! Definitely worth the time to watch all the way through, can’t wait to see what else you have coming up.
Aaron Berk Says:
June 21st, 2009 at 5:16 pm
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!
Brian Kendig Says:
June 22nd, 2009 at 11:42 am
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.
Fernando Says:
June 22nd, 2009 at 11:51 am
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?
Collin Says:
June 22nd, 2009 at 12:31 pm
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!
Chad Says:
June 22nd, 2009 at 5:43 pm
Awesome tutorial! Thanks for that very informative introduction to Objective-C.
Graham Says:
June 23rd, 2009 at 5:09 pm
Great post. BTW, the change to initWithContentsOfURL hasn’t been put into the code for example 2 yet
Keep up the good work!
rob Says:
June 25th, 2009 at 5:24 am
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
Slim Says:
June 26th, 2009 at 2:01 pm
Thanks a lot, that’s so useful !
I hope more similar tutorials will be posted going more complicated in a gradual pace.
Thanks again
Andrew Says:
June 29th, 2009 at 12:40 pm
This tutorial really helped me understand the stuff from the other tutorials on this site. I can’t wait for more tutorials.
Pupsor Says:
June 30th, 2009 at 7:18 pm
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
Ryan Says:
July 4th, 2009 at 9:05 am
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.
Dennis Says:
July 7th, 2009 at 10:55 am
Thanks for the tutorials! Great job. You’ve really helped me a lot. Keep up the good work.
Lewis Says:
July 7th, 2009 at 9:55 pm
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!
LockeCole117 Says:
July 9th, 2009 at 2:09 pm
The link appears to be broken again, could you post the screencast on Vimeo instead?
Contrite Says:
July 9th, 2009 at 6:40 pm
I was looking for the screencast as well. Would appreciate it you could let me know when it’s back up! Thanks!
Richard Says:
July 9th, 2009 at 8:10 pm
Hi, this tutorial is exactly what I have been looking for – please get the link to the screencast working again!
Great site BTW.
Simon Says:
July 9th, 2009 at 9:16 pm
Link to the video seems to be broken again
Please update as I would really like to watch it.
Carbon Rabbit Says:
July 9th, 2009 at 11:22 pm
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.
Matthijn Says:
July 11th, 2009 at 8:35 am
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
JD Says:
July 12th, 2009 at 11:09 am
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!
Wes Says:
July 12th, 2009 at 8:54 pm
The link is broken. Please update the link, i’d lovw to check out this video.
Jon Says:
July 13th, 2009 at 5:01 am
Video link broken for me too… can it be posted on Vimeo? Thanks!
Kinara Says:
July 16th, 2009 at 3:08 pm
Would really appreciate if you could fix the link to the Video.
Thanx
dave Says:
July 18th, 2009 at 3:20 pm
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];
Ben Says:
July 22nd, 2009 at 12:25 pm
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..
nFieldFlyGuy Says:
September 22nd, 2009 at 10:00 pm
Phenomenal tutorial. You can’t get too basic for newbies like myself. I really appreciate the screencasts. thanks for your work…Mark
josh Says:
September 30th, 2009 at 2:48 pm
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!



























