Function / Subroutine in OBJC?

Discussion in 'iOS Development' started by Collateral, Oct 20, 2009.

  1. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    hmm. ive been looking online and i cant find what im looking for ..

    How would you write a function in OBJC?

    I read that it was:

    [OBJC]- ReturnType name (parameters)
    {
    Body
    }
    [/OBJC]


    So an example would be:
    [OBJC]
    - (void) betcheck () // void because its a mutator
    {
    // Checks UITextField's text against variable:
    if (txtBet.text > intBank)
    //Changes amount if necessary
    txtBet.text = intBank;
    }
    [/OBJC]


    But that function sends me these errors:


    What am i doing wrong?
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Objective-C uses smalltalk. The objects are passed messages. There are no "functions" in Objective-C. It's ridiculous, the name change for the exact same thing, but it is what it is. : /

    The problem with having is that you are not prototyping your method. You have to do that in ALL C based languages.


    EDIT: Also, the actual code within your function will not work at all.
  3. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    ok thanks. im moving to objc from java and visual basic and its a lot different. I'm basically doing trial and error.

    and as for the code in the attempted function, why not? Are there {}'s around body of the if statement?
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    You can't use binary operators (like +, -, ==, >, <, etc.) to compare NSStrings. You'll have to use the NSString method [OBJC]- (BOOL)isEqualToString

    Please Register or Log in to view images

    NSString *)aString
    //
    //
    //
    if ([firstString isEqualToString

    Please Register or Log in to view images

    therString]) {
    // strings are equal, do something
    }[/OBJC]
  5. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Interesting.

    Thank you for that, that would have taken me forever to figure out
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I'm assuming intBank is an integer. You might be better off doing
    [OBJC]if ([txtBet.text intValue] > intBank)
    txtBet.text = [[NSNumber numberWithInt:intBank] stringValue];[/OBJC]


    Although personally, if I were making an app invloving betting and a bank, I wouldn't use int. Instead, I'd opt to use either a long or a long long. I wouldn't go unsigned either because you want the users's bank to dip in the negative if he loses. How can he lose more money than he bet? Easy, doubling down, seeing bets, raising, anything is possible.
  7. Collateral

    Collateral Active Member

    Joined:
    Sep 23, 2007
    Messages:
    1,974
    Likes Received:
    6
    Device:
    iPhone 3GS (Black)
    Yea, that makes a lot of sense.

    Thanks Skylar

    Please Register or Log in to view images



    Ps. Its BlackJack, haha

Share This Page