2

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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

What's improper about a function? He wants a function outside of an object, and last I checked that was OK (and useful).
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.
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.
StackOverflow self-moderation at its finest. =)
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.
|
6

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.

1 Comment

You're quite correct, C functions are perfectly legal in Obj-C. +1 for style correction as well.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.