【问题标题】:using swift (4.0) class from objective-c++使用来自 Objective-C++ 的 swift (4.0) 类
【发布时间】:2018-02-20 03:39:52
【问题描述】:

如何在ios-project中使用objective-c++中的swift类?

Apple 文档只讨论目标 c:https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

我试过这样做 -

Console.swift

public class Console : NSObject {
  public func Dump() -> Void {
      let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Console")
      os_log("url = %@", log: log, "Some thing here")
  }
}

ConsoleDelegate.hpp

#ifndef ConsoleWrapper_h
#define ConsoleWrapper_h
#import <Foundation/Foundation.h>
#import <Interop-Swift.h>

@interface ConsoleDelegate : NSObject {
@private
  Console* console;
}

- (ConsoleDelegate*) init;
- (void) DumpWrapper;
@end
#endif /* ConsoleWrapper_h */

ConsoleDelegate.mm

#import "ConsoleDelegate.hpp"
#import <Foundation/Foundation.h>

@implementation ConsoleDelegate

- (ConsoleDelegate*) init {
  console = [[Console alloc] init];
  return self;
}

- (void) DumpWrapper {
  [console Dump];
}
@end

但我得到了类似的错误 -

/path/to/file/Interop-Swift.h:192:39:未命名类型或协议 'UIApplicationDelegate'

如果我重命名 ConsoleDelegate.mm -> ConsoleDelegate.m,那么它编译时不会出现任何错误。

【问题讨论】:

  • 尝试将 swift 类暴露给 Objective C,然后从 Objective C 暴露给 Objective C++。你可能还想看看stackoverflow.com/questions/24042774/…
  • 我也试过了。当我尝试在目标 C++ 实现文件中导入目标 C 头文件并抱怨相同的错误时,编译器不高兴 - “没有名为 UIApplcationDelegate 的类型或协议”
  • UIApplicationDelegate - 你是不是想在 MacOS 上使用 iOS 代码?
  • 这是一个 iOS 项目,Xcode 将从我的 MacOS 交叉编译 iOS。

标签: swift interop objective-c++


【解决方案1】:

好吧,我所要做的就是在 ConsoleDelegate.hpp 中包含 UIKit

#ifndef ConsoleWrapper_h
#define ConsoleWrapper_h

#import <UIKit/UIkit.h>
#import <Foundation/Foundation.h>
#import <Interop-Swift.h>

@interface ConsoleDelegate : NSObject {
@private
  Console* console;
}

- (ConsoleDelegate*) init;
- (void) DumpWrapper;
@end
#endif /* ConsoleWrapper_h */

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多