skip to Main Content

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

3

Answers


    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.

    Login or Signup to reply.
  1. 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
    }
    

    FYI :

    Also if you want to use Swift Files in Objective-C Files or mix of it, you need to have BridgeHeader File

    Follow answer suggested by @Pushkraj Lanjekar

    Hope it helps.

    Login or Signup to reply.
  2. The AppDelegate is a Swift file and you need to import it to an Objective-C file. Check the Apple’s guide importing swift into Objective-C, you need to:

    1. Import the genareted Swift header to the SettingsViewController.m

      #import "ThinkNotes-Swift.h"

    If the module name is not ThinkNotes then replace that with your projects module name.

    1. Make sure the Swift classes you need to access from Objective-C have the @objc attribute in their declaration.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search