【发布时间】: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++