tags:

views:

157

answers:

3

I am trying to write a function which returns a string created from two input strings;

but when I try the function declaration

   NSString Do_Something(NSString str1, NSString str2)
   {

   }

the compiler gets sick. (Worked fine for a different function with int arguments.)

If I change the input arguments to pointers to strings, in also gets sick.

So how do I pass Objective-C objects into a function?

+8  A: 

All Objective-C objects being passed to functions must be pointers. Rewriting it like this will fix your compiler error:

NSString *Do_Something(NSString *str1, NSString *str2) { }

Also, please keep in mind that this is a (C-style) function and not an instance method written on an Objective-C object. If you wanted this to actually be a method on an object it would probably look something like this:

NSString *doSomethingWithString1:(NSString *)str1 string2:(NSString *)str2 { }

I say "probably" because you can name it however you want.

Marc W
What's improper about a function? He wants a function outside of an object, and last I checked that was OK (and useful).
Jed Smith
We don't actually know where he wants to use the function. Sometimes people new to a programming language don't realize that there's another way to do things. This is especially the case when someone moves from C to Objective-C and keeps using C-style functions everywhere. I gave corrections for both cases since the OP didn't explicitly mention the context in which he was writing the function.
Marc W
I tend to assume people know what they're doing, but I agree that this question could have gone either way. However, it was your word *properly* which killed the answer for me.
Jed Smith
You're probably right. Nothing a simple edit cannot fix.
Marc W
And voila, -1 removed. :)
Jed Smith
StackOverflow self-moderation at its finest. =)
Marc W
I appreciate all the help. I never came through C: I got here by means of Pascal (DELPHI), so I am just experimenting with what can be done. There are times and places in Objective_C where a functional notation can be a good bit more succinct than the usual method notation, and so I am looking at that sort of situation.
John R Doner
+6  A: 

Functions are perfectly fine in Objective-C (and in fact earn some of the language's benefits).

See my answer to C function always returns zero to Objective C, where someone was trying what you are and had a problem with the compiler assuming return type. The structure that I set up there is important when you are using functions, just like when you are using objects and methods. Be sure to get your headers right.

To be pedantic, you're using a function definition of:

NSString *DoSomething(NSString *str1, NSString *str2) {
    // Drop the _ in the name for style reasons
}

And you should be declaring it in a .h file like so:

NSString *DoSomething(NSString *str1, NSString *str2);

Just like C.

Jed Smith
You're quite correct, C functions are perfectly legal in Obj-C. +1 for style correction as well.
Abizern
A: 

that doesn't work for me. i've just declared in the .h: NSString *myFunction(NSDecimal *value);

and i type in the .m: NSString *myFunction(NSDecimal *value){ //code }

but always i get an error saying expected '(' before '*' token

now is fixed. for some reason... sorry.

Néstor