0

I'm currently taking several Swift classes online to figure this new syntax out, but I feel like I've run into a wall that I can't really figure out.

My question is: What do I need to do to call custom Objective-C methods inside Swift? Why doesn't Swift recognize where the code is coming from? Why do I get errors like, "UIViewController does not have a member named 'customMethod'"? Is it an import problem?

Now that you know my question, here is why I'm asking and the background

I have an objective-c .m file that calls this method:

- (void)configureMyApp { // overridden by subclasses } 

Then this one:

- (void)addAppElement:(MyAppElement *)element { [self addAppElement atIndex:[self.elements count]]; } 

In my Swift app I've set up the bridging header and imported the .h file into said header file

I attempted calling the configureMyApp method through this Swift function:

func configureMyApp() { self.addAppElement... } 

but it doesn't recognize that function as coming from my imported objective-c file, and neither does myswiftfile.swift recognize addAppElement

The way I would call it in obj-c would be this:

- (void)configureMyApp { [super configureMyApp]; [self addAppElement:.....]; } 

Is there anything else I need to do to import the obj-c code? What could I be missing?

3
  • 1
    Do you have your bridging header configured properly? See Using Swift with Cocoa and Objective-C: Swift and Objective-C in the Same Project. Commented Oct 10, 2014 at 21:07
  • 1
    Did you added those methods to your ViewController.h file? Commented Oct 10, 2014 at 21:08
  • yes those methods are in my ViewController.h file, and I'm positive the bridging header is configured properly as I am already using a snippet of code that needs the bridging header Commented Oct 10, 2014 at 21:13

1 Answer 1

1

This error: UIViewController does not have a member named 'customMethod' indicates that you're trying to call customMethod on an object typed as UIViewController, not as your custom subclass of UIViewController.

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

1 Comment

got it that did the trick! I forgot you needed to change the subclass

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.