[英]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項目中混合使用Swift
, C
, C++
, Objective-C
和Objective-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);
}
// 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;
}
// 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
// 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
// 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")
}
無法導入CPP.hpp
頭文件,不是因為它的命名約定,而是因為它包含class
關鍵字。
#import "C.h"
#import "CPP-Wrapper.h"
#import "Objective-C.h"
#import "Objective-CPP.h"
// 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")
(參見本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++
包裝成C
或Objective-C
即可在Swift
使用。
實際上,我有一個項目正是如此。 C++
是抽象的跨平台模型的主旨,下面是一些C
部分; Objective-C
用於為Swift
目的包裝C++
類, Swift
將所有這些類綁定到NSDocument
的子類,以及一些查詢C
內容的自定義視圖。
根據您的出色建議添加了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.