我可以在同一个Xcode项目中拥有Swift、Objective-C、C和C++文件吗

Can I have Swift, Objective-C, C and C++ files in the same Xcode project?

本文关键字:Objective-C C++ 文件 Swift 同一个 Xcode 项目 拥有 我可以      更新时间:2023-10-16

是否可以在同一个项目中使用所有4种语言,如果可以,如何使用?

类似的问题:我可以将Swift与C++混合吗?就像Objective-C.mm文件一样,接受的答案是

充分使用Bridging Header、不包含C++语句的.h、当.h包含C++时的Objective-C包装器、用于实际包装C++类的.mm文件以及.swift,这4种语言(如果包含Objective-C++,则为5(是否可以构建并链接到单个可执行文件中?


objective-c++xcode

您可以混合使用SwiftCC++Objective-C&Objective-C++文件在同一Xcode项目中

C

// Declaration: C.h
#ifndef C_h
#define C_h
#ifdef __cplusplus
extern "C" {
#endif
    void hello_c(const char * name);
#ifdef __cplusplus
}
#endif
#endif /* C_h */
// Definition: C.c
#include "C.h"
#include <stdio.h>
void hello_c(const char * name) {
    printf("Hello %s in Cn", name);
}

C++

// Declaration: CPP.hpp
#pragma once
#include <string>
class CPP {
public:
    void hello_cpp(const std::string& name);
};
// Definition: CPP.cpp
#include "CPP.hpp"
#include <iostream>
using namespace std;
void CPP::hello_cpp(const std::string& name) {
    cout << "Hello " << name << " in C++" << endl;
}

C的Objective-C包装器++

// Declaration: CPP-Wrapper.h
#import <Foundation/Foundation.h>
@interface CPP_Wrapper : NSObject
- (void)hello_cpp_wrapped:(NSString *)name;
@end
// Definition: CPP-Wrapper.mm
#import "CPP-Wrapper.h"
#include "CPP.hpp"
@implementation CPP_Wrapper
- (void)hello_cpp_wrapped:(NSString *)name {
    CPP cpp;
    cpp.hello_cpp([name cStringUsingEncoding:NSUTF8StringEncoding]);
}
@end

目标-C

// Declaration: Objective-C.h
#import <Foundation/Foundation.h>
@interface Objective_C : NSObject
- (void)hello_objectiveC:(NSString *)name;
@end
// Definition: Objective-C.m
#import "Objective-C.h"
@implementation Objective_C
- (void)hello_objectiveC:(NSString*)name {
    printf("Hello %s in Objective-Cn", [name cStringUsingEncoding:NSUTF8StringEncoding]);
}
@end

目标-C++

// Declaration: Objective-CPP.h
#import <Foundation/Foundation.h>
@interface Objective_CPP : NSObject
- (void)hello_objectiveCpp:(NSString *)name;
@end
// Definition: Objective-CPP.mm
#include <iostream>
#import "Objective-CPP.h"
using namespace std;
@implementation Objective_CPP
- (void)hello_objectiveCpp:(NSString *)name {
    cout << "Hello " << [name cStringUsingEncoding:NSUTF8StringEncoding] << " in Objective-C++n";
}
@end

Swift

// Declaration & definition: Swift.swift
func hello_swift(_ name: String) {
    print("Hello (name) in Swift")
}

桥接标头.h

无法导入CPP.hpp头文件,不是因为它的命名约定,而是因为它包含class关键字

#import "C.h"
#import "CPP-Wrapper.h"
#import "Objective-C.h"
#import "Objective-CPP.h"

Swift调用

// Invoke C
hello_c("World".cStringUsingEncoding(NSUTF8StringEncoding))
// Can't Invoke C++ without a wrapper
// CPP().hello_cpp("World".cStringUsingEncoding(NSUTF8StringEncoding))
// Invoke C++ through Objective-C
CPP_Wrapper().hello_cpp_wrapped("World")
// Invoke Objective-C
Objective_C().hello_objectiveC("World")
// Invoke Objective-C++
Objective_CPP().hello_objectiveCpp("World")
// Invoke Swift
Swift().hello_swift("World")

.h(标头(

(请参阅此堆栈溢出答案中的第3项(

.h:这是一个棘手的部分,因为它们被模糊地用于C的所有风格,++与否,Objective与否。当.h不包含一个C++关键字时,就像类一样,它可以添加到。。。桥接Header.h,并将公开它声明的任何函数的相应.c或.cpp功能。否则,该头必须包装在纯C或Objective-C API中。

输出

Hello World in C
Hello World in C++
Hello World in Objective-C
Hello World in Objective-C++
Hello World in Swift

评论

Cy-4AH

是的。您只需要将C++包装成CObjective-C即可在Swift中使用。

Tommy

事实上,我有一个项目正是这样做的。CCD_ 22用于抽象跨平台模型的推力,下面有一些CCD_;Objective-C用于封装C++类以达到Swift的目的,Swift用于将所有这些绑定到NSDocument的子类,并具有一些查询C内容的自定义视图。

MaddTheSane

根据您的建议添加了extern "C"包装。要从C++方法hello_cpp(const std::string& name)调用Cvoid hello_c(const char * name),请添加#include "C.h"并调用hello_c(name.c_str());

Keith Adler

新的SO-32541268:现在有参数了!


►在GitHub上找到这个解决方案,并在Swift Recipes上找到更多详细信息。