Sunday, June 22, 2008

Programming in Objective-C with Xcode

I'll start with an excerpt from my last post:
"I finally settled on a book by Stephen G. Kochan titled Programming in Objective-C. I am pretty well into the book now and I would definitely recommend it if you're new to Objective-C and/or OOP. Click here to check out the book on Amazon."

If this isn't the book you're using then this post won't be too relevant to you, as I'll be outlining some things specific to it in the post. You should still read it though, especially if you haven't yet decided on what resource you're going to choose for learning Objective-C.

The second half of the post is geared towards writing Objective-C in Xcode for the programs and exercises in the book. You're going to live in Xcode while developing iPhone applications, so you may as well get familiar now. There's a brief mention of Xcode in the book, but it wasn't ready yet when the book was released so I'll give you a little guidance to get started.

The Book: Programming in Objective-C

So far so good. It's living up to it's product description very well.
"Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming. The book makes no assumption about prior experience with object-oriented programming languages..."

Click here for a very detailed review, the one that sold me on the book. In that review are two very important suggestions that I would heed to. The first is the errata, and I will get to the second in the next half of this post about Xcode.
  • If you have the first run of this book (which has a Lighthouse on the front) you'll definitely want to print out the errata found here (PDF) and keep it handy, or make notes right in the book. The second run of the book (which has a merry-go-round on the front) has been updated.
Here are a few general notes on Objective-C from the first two chapters of the book:
  • Objective-C files should always end with a .m extension.
  • Objective-C is case-sensitive.
  • Similar to PHP (amd many other languages probably) you can single line comment with // and multi-line comment with /* */.
  • All program variables must be defined before they can be used.

Using Xcode to Write Objective-C

As you get through the first two chapters of the book you will begin writing a few simple programs. These tips should help get you on your way to writing those programs in Xcode:
  • To start a new project in Xcode: File Menu -> New Project -> Select Application under Mac OS X
    -> Cocoa Application
  • Each time you begin a new project, before writing any code you'll want to delete the entire contents of the <project_name>_Prefix.pch file and save it. Also delete the #import <cocoa/cocoa.h> line at the top of main.m. If you don't, your programs won't compile properly. Thanks to Kayembi on Amazon for pointing these out.
  • This is a matter of personal preference, but I like having line numbers in my code editor. To turn them on in Xcode go to Preferences Menu -> Text Editing -> Select Show Line Numbers
  • Lastly, I had a hard time finding the output of my first few programs. I would select "Build and Go" in XCode but never see anything aside from the message that says "Debugging of <project_name> ended normally." Then after a little poking around I discovered in Xcode if you go to Window Menu -> Organizer, then at the bottom select the small plus sign and choose the "Add Existing Folder" option, then choose the directory where you stored your first programs. Once your projects show up in Organizer you can select the folder for that program and click "Run". Provided you have already run "Build and Go" in Xcode, that window will show you the output of the program. Many of the programs and exercises in the book output simple strings, and this is a great way to see that output. In the same output window you can click "Clear log" on the right to clear the output if you want.
    UPDATE, AN EASIER WAY: "MaximumTheLyricist" commented the following: "I noticed that there is an easier way. If you go to the Run Menu in xcode then Console, you will see the output and save computer screen real estate." Thanks MTL!
At this point you're pretty much caught up with me. I've gotten through the first two chapters, and I can write a couple basic programs in Objective-C. I am about to crack Chapter 3: Classes, Objects, and Methods which is where the real (OOP) challenge begins!

7 comments:

Unknown said...

I appreciate the tips. I am learning programming for the first time in my life and I am Using this same book. I noticed that you are using the organizer window to see the output of your program. I noticed that there is an easier way. If you go to the Run Menu in xcode then Console, you will see the output and save computer screen real estate. Hope this helps.

-MaximumTheLyricist

Trae Regan said...

MTL, that's great advice, I appreciate it. I will use the console from now on. Thanks!

Unknown said...

When you guys ran the first "Programming is fun" program, did you get these two errors?

"warning: implicit declaration of function 'printf'"

"warning: incompatible implicit declaration of built-in function 'printf'"

Trae Regan said...

Hi David,

I never experienced that error. It sounds like you missed a portion of the code example maybe? I did it verbatim and it works fine. I repeated just now to be sure.

Best of luck,
-Trae

Rafael Mejia said...

In a very late response to David, it seems that if you get those 2 warnings, you probably didn't include the import stdio.h line.

Unknown said...

Good resources. then you have to quit the surfing to attend the incoming call.

iphone developer

Hire iphone developer said...

Hello,
If you want to hire developer programmer for iPhone mobile applications development with all the qualities mentioned above, you can hire them right away at very affordable prices here now!

hire iphone Developer