Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

skunkio

macrumors newbie
Original poster
Apr 25, 2010
28
0
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è
 
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.
 
1) They are not functions: they are methods

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).

2) Can you post the actual code, using code tags, what you expect to happen and what is actually happening.

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è
 
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.
 
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];
 
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).

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.

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).

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

I suggest you go and read the basics of Objective-C. Apple provide a very good guide.

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


ciao,
stè
 
I didn't get properly the first line but i understood the error.

He's basically saying:

Wrong:
Code:
[self function:parm1 parm2];

Right:
Code:
[self function:parm1 :parm2];

Right:
Code:
[self function:parm1 Param2:parm2];
 
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.

I agree, but conceptually a method and a function are different things. Deciding to call internal, private methods functions is not a good idea.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.