iOS Passing function as parameter with two input - objective-c

I am still studying for passing function as parameter.
Currently, I can do like this.
- (void)getLocation:(void (^)(CLLocation *location))didUpdateLocation andTimeOut:(void (^)(void))timeout andDidFailUpdate:(void (^)(void))didFailUpdate
What I want to know is that "didUpdateLocation" can have multiple parameter? (Instead of just CLLocation *location) ? Currently, I can't find about syntax for that.

Since this is a block you have defined yourself, it can have as many parameters as you want. Just add the parameter you want to the parameter list of the block like this:
- (void)getLocation:(void (^)(CLLocation *location, NSString *otherParameter))didUpdateLocation andTimeOut:(void (^)(void))timeout andDidFailUpdate:(void (^)(void))didFailUpdate

Related

iOS - Method With Success And Error Callbacks Without Initial Parameter

I use success and error callback blocks a lot in method definitions with initial parameters like so:
+(void)doSomethingWithObject:(MyObject*)myObject successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;
where myObject is the initial parameter. However, I have come across a situation right now where I don't need any parameters. I'm trying to define my method like so:
+(void)getSomeData successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;
But now Xcode is giving me some syntax complaints. How can I define a method without any initial parameters but also having a success and error callback? Is this impossible or is there just something I don't understand about the correct syntax?
You should edit to
+(void)getSomeDataSuccessCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;

Runtime method to get names of argument variables?

Inside an Objective-C method, it is possible to get the selector of the method with the keyword _cmd. Does such a thing exist for the names of arguments?
For example, if I have a method declared as such:
- (void)methodWithAnArgument:(id)foo {
...
}
Is there some sort of construct that would allow me to get access to some sort of string-like representation of the variable name? That is, not the value of foo, but something that actually reflects the variable name "foo" in a local variable inside the method.
This information doesn't appear to be stored in NSInvocation or any of its related classes (NSMethodSignature, etc), so I'm not optimistic this can be done using Apple's frameworks or the runtime. I suspect it might be possible with some sort of compile-time macro, but I'm unfamiliar with C macros so I wouldn't know where to begin.
Edit to contain more information about what I'm actually trying to do.
I'm building a tool to help make working with third-party URL schemes easier. There are two sides to how I want my API to look:
As a consumer of a URL scheme, I can call a method like [twitterHandler showUserWithScreenName:#"someTwitterHandle"];
As a creator of an app with a URL scheme, I can define my URLs in a plist dictionary, whose key-value pairs look something like #"showUserWithScreenName": #"twitter://user?screenName={screenName}".
What I'm working on now is finding the best way to glue these together. The current fully-functioning implementation of showUserWithScreenName: looks something like this:
- (void)showUserWithScreenName:(NSString *)screenName {
[self performCommand:NSStringFromSelector(_cmd) withArguments:#{#"screenName": screenName}];
}
Where performCommand:withArguments: is a method that (besides some other logic) looks up the command key in the plist (in this case "showUserWithScreenName:") and evaluates the value as a template using the passed dictionary as the values to bind.
The problem I'm trying to solve: there are dozens of methods like this that look exactly the same, but just swap out the dictionary definition to contain the correct template params. In every case, the desired dictionary key is the name of the parameter. I'm trying to find a way to minimize my boilerplate.
In practice, I assume I'm going to accept that there will be some boilerplate needed, but I can probably make it ever-so-slightly cleaner thanks to NSDictionaryOfVariableBindings (thanks #CodaFi — I wasn't familiar with that macro!). For the sake of argument, I'm curious if it would be possible to completely metaprogram this using something like forwardInvocation:, which as far as I can tell would require some way to access parameter names.
You can use componentsSeparatedByString: with a : after you get the string from NSStringFromSelector(_cmd) and use your #selector's argument names to put the arguments in the correct order.
You can also take a look at this post, which is describing the method naming conventions in Objective C

MKNetworkOperation add params list

How to add params to the MKNetworkOperation. When I use a NSDictionary it does not work because I would like to have something like this:
username=blabla&nr=1&nr=3&nr=4
nr is a list.
So I cannot treat nr as a key because it is not unique.
How can I solve this?
According the MKNetworkkit Multivalued parameters should be possible?
It is beyond me why you are not simply using NSURLConnection, so I can only guess what your third party framework is or is not capable of.
If I read the limitation you describe correctly, one solution that comes to mind is to just device your own scheme, e.g. like this:
username=blabla&nr=1,2,3,5,8,13,21
and then parse the number list yourself.
You can always construct the URL manually. If you pass nil in the 'params' argument of the operationWithPath:params:httpMethod:ssl:, the framework will leave the URL alone and not try and append anything at the end. You can start by using the NSDictionary+RequestEncoding.h methods to encode the initial url on an NSMutableString and then append the rest at the end.

Defining protocols without parameters

I am trying to define a protocol method without adding parameters but couldn't find the correct syntax.
Here is the definition (it has a syntax error)
- (void)cameraOverlayView:(CameraOverlayView *)cameraOverlay didTakePhoto;
I don't want to pass any values with the second parameter. My aim is only to signal that something happened to the delegate instance.
How should I write the definition?
Your the second part of the method is not formatted correctly:
- (void)cameraOverlayView:(CameraOverlayView *)cameraOverlay didTakePhoto;
Because of the space, it's expecting a parameter. Instead, work the didTakePhoto part into the method name, like:
- (void)cameraOverlayViewDidTakePhoto:(CameraOverlayView *)cameraOverlay;
- (void)cameraOverlayViewDidTakePhoto:(CameraOverlayView *)cameraOverlay;
basically in objective c you can't have method name parts dangling after parameters...
so:
illegal:
-(void)methodWith:(int)theInt forMyMom;
normal:
-(void)methodForMyMomWithInt:(int)theInt;
legal but strange
-(void)method:(int)theInt :(int)theOtherInt;
with the selector: #selector(method::)
This is an issue of Objective-C convention. You could rewrite it as:
- (void)cameraOverlayView:(CameraOverlayView *)cameraOverlayViewDidTakePhoto;

What is the definition of ":=" in vb

I came across some sample code in VB.Net which I have some experience with and kinda having a duh moment figuring out the meaning of :=.
RefreshNavigationImages(bForward:=True, startIndex:=-1)
The sig for this method is RefreshNavigationImages(boolean, int). Is this a default value if null? Like "bIsSomething ?? false"?
Tried to bing/google but they just don't like searching for operators especially if it's only 2 chars.
They are named parameters. They let you specify values for arguments in function calls by name rather than order.
The := indicates the use of named parameters. Rather than relying on the order of the parameters in the method declaration, named parameters allow you to specify the correlation of parameters to values by specifying the name of the parameter.