0

I need to write these two Swift functions in Objective-C and then call the returned values in Swift how do i go about doing this?

Also where do i need to put them in my AppDelegate?

Sorry this might be really easy but I have no experience using Objective-C.

Thanks in advance!

Functions to convert:

class func getAppDelegate() -> AppDelegate {
    return UIApplication.shared.delegate as! AppDelegate
}

func getDocDir() -> String {
    return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}

AppDelegate.m:

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication*)application 
 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{

}
@end

AppDelegate.h:

#import <Cordova/CDVViewController.h>
#import <Cordova/CDVAppDelegate.h>

@interface AppDelegate : CDVAppDelegate {}


@end
3
  • Why do you need to cross over into ObjC like this? Commented Jul 5, 2018 at 10:42
  • It is an objective C project and i need those two values in a swift file I am building but was told cant have a swift and objective C appDelegate Commented Jul 5, 2018 at 10:44
  • @alimcc56 but why? "and then call the returned values in" Commented Jul 5, 2018 at 18:12

3 Answers 3

1

Here is your AppDelegate class written with objective-c including your Swift method. But we need to know more detail to help.

AppDelegate.h

#import <Cordova/CDVViewController.h>
#import <Cordova/CDVAppDelegate.h>

@interface AppDelegate : CDVAppDelegate {}

+ (id)getAppDelegate;
- (NSString*)getDocDir;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication*)application 
 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    return YES;
}

+ (id)getAppDelegate {
    return self;
}

- (NSString*)getDocDir {
    return NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
}
@end
Sign up to request clarification or add additional context in comments.

5 Comments

The function getAppDelegate() is a class method which returns the current instance of AppDelegate. If you make it an instance method, you need to access the current instance...
A little more, - to + for getAppDelegate.
What do you mean @OOPer?. Self and [[UIApplication sharedApplication] delegate] are same.
When you keep it as an instance method, you need to use it something like [anAppDelegate getAppDelegate], how do you get anAppDelegate to call the method? It's called from outside of the AppDelegate class.
@OOPer I got what you meant, and updated my answer thanks.
0

You need bridging header and thats it. Import your AppDelegate in bridging header file. The bridging header file is an interface for sharing code from objective C to Swift. So when you add any swift file to objective C project it will prompt you for adding this header file. e.g. ProjectName-Bridging-Header.h. If its not there check how to create it manually here.

Then import all needed objective C files in this header as:

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

#import "Appdelegate.h"

Then you can add your mentioned functions in your swift file itself.

Hope it helps...

Comments

0

Ali's post seems to be the right answer for what you asked:

write these two Swift functions in Objective-C and then call the returned values in Swift

But if you mean you want to call the two methods only from your Swift code, why don't you define it in an extension.

AppDelegate+.swift:

import UIKit

extension AppDelegate {
    class func getAppDelegate() -> AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }

    func getDocDir() -> String {
        return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    }

}

Of course, you need #import "AppDelegate.h" in your "...-Bridging-Header.h" .

You can use them as were in your own AppDelegate.

Comments

Your Answer

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