Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use a delegate method written in Objective-C in Swift is throwing "Cannot find protocol Declaration"

Tags:

swift

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.

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

//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.

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

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

My RooVC.Swift file looks like:

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!

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

like image 395
Raja Rao Avatar asked Jun 12 '14 04:06

Raja Rao


1 Answers

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

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.

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

#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.

like image 134
johnnyclem Avatar answered Nov 11 '22 21:11

johnnyclem