views:

90

answers:

3

How can I send parameters to my function?

- (void)alertURL {
    NSLog(@"%@",url);
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    alertURL(url);
    return YES;
}

If there is anything else wrong, please tell me :)

A: 

Just like what you would do in C.

void alertURL(NSURL* url) {
  NSLog(@"%@",url);
}
Iamamac
+2  A: 

The proper way to define what you call a function, which is a method in Obj-C talk, is to add a colon and the type in parentheses and the parameter variable name.

Then to invoke the method, you use the square brackets.

- (void)alertURL:(NSURL *)url {
    NSLog(@"%@",url);
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    // Old C function: alertURL(url);

    [self alertURL:url];
    return YES;
}

Functions are still supported, they're just regular C functions, which mean they're not associated with any object. What you want to be doing is sending a message to the object using the square brackets. It's an Obj-C thing, you'll get used to it.

lucius
perfect, thanks!!
tarnfeld
+4  A: 

First, that isn't a function, that is an instance method. Instance methods can take arguments:

- (void)alertURL:(NSURL *)url {
    NSLog(@"%@",url);
}

Or, if you wanted to add more than one:

- (void)alertURL:(NSURL *)url ensureSecure: (BOOL) aFlag
{
    NSLog(@"%@",url);
    if (aFlag) { ... secure stuff ... }
}

Secondly, you don't call a method using function call syntax, you call it via method call syntax:

[self alertURL: anURL];
[self alertURL: anURL ensureSecure: YES];

Finally, the question indicates that you don't yet understand Objective-C. No worries -- we were all there once. Apple provides an excellent introduction to Objective-C.

bbum