我可以有斯威夫特的Objective-C,C和C ++中相同的X code项目文件? [英] Can I have Swift, Objective-C, C and C++ files in the same Xcode project?

查看:184
本文介绍了我可以有斯威夫特的Objective-C,C和C ++中相同的X code项目文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can all 4 languages be used in the same project at all, and if so how?

类似的问题在味:<一href=\"http://stackoverflow.com/questions/24042774/can-i-mix-swift-with-c-like-the-objective-c-mm-files\">Can我用C ++混合斯威夫特?像目标 - C .mm文件到接受的答案是的没有

There are similar questions in the flavor: Can I mix Swift with C++? Like the Objective - C .mm files to which the accepted answer is no.

使用桥接报头充分, .H 不包含 C ++ 语句,的Objective-C 包装时 .H 确实包含 C ++ .mm 文件做 C ++ 类,实际包装和 .swift ,可以在4种语言(5如果包括的Objective-C ++ )建立并链接到一个单一的可执行文件?

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?

的Objective-C ++ X $ C $ ç

推荐答案

你可以混合斯威夫特 C C ++ 的Objective-C &安培; 的Objective-C ++ 在相同的X code项目文件。

YES.

You can mix Swift, C, C++, Objective-C & Objective-C++ files in the same Xcode project.

// 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;
}

对于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

的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

斯威夫特

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

转职Header.h

无法导入 CPP.hpp 头文件,不是因为它的命名惯例,但因为它包含了关键字。

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"

从斯威夫特调用

// 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")

输出

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 斯威夫特使用。

Yes. You only need wrap C++ into C or Objective-C to use in Swift.

汤米

事实上,我有一个项目,正是这么做的。 C ++ 为抽象的跨平台模式的东西与下面一些 C 部分推力; 的Objective-C 来包裹 C ++ 斯威夫特目的,斯威夫特绑定所有以 NSDocument 的一个子类,与询问 C 的东西。

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

新增了的externC包装按您的指教。要调用的 C 方式无效hello_c(为const char *名称) C ++ 方式 hello_cpp (常量标准::字符串&放大器;名称)添加的#includech并调用 hello_c(name.c_str( ));

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());.

基思·阿德勒

新的 SO-32541268 的:现在带参数

The new SO-32541268: Now with parameters!

经过测试的 X code 7 + 斯威夫特2 + 的iOS 9

要下载完整的项目,在的 SO-32541268 :/ /swiftarchitect.com/recipes\">Swift食谱。

To download the full project, search for SO-32541268 in Swift Recipes.

这篇关于我可以有斯威夫特的Objective-C,C和C ++中相同的X code项目文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆