What does (void (^)(BOOL supported)) mean? [duplicate] - objective-c

This question already has answers here:
Caret in objective C
(3 answers)
Closed 7 years ago.
as I am working myself into some new code, I came across a thing, where I couldnt find any explanation for in the web so far. So hopefully you can give me one.
I have this method signature in Objective-C code:
-(void) supportsUrl: (NSString*) url callback:(void (^)(BOOL supported)) callback;
Can someone please tell me what it is about in the last parameter?
Thanks a lot!

It is a block that takes a BOOL argument and returns void. See the documentation for more info on the syntax.
When invoking this method, you can provide a callback through this block. This will let you submit code to be executed after the method has run.
For example:
[self supportsUrl:#"http://www.google.com" callback:^(BOOL supported){
if (supported) NSLog(#"Yay, supported");
else NSLog(#"Nay, not supported");
}];

Related

Is there an ObjC macro or function to get all the argument values received by a method? [duplicate]

This question already has answers here:
Pass all arguments of a method into NSLog
(2 answers)
Closed 8 years ago.
I know that NSStringFromSelector(_cmd) gives the name of the current method. I use that in a debugging macro to print what method I'm in. I would like to also get the method's arguments as strings so I can print them too. I'm thinking of something like *argv[] in C.
Is there any built-in facility for that?
Yes, c++ (and Objective-C) support va_list.
Do something like this:
- (void)do:(va_list)args
{
// do something with all your args
}
And use it like this:
[self do:#"foo", #"bar"]

what does the caret sign mean in Objective-C? [duplicate]

This question already has answers here:
Caret in objective C
(3 answers)
Closed 9 years ago.
There is a piece of code like
typedef void (^SignIn) (NSString *email, NSString *password);
What does the ^ mean before SignIn? Is this Objective-C specific usage?
It's the syntax for blocks.
That typedef declares SignIn to mean a block which takes two NSString* arguments and returns void (i.e. nothing).
It is a block.
For a guide to understanding blocks, see this tutorial
Unless, you already know what a block is, and you just didn't know what the caret was for.

How to call a method in Objective-C? [duplicate]

This question already has answers here:
How can I call a method in Objective-C?
(8 answers)
Closed 9 years ago.
I am just starting out in ios and am trying to make user defined functions...
my code:
-(void) testFunction{
//has self. calls
}
-(IBAction)button:(id)sender {
testFunction();
}
I get the warning: Implicit declaration of function is invalid in C99
What am I doing wrong? Or what do I have to add to my code?
In Objective-C you cant write testFunction(). You send 'messages' and you can do this via
[self testFunction]

objective c method asterisk [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Asterisk in parenthesis in Objective-C… What does it mean?
does anyone can find some resource or detail explanation about following code
-(void) add:(Cal *)c;
I am very new to obj-c.what does asterisk means in that code?
Thanks.
It means that the argument for the method is a pointer to an instance of a "Cal" object.

Objective C compiler not complaining about syntax error [duplicate]

This question already has answers here:
Semicolon after the method name in Objective-C implementation file
(6 answers)
Closed 9 years ago.
I'm trying to figure out why gcc is OK with the following:
- (void) methodname: (id) sender;
{
// do stuff
}
Notice what's wrong here: There is a semicolon that should not be there.
I'm also finding that execution of the method is having bizarre results.
Anybody know what effect the semicolon is supposed to have or not have here?
Thanks.
The semi-colon is optional in the context of the #implementation. Some teams standardize on requiring it, some don't.
In general ; makes it a function prototype. But the compiler must be smart enough to ignore it in this case. I don't think it's the ; that's causing your odd issues but I'm not sure.