Swift, Objective-C, C, C++ 파일을 같은 Xcode 프로젝트에 넣을 수 있습니까?
4개 언어를 모두 같은 프로젝트에서 사용할 수 있습니까?그렇다면 어떻게 사용할 수 있을까요?
Swift와 C++를 섞어도 될까요? 예를 들어 "아니오"라는 답변이 허용된 목표 - C .mm 파일입니다.
사용.Bridging Header
충분히.h
를 포함하지 않는다.C++
진술들,Objective-C
포장지.h
포함하다C++
,.mm
실제 래핑을 하는 파일C++
클래스 및.swift
, 4개의 언어를 사용할 수 있습니다(포함할 경우 5개).Objective-C++
)를 구축하여 단일 실행 파일에 링크할 수 있습니까?
네.
믹스 가능Swift
,C
,C++
,Objective-C
&Objective-C++
파일을 같은 Xcode 프로젝트에 저장합니다.
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;
}
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
목표-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")
}
Bridging-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(헤더)
.h: C의 모든 맛, ++, Objective 또는 not에 모호하게 사용되고 있기 때문에 이 부분이 까다롭습니다..h에 클래스 등의 C++ 키워드가1개도 포함되어 있지 않은 경우는, 그 키워드를 ...에 추가할 수 있습니다.Bridging-Header.h는 대응하는 .c 또는 .cpp 기능을 표시합니다.그 이외의 경우 헤더는 순수 C API 또는 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
평.
네, 포장만 하면 됩니다.C++
안으로C
또는Objective-C
에 사용하다Swift
.
확실히, 나는 그것을 하는 프로젝트가 있다. C++
추상적인 크로스 플랫폼 모델의 추력을 위해C
아래 부분Objective-C
포장하다C++
을 위한 수업Swift
목적들,Swift
그 모든 것을 아류 분류에 묶다NSDocument
, 를 확인하는 커스텀뷰가 있습니다.C
물건.
추가했습니다.extern "C"
당신의 훌륭한 제안대로 포장해 주세요.C 메서드를 호출하려면void hello_c(const char * name)
C++ 메서드에서hello_cpp(const std::string& name)
,더하다#include "C.h"
및 콜hello_c(name.c_str());
.
새로운 SO-32541268: 파라미터 포함!
§ GitHub에서 이 솔루션을 찾고 Swift Recipes에 대한 자세한 내용은 참조하십시오.
언급URL : https://stackoverflow.com/questions/32541268/can-i-have-swift-objective-c-c-and-c-files-in-the-same-xcode-project
'programing' 카테고리의 다른 글
Firebase Vuex에서 항목 제거 (0) | 2022.06.02 |
---|---|
Android JNI 프로그램에서 호출하는 Log API란 무엇입니까? (0) | 2022.06.01 |
요소에 ID가 없을 때 vuej에서 v-for를 처리하는 방법 (0) | 2022.06.01 |
C/C++ 포함 헤더 파일 순서 (0) | 2022.06.01 |
표현식과 상수는 메모리에 저장되지 않는 경우 어디에 저장됩니까? (0) | 2022.06.01 |