Why does Objective C not have simple if/then and simple loops

Discussion in 'Mac Programming' started by begdev, May 17, 2009.

  1. begdev macrumors newbie

    Joined:
    Mar 14, 2009
    #1
    I've been programming in C++ for a few months, and I find it really easy actually.

    I wanted to make an Cocoa Application for Mac OSX, but realize that it is much more complicated.

    I have some check boxes and when they are selected and the user clicks a button, it will assign a variable to something.

    In C++ i would roughly have

    Code:
    if (checkboxOne == ON) {
    int a = 23;
    }
    
    What would be the objective c equivalent?

    (I've looked through the Apple Documentation and also the Cocoa Documentation, but can't find much on it)

    Also, why is this goal so much more complicated in ObjC?

    EDIT: I read some of a nother book and found out that it is similar with foundation tools where you are editing main.m like in C++. but i would like to know how to do it in a cocoa application.
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Objective-C includes all of C; it uses the same loops, conditionals, etc... that you're already used to.

    The equivalent code to your C++ would be:

    Code:
    if ([checkboxOne state] == NSOnState) {
    int a = 23;
    }
    
    That said, I'm not aware of any C++ framework where that would actually accomplish the goal you're aiming for, and it doesn't in Cocoa either.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    As far as I know, the only loop structure in Objective-C that is not present in C is the for...in structure introduced in Objective-C 2.0.

    http://developer.apple.com/document....html#//apple_ref/doc/uid/TP30001163-CH18-SW3

    Otherwise, carry on as normal. If you choose, it would be nice to use BOOL, YES and NO instead of depending on the 0 means false C-ism, but you don't have to.

    -Lee

    Edit: As Catfish_Man said, state will check the state of a checkbox, which may have been your real question. You may need to spend some time working with interface builder, and the docs to get the hang of this. Also, it would be best to forget whatever you might be used to when approaching a new API, because it may be so different that your existing experience might lead you astray, or to approach a problem incorrectly. Here is the documentation for state:
    http://developer.apple.com/document...nce.html#//apple_ref/occ/instm/NSButton/state
     
  4. begdev thread starter macrumors newbie

    Joined:
    Mar 14, 2009
    #4
    and that can be used in an objective-c class file? How exactly do I say checkboxOne exsists? would i go into my class file and do that thing like:
    [NSButton] *checkboxOne
    (I know that is terribly inccorect, but the thing I'm talking about is similar to it...)

    Then I would put that code in, and then a would be 23, if the box is checked.

    Also a little bit more help (theory) is needed...

    How about would I get it to check the if's after a button has been clicked. I am trying to have like a window with the check boxes and then the button and then the user selects their options, and clicks the button to bring them to the next "page".

    thanks.
     
  5. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    By far the easiest way to set up UIs is with Interface Builder, which you then talk to from your code via IBOutlets and IBActions. I would *really* recommend going through some basic Cocoa tutorials (such as at cocoadevcentral.com, or even Apple's currency converter tutorial).
     
  6. begdev thread starter macrumors newbie

    Joined:
    Mar 14, 2009
    #6
    I've already done the currency converter, but i will check out those too.

    Thanks again.

    EDIT: also, do a lot of people have trouble with this, or am i just stupid? haha.
     
  7. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #7
    If you're coming from C++, I guess the main insight to get you going in Objective-C is that

    Code:
    object->method();
    in Objective-C is spelled

    Code:
    [object method];
    and that when passing arguments to methods, instead of

    Code:
    object->methodWithArguments(arg1, arg2);
    it's

    Code:
    [object methodWithArgument1:arg1 andArgument2:arg2];
    The "bindings" in C++ aren't standardized; so in your original code, it depends on the platform how you make sure that checkBoxOne is properly initialized with the state of the real checkbox; probably something like

    Code:
    CheckBoxState checkBoxOne = m_myCheckBox->State();
    If you are used to building UIs "by hand", in code, you can still do it this way in Objective-C. It was "reassuring" to me that this is the case, but I went to the suggested Interface Builder way of doing things. Like others said, simply follow a few Cocoa tutorials to see how Interface Builder works, and how things get hooked up for you in actual code.
     
  8. vishvesh macrumors newbie

    Joined:
    Mar 17, 2008
    Location:
    India
    #8
    Hi,

    Objective c is an extension of c, so all the c programming rules would apply to it.

    Well the syntax is very painfully i ll have to give it to you :)
     

Share This Page