0

I'm having problems executing some Swift code in an existing ObjC project. This is my first attempt at Swift so I'm sure I'm missing something simple.

I have added my new swift file to my project - this process generated the bridging header. So now I have the following:

bridging-header file

//
//  Use this file to import your target's public headers 
//  that you would like to expose to Swift.
//

#import "historyViewController.h"

In my swift file I have the following test class and function:

import Foundation

@objc class Hello: NSObject {
    func sayHello() {
        print("Hi There")
    }
}

In my historyViewController.m file I have the following

#import "xx-Bridging-Header.h"

In my historyViewController.h file I have the following

@class Hello;

How do I actually go about executing the sayHello function from within my historyViewController.m file? I've tried [Hello sayHello]; - but get 'no known class method'.

1 Answer 1

1

The problem is only that you are calling sayHello as if it were a class method, but sayHello is declared as an instance method. So:

[[Hello new] sayHello];
Sign up to request clarification or add additional context in comments.

5 Comments

Or the other way round: Make it a type method (aka class method).
How do I make it a type method?
When I do [[Hello new] sayHello]; I get error - Receiver 'Hello' for class message is a forward declaration
Wow, you're quite rude. Why you took the time I don't know.
Sorry about that. I'll delete that comment. But when you deleted @class Hello; how did things go? This is very easy stuff so it's a pity you're having so much difficulty getting started with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.