Functions in objective-c

Discussion in 'iOS Programming' started by skunkio, May 2, 2010.

  1. skunkio macrumors newbie

    Joined:
    Apr 25, 2010
    #1
    Hi all,
    as per objecst i have one basic questions about funtions in my code.
    I wrote some private functions in some classes of mine but i don't find a way to store in a variable the return of a function. Somethine like that:

    result = [self myPrivateFunction];

    where myPrivateFunction is defined like:

    - (BOOL) myPrivateFunction: (NSString *) parm1 Parameter1: (NSString *) parm2


    Ciao,
    stè
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    1) They are not functions: they are methods

    2) Can you post the actual code, using code tags, what you expect to happen and what is actually happening.
     
  3. skunkio thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #3
    Functions and methods are not really the same things.
    A function is a routine that has inside a specific operation (for example) as, sum two numbers, that you can reuse in your code. In this case every time you need to sum two numbers you can use this function.
    A method is used to identify a specific "action" is exposed by an object (method can be executed for object istance of a specific class).
    A function becames a method if, following the sum example, you create a calculator calss and sum will a method of it (otherwise hey are two differente things).

    class x
    Code:
    - (BOOL) Method_A
    {
    	BOOL result;
    	
    	result = [self function:parm1 parm2];
    
    	if (result == TRUE) {
    		NSLog(@"true");
    	} else {
    		NSLog(@"false");
    	}
    }
    
    - (BOOL) function: (NSString *) a Param2: (NSString *) b 
    {
            BOOL result;
    
    	if (a == b) {
    	  result = true;
            } else {
              result = FALSE;
            }
    
            return result;
    }
    
    with a code like this (this is an example) but my real code has the same structure, i receive this error:

    error: expected ':' before ']' token

    for the line

    result = [self function:parm1 parm2];

    is it the right way to have back the function result?! Maybe not... :confused:


    ciao,
    stè
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    I know methods and functions are not the same. You don't seem to know the difference. In your example above there are zero functions and two methods. A method is a "function" that is part if a class. There are two types of methods: class and instance. A function does not belong to a class and is declared using normal C function syntax (as they are C, not Objective-C constructs).

    From your code I can tell that you fundamentally have no idea what you are doing at all. Your method call to function:param2: (which is a method, not a function) is illegal: you do not have the second param name (which is optional) or the second parameter positional specified (which is not optional).

    This is technically a legal call:
    Code:
    [self function:parm1 :parm2];
    
    although pretty much every single person would write it as:

    Code:
    [self function:parm1 Param2:parm2];
    
    I suggest you go and read the basics of Objective-C. Apple provide a very good guide.
     
  5. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #5
    You have to label all parameters with a selector

    Instead of:

    Code:
    	result = [self function:parm1 parm2];
    
    try:

    Code:
    	result = [self function:parm1 Param2:parm2];
    
     
  6. Avizzv92 macrumors regular

    Joined:
    Mar 23, 2008
    #6
    Ignore the post...

    I see it has been answered already.
     
  7. skunkio thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #7
    In this case you are right because in my class function is a method; i don't know how to specify in objectice-c something like protected, public, private. In my example initializing my class you can call both methods.
    About "A function does not belong to a class" i'm not agree because i can define a private method (so nobody can use it setting my object, it's like a private function within my class) and reuse it inside the same class.

    I didn't get properly the first line but i understood the error.

    I will do asap because i'm starting now with objective-c.


    ciao,
    stè
     
  8. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #8
    He's basically saying:

    Wrong:
    Code:
    [self function:parm1 parm2];
    Right:
    Code:
    [self function:parm1 :parm2];
    Right:
    Code:
    [self function:parm1 Param2:parm2];
     
  9. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    A method (Obj C message) is just a funny syntax for a function with a hidden parameter (usually a pointer to an object or class). Not much diff if you know how the Obj C preprocesser and runtime work.
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    I agree, but conceptually a method and a function are different things. Deciding to call internal, private methods functions is not a good idea.
     

Share This Page