Baby steps in Xcode 4

Discussion in 'Mac Programming' started by sioannou, Aug 13, 2011.

  1. sioannou macrumors member


    Mar 25, 2010
    Nicosia Cyprus

    I'd say I am familiar with C , C++ , Java and C# especially the last two as I've used them not only for university projects (i am cs major) but also for my own purposes like for example java for android programming.

    Recently, I've tried to put my hands on Objective C. Understanding objective c and also the concept of OO is not really a problem for me.

    The problem comes down to COCOA and the use of XCODE itself. I didn't imagine that I will come across with such difficulties and i think the problem it's the XCODE and not that much COCOA. I am frustrated trying to develop even the simplest program as I don't understand how to use the interface builder and all these connection.

    Through the books I've read (there are for earlier versions of xcode other than 4.0 but this is not a problem) i understand that any objects needs a pointer to it. For example if we have a textfield we need a pointer to a textfield.

    This is ok as :

    1. Declare the pointer to the .m
    2. We need an NSobject (is this an instance of the class xxx.m?) so the textfield points to
    3. A button which points the NSobject.
    4. Drag the button to .h and then program the method in .m

    IS this procedure correct? Please if you have any info or any books to recommend I would highly appreciate it.
  2. mathmad macrumors newbie

    Jul 23, 2011


    I have no idea what you are talking about, but I am stepping my first steps into cocoa world too, and here what i got:

    -When you want to create an instance of a class, always use pointer, i.e., never do something like this:

    ClassName aObject;

    but like this:

    ClassName *aObject;

    Is that what you meant by declare a pointer to the .m ?

    -NSObject is a class. Every class you create, if you dont subclass any other class, subclass NSObject class.

    -You use interface builder (IB) to create your interface and save it in a .nib (or .xib) file. A project created in xcode 4 will have one default .nib file created. When compliled and the program run, it will display what are in the default nib file on the screen.
    Now imagine that you have a button in your .nib file. You might want to do something when that button get clicked, so you need to "link" the button to the code that do whatever you need to do when the button get clicked. But how do we do that kind of linking?

    A button does not point to an NSObject!!! A button in the .nib file need two things to work: a target and an action. We are talking about Objective C, and in objective c, we rarely do something without objects. By that, i mean you rarely write a function, and then call that function somewhere in your program. You will always create a class, then create an instance of that class and send a message to it (in objective C, we dont say something like invoke a method, or call a method, but we say sending a message).
    So, when your button got clicked, it need to send something a message, or equivalently, it need to communicate with something. That something is the button's target, and that message is the action.

    There are something i also want to tell you. A button, or a textfield in a nib file are objects. Precisely, when you create a button in Interface Builder, an instance of the NSButton class will be created at run time, when the nib file loaded.

    It is frustrating. I felt the same too when i first tried it. But be patient, and DONT RUSH, you will get it soon. Here are some books i would like to recommend:

    -First, you need to learn some basic Objective C, and a good book is this:

    Also, this one is very good too,

    And this one too:

    You need to know basic objective C (from the first book), and some Objective C's term like categories, protocols.....I find the third book explain protocols the best.

    Now for cocoa, i think the book "Learn Cocoa on the Mac" is a good one. Another one is "Cocoa® Programming for Mac® OS X (3rd Edition) by Aaron Hillegass", a new edition for this one will be released soon, just wait.

    Hope it helps and sorry for my bad writing.

Share This Page