0

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

1

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

0

Just like what you would do in C.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.