栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以在同一个Xcode项目中拥有Swift,Objective

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以在同一个Xcode项目中拥有Swift,Objective

YES.

您可以混合使用

Swift
C
C++
Objective-C
Objective-C++
中的文件相同的
Xpre
项目。

C

// Declaration: C.h#ifndef C_h#define C_h#ifdef __cplusplusextern "C" {#endif    void hello_c(const char * name);#ifdef __cplusplus}#endif#endif // 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;}

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-Cn", [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 newSO-32541268: Now
with parameters!


► Find this solution on
GitHub and additional details
on Swift Recipes.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号