iOS 的 c++,c,obj-C,swift 混用 (转载)
来源:互联网 发布:centos 编译安装lnmp 编辑:程序博客网 时间:2024/04/24 07:17
转载自: https://stackoverflow.com/questions/32541268/can-i-have-swift-objective-c-c-and-c-files-in-the-same-xcode-project/32546879#32546879
Can I have Swift, Objective-C, C and C++ files in the same Xcode project?
Can all 4 languages be used in the same project at all, and if so how?
There are similar questions in the flavor: Can I mix Swift with C++? Like the Objective - C .mm filesto which the accepted answer is no.
Using Bridging Header
adequately, .h
that do not contain C++
statements, Objective-C
wrappers when .h
do contain C++
, .mm
files to do the actual wrapping of C++
classes, and .swift
, can the 4 languages (5 if you include Objective-C++
) build and link into a single executable?
YES.
You can mix Swift
, C
, C++
, Objective-C
& Objective-C++
files in the same Xcode project.
C
// Declaration: C.h#ifndef C_h#define C_h#ifdef __cplusplusextern "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 wrapper for 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
Objective-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.swiftfunc hello_swift(_ name: String) { print("Hello \(name) in Swift")}
Bridging-Header.h
Cannot import CPP.hpp
header file, not because of it's naming convention, but because it contains the class
keyword.
#import "C.h"#import "CPP-Wrapper.h"#import "Objective-C.h"#import "Objective-CPP.h"
Invocation from Swift
// Invoke Chello_c("World".cStringUsingEncoding(NSUTF8StringEncoding))// Can't Invoke C++ without a wrapper// CPP().hello_cpp("World".cStringUsingEncoding(NSUTF8StringEncoding))// Invoke C++ through Objective-CCPP_Wrapper().hello_cpp_wrapped("World")// Invoke Objective-CObjective_C().hello_objectiveC("World")// Invoke Objective-C++Objective_CPP().hello_objectiveCpp("World")// Invoke SwiftSwift().hello_swift("World")
.h (Headers)
(See item 3 in this Stack Overflow answer)
.h: this is the tricky part, since they are ambiguously used for all flavors of C, ++ or not, Objective or not. When a .h does not contain a single C++ keyword, like class, it can be added to the ...Bridging-Header.h, and will expose whatever function the corresponding .c or .cpp functionalities it declares. Otherwise, that header must be wrapped in either a pure C or Objective-C API.
Output
Hello World in CHello World in C++Hello World in Objective-CHello World in Objective-C++Hello World in Swift
Comments
Cy-4AH:
Yes. You only need wrap C++
into C
or Objective-C
to use in Swift
.
Tommy
Indeed, I have a project that does exactly that. C++
for the thrust of the abstract cross-platform model stuff with some C
parts underneath; Objective-C
to wrap the C++
classes for Swift
purposes, Swift
to bind all that to a subclass of NSDocument
, with some custom views that interrogate the C
stuff.
MaddTheSane
Added the extern "C"
wrapper as per your excellent suggestion. To invoke the C method void hello_c(const char * name)
from C++ method hello_cpp(const std::string& name)
, add #include "C.h"
and call hello_c(name.c_str());
.
Keith Adler
The new SO-32541268: Now with parameters!
- iOS 的 c++,c,obj-C,swift 混用 (转载)
- iOS 的 c++,c,obj-C,swift 混用 (用在解密服务器发过来的信息)
- <转载> c和obj-c如何混用
- Swift Objective-C混用
- c和obj-c如何混用
- C和obj-c 如何混用
- c和obj-c如何混用?
- c和obj-c如何混用?
- ios 删除线的obj-c实现和swift实现
- iOS: 学习笔记, Swift与Objective-C混用总结
- Swift学习之Objective-C 与 Swift 混用(1)
- iOS Obj-C
- [C++]C和C++的混用
- iOS学习爬坑记录13:查看内存使用情况的obj-c和Swift代码
- iOS复杂动画之抽丝剥茧(Objective-C & Swift)(转载)
- swift调用obj-c要注意的小问题
- 在swift项目中引入obj-c代码的注意事项
- iOS Obj-C General Info
- Eclipse自动编译NDK/JNI的三种方法
- Array常用方法总结
- No qualifying bean of type...found for dependency:expected at least 1 bean which qualifies... 异常解决方案
- SPI 串行外设接口
- babel es6转码的基本用法
- iOS 的 c++,c,obj-C,swift 混用 (转载)
- 浅谈区块链开发数字货币过程中环境变量问题
- cocos2d-x IOS11 删除文件 removeFile等修改
- webpack安装及基础配置
- @Autowired注解出错的原因
- 环境变量,include搜索路径,lib库搜索路径
- Anroid8.0安装未知来源应用
- hive,hbase命令行无法回格
- 机房逻辑捋一捋(二)