Home > Back-end >  Calling Swift Function in objective C
Calling Swift Function in objective C

Time:10-10

Extremely new to Swift and Objective C. My manager has tasked me with reposting an outdated app back onto the app store. It was removed due to some Apple updates and our app didn't meet the new qualifications. But I've been having issues just trying to compile the project. So I by first updating the project by running updates on its old software like XCode. But now I'm stuck at this issue of the AppDelegate Swift functions can't be seen in the objective C code. However, when I right-click and go to definition, it has has no problem finding them. This is one example.

Objective C

Swift

CodePudding user response:

  1. You need to add @objc as prefix for variables or functions which you would like to access from Swift file to Objective C or mark class as @objcMembers if you want to access everything from swift file to Objective c

  2. make sure you have created a bridging header file. Here is helper link

  3. You need to import that bridging header in your Objective C file.

If you have do all three steps as mentioned, you will be able to access that function.

CodePudding user response:

In your code you are accessing instance methods as class methods

Error itself saying that

No known class method for selector 'myBlue'

Follow below steps

@property(nonatomic,strong) IBOutlet AppDelegate *appDelegate; // .h file

self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; // .m file. //ViewDidLoad Method

Now you can access method as

[self.appDelegate myBlue]

Else

[delegate myBlue] // as per your code

Finally you need to know difference between Class & Instance Methods

-(void)someInstanceMethod{
    //Whatever you want your instance method to do here
}

 (void)someClassMethod{
    //Whatever you want your class method to do here
}

Hope it helps.

  • Related