试图在Swift中使用Objective-C编写的委托方法抛出“无法找到协议声明”

时间:2022-09-07 09:16:22

I have an existing Sample Objective-C app that uses Salesforce's SDK. I am trying to convert it to use Swift one-class at a time. Salesforce SDK has a class called 'SFRestRequest.h' that has 'SFRestDelegate' delegate.

我有一个使用Salesforce SDK的现有Sample Objective-C应用程序。我试图将它转换为一次使用Swift一类。 Salesforce SDK有一个名为“SFRestRequest.h”的类,它具有“SFRestDelegate”委托。

In Objective C, I have a class called 'RootViewController.h' that is a subclass of UITableViewController. It implements SFRestDelegate. It works fine.

在Objective C中,我有一个名为'RootViewController.h'的类,它是UITableViewController的子类。它实现了SFRestDelegate。它工作正常。

//RootViewController.h
#import <UIKit/UIKit.h>
#import "SFRestAPI.h"

@interface RootViewController : UITableViewController <SFRestDelegate> {

    NSMutableArray *dataRows;
    IBOutlet UITableView *tableView;    

}

I am trying to create a RootVC.swift file to replace RootViewController Objective-c class.

我正在尝试创建一个RootVC.swift文件来替换RootViewController Objective-c类。

I have a bridging header file that imports all those headers that are imported in objective-c

我有一个桥接头文件,导入所有在objective-c中导入的头文件

//SwiftForce-Bridging-Header.h 
#import "SFRestAPI.h"
#import "SFRestRequest.h"

My RooVC.Swift file looks like:

我的RooVC.Swift文件如下所示:

import UIKit

class RootVC: UITableViewController,SFRestDelegate {
 ..
..
}

Now, if I command+click on the SFRestDelegate, it correctly goes to protocol implementation. However, if I try to build, I get.. "Cannot find Protocol declaration SFRestDelegate Error!

现在,如果我命令+单击SFRestDelegate,它将正确进入协议实现。但是,如果我尝试构建,我得到..“找不到协议声明SFRestDelegate错误!

SWIFT_CLASS("_TtC10SwiftForce6RootVC")
@interface RootVC : UITableViewController <SFRestDelegate>
@property (nonatomic) NSArray * dataRows;
- (instancetype)initWithStyle:(UITableViewStyle)style OBJC_DESIGNATED_INITIALIZER;
- (void)viewDidLoad;
- (void)didReceiveMemoryWarning;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end

Appreciate any help. You can test it by downloading the app from: https://github.com/rajaraodv/SwiftForce

感谢任何帮助。您可以从以下网址下载应用程序来测试它:https://github.com/rajaraodv/SwiftForce

1 个解决方案

#1


5  

I just sent you a pull request to the repo you linked which solves the problem.

我刚给你发送拉动请求给你链接的repo解决了问题。

Your problem appears to be an issue with the load order of the Swift and Objective-C classes and headers. You have to make sure that any headers required by your Swift classes are compiled before the Swift code is compiled. This is handled fairly seamlessly with simple .h and .m Objective-C files, but when using static libraries (as in your project) the .pch file seems to be the most appropriate place to import any headers that your Swift code needs at compile time.

您的问题似乎是Swift和Objective-C类和标头的加载顺序的问题。您必须确保在编译Swift代码之前编译Swift类所需的任何头文件。这与使用简单的.h和.m Objective-C文件相当无缝地处理,但是当使用静态库时(如在项目中),.pch文件似乎是导入Swift代码在编译时需要的任何头文件的最合适的位置。时间。

Adding this line to your SwiftForce-Prefix.pch file seems to work:

将此行添加到SwiftForce-Prefix.pch文件似乎有效:

#import <SalesforceNativeSDK/SFRestRequest.h>

This doesn't seem very elegant (just like the Bridging-Header solution) however only time will tell if this is a bug or a best practice.

这看起来并不优雅(就像Bridging-Header解决方案一样)但是只有时间才能证明这是一个错误还是最佳实践。

#1


5  

I just sent you a pull request to the repo you linked which solves the problem.

我刚给你发送拉动请求给你链接的repo解决了问题。

Your problem appears to be an issue with the load order of the Swift and Objective-C classes and headers. You have to make sure that any headers required by your Swift classes are compiled before the Swift code is compiled. This is handled fairly seamlessly with simple .h and .m Objective-C files, but when using static libraries (as in your project) the .pch file seems to be the most appropriate place to import any headers that your Swift code needs at compile time.

您的问题似乎是Swift和Objective-C类和标头的加载顺序的问题。您必须确保在编译Swift代码之前编译Swift类所需的任何头文件。这与使用简单的.h和.m Objective-C文件相当无缝地处理,但是当使用静态库时(如在项目中),.pch文件似乎是导入Swift代码在编译时需要的任何头文件的最合适的位置。时间。

Adding this line to your SwiftForce-Prefix.pch file seems to work:

将此行添加到SwiftForce-Prefix.pch文件似乎有效:

#import <SalesforceNativeSDK/SFRestRequest.h>

This doesn't seem very elegant (just like the Bridging-Header solution) however only time will tell if this is a bug or a best practice.

这看起来并不优雅(就像Bridging-Header解决方案一样)但是只有时间才能证明这是一个错误还是最佳实践。