Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a string to a function in Objective-C

I made a method like this:

-(void) doSomething:(NSString *)str
{

}

I call it like this

doSomething(foo);

It doesn't work.

like image 688
node ninja Avatar asked Sep 24 '10 23:09

node ninja


2 Answers

The way that you call methods in objective c is like the following

[class method:parameter];

In your case, to call doSomething, you would do this:

[self doSomething:@"foo"];
like image 76
Daniel G. Wilson Avatar answered Nov 16 '22 20:11

Daniel G. Wilson


That is because doSomething is a method of an Objective-C class. The C syntax for function calls doesn't apply here and you need an instance to call it on, e.g.:

[instance doSomething:foo];

I strongly recommend to read through Apples The Objective-C programming language.

like image 25
Georg Fritzsche Avatar answered Nov 16 '22 19:11

Georg Fritzsche