tarnfeld
tarnfeld

Reputation: 26556

Obj-C Function Parameters

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 :)

Upvotes: 0

Views: 2051

Answers (3)

bbum
bbum

Reputation: 162722

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.

Upvotes: 4

iamamac
iamamac

Reputation: 10116

Just like what you would do in C.

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

Upvotes: 0

lucius
lucius

Reputation: 8685

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.

Upvotes: 4

Related Questions