ios - 将 Swift 函数转换为 Objective C 并在 swift 中使用返回值

标签 ios objective-c swift

我需要在 Objective-C 中编写这两个 Swift 函数,然后在 Swift 中调用返回值我该怎么做这个?

此外,我需要将它们放在我的 AppDelegate 中的什么地方?

抱歉,这可能真的很简单,但我没有使用 Objective-C 的经验。

提前致谢!

要转换的函数:

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

最佳答案

这是您使用 objective-c 编写的 AppDelegate 类,包括您的 Swift 方法。但我们需要了解更多细节才能提供帮助。

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

关于ios - 将 Swift 函数转换为 Objective C 并在 swift 中使用返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51189255/

相关文章:

iphone - CorePlot 图在 iOS6 中错位

ios - 为什么 swift 协议(protocol)使用函数重载而不是不同名称的 func ?

swift - 检测 UITextField 中的提及不起作用

ios - 子类化 UITextField 并检测它何时成为或退出第一响应者

iphone - 如何在 xcode 4.2.1 控制台窗口中显示日志消息

ios - 从 Swift 2.3 (xCode 7) 迁移 --> Swift 4(xCode 9) + 更新 Cocoapods

ios - iOS 11 中的 UITableViewCell 转换中断

ios - "Invalid use of ' 这个 ' in non-member function"在 objective-c 上下文中?

iphone - 在 Game Center 上验证本地用户时出现延迟

ios - 为日程应用程序设计 UI (ios Swift)