Objective-C 프로젝트에서 Swift Pod Framework를 가져오고 사용하는 방법
CocoaPods의 새로운 프레임 워크 설정을 확인하여 Pod를 가져 오려고했는데 Objective-C 프로젝트에서 Swift를 사용하는 데 문제가 있습니다.
먼저, CocoaPods 프리 릴리즈 0.35 입니다. 여기 에서 사용 및 설치 방법에 대해 읽을 수 있습니다 .
내 현재 Podfile은 다음과 같습니다.
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'MBProgressHUD'
pod 'SLPagingViewSwift'
MBProgressHUD는 일반적인 회전 표시기이며 SLPagingViewSwift는 cocoapods 검색에 Swift를 입력하여 찾은 임의의 프로젝트입니다. ViewController.m
내 프로젝트 는 다음과 같습니다 .
#import "ViewController.h"
@import SLPagingViewSwift;
@import MBProgressHUD;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Works just fine
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:hud];
[hud show:YES];
// Causes Error -- Won't build
SLPagingViewSwift *sl = [[SLPagingViewSwift alloc] init];
}
@end
다음은 SLPagingViewSwift
선언입니다.
class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
보시다시피에서 상속 받으 UIViewController
므로 할당하고 초기화하는 것이 문제가되지 않습니다. 파일을 파일로 별도로 추가하면 위의 코드가 제대로 실행됩니다. 나는 그것이 작동한다는 것을 안다.
tl; dr
순수한 Objective-C 클래스에서 CocoaPods로 만든 순수한 Swift Framework를 어떻게 사용할 수 있습니까?
문제 해결
주로 다양한 수입품을 시도해 왔습니다. Apple은 여기 에 @import
스타일을 권장합니다 .
그러나 나는 여러 다른 품종을 시도해 왔습니다.
// Compiler Error
#import <SLPagingViewSwift/SLPagingViewSwift.h>
// Builds Fine -- Doesn't Work
#import <SLPagingViewSwift/SLPagingViewSwift-Swift.h>
#import "SLPagingViewSwift-Swift.h"
나는 또한 클릭을 할 수 있는지 확인하기 위해 때때로 다른 Swift 라이브러리를 시도해 왔습니다.
나는 이것을 도울 수있는 Cocoapods 이슈에 대해 아무것도 보지 못했고, 그들의 블로그 / 릴리스 물건에서도 아무것도 찾지 못했습니다.
노트
SLPagingViewSwift.swift
구식 방식으로 프로젝트에 파일을 별도로 추가하면 정상적으로 작동합니다.
I think you have to declare the swift class as public, otherwise it is treated as an internal class and can be only be seen within the same module, and this could be the reason why adding it to the same project as files work, but as a framework doesn't. Other thing that occurs to me is that the framework may need to add @objc in front of the class declaration so that it can be seen within objective-c classes. Also reading Apple's guide of Mix and Match between objective c and swift it says that when you import an external framework, you need to make sure the Defines Module build setting for the framework you’re importing is set to Yes. Have you checked with any of those options?
'Development Tip' 카테고리의 다른 글
벡터를 이동하면 반복기가 무효화됩니까? (0) | 2020.11.15 |
---|---|
C ++ 최적화 프로그램이 clock ()에 대한 호출을 재정렬하는 것이 합법적입니까? (0) | 2020.11.15 |
C ++ 11에서 메모리를 정렬하는 권장 방법은 무엇입니까? (0) | 2020.11.15 |
스레드 안전성을위한 단위 테스트? (0) | 2020.11.15 |
C # (. NET)에서 SFTP 서버에 파일을 업로드하려면 어떻게해야합니까? (0) | 2020.11.15 |