繁体   English   中英

我可以在同一个Xcode项目中使用Swift,Objective-C,C和C ++文件吗?

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

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

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

充分使用Bridging Header.h不包含C++语句, Objective-C包装器.h确实包含C++ .mm文件来实际包装C++类,和.swift ,可以使用4种语言(如果包含5种语言) Objective-C++ )构建并链接成单个可执行文件?


是。

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

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 C\n", 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;
}

Objective-C 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

Objective-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-C\n", [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

迅速

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

转职Header.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(标题)

(参见本Stack Overflow答案中的第 3项

.h:这是一个棘手的部分,因为它们被模糊地用于所有C,++或不同的目标,无论目标与否。 当.h不包含单个C ++关键字(如类)时,它可以添加到... Bridging-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使用。

汤米

实际上,我有一个项目正是如此。 C++是抽象的跨平台模型的主旨,下面是一些C部分; Objective-C用于为Swift目的包装C++类, Swift将所有这些类绑定到NSDocument的子类,以及一些查询C内容的自定义视图。

MaddTheSane

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

凯斯阿德勒

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


►在GitHub上找到此解决方案以及有关Swift Recipes的其他详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM