cedarbdd / cedar

BDD-style testing using Objective-C
http://groups.google.com/group/cedar-discuss
1.19k stars 140 forks source link

Cedar, Swift and Protocol Declarations - Compiler error #397

Closed mkanthan closed 7 years ago

mkanthan commented 7 years ago

I'm running into a compilation error when introducing a WKNavigationDelegate into my view controller.

My project config: I'm using Cedar in the "stand-alone" setup, with a separate Specs target. I have not been able to get Swift specs working properly (although this is probably a different issue). However, I can write Objective-C specs for Swift modules. So for now, my Spec files are all in Objective-C, and my main target is a mix of Swift and Objective-C which compiles and runs fine (with the correct bridging headers). I should add that my project is an older project started with Cedar/Objective-C from a while ago, so I'm not sure if there's other config that I'm lacking. I'm running Cedar 0.13.1.

Everything compiles and runs fine when my code is the following:

MyViewController.swift:

import Foundation

class  MyViewController: UIViewController {
    var webView: WKWebView!

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.displayWKViewContent()
    }

    func displayWKViewContent() -> Void {
        let baseURLForRequest = kAccountsURL
        let url = NSURL(string: baseURLForRequest)
        let request = NSURLRequest(URL: url!)
        webView = WKWebView(frame: self.view.frame)
        webView.loadRequest(request)
        webView.frame = CGRect(origin: CGPoint(x: 43, y: 44), size: CGSize(width: 938, height: 680))
        self.view.addSubview(webView)
        self.view.sendSubviewToBack(webView)
    }
}

MyViewControllerSpec.mm:

#import <My_App-Swift.h>
#import <WebKit/WebKit.h>

using namespace Cedar::Matchers;
using namespace Cedar::Doubles;

SPEC_BEGIN(MyViewController)

fdescribe(@"MyViewController", ^{
    __block MyViewController * wkController = nil;
    __block WKWebView * webView = nil;

    beforeEach(^{
        wkController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
        [wkController view];
        webView = wkController.webView;
    });

    it(@"should have a WKWebView assigned to the webView", ^{
        expect(webView).to(be_instance_of([WKWebView class]));
    });    
});

SPEC_END

Things start to go awry when I introduce the WKNavigationDelegate:

class  MyViewController: UIViewController, WKNavigationDelegate {
    ...
}

Running my Specs target, I get a compiler error inside my auto-generated Swift header (My_App-Swift.h):

SWIFT_CLASS("_TtC24My_App25MyViewController")
@interface MyViewController : UIViewController <WKNavigationDelegate>
...
@end

On this line, I get the compiler error Cannot find protocol declaration for 'WKNavigationDelegate'; did you mean 'UINavigationBarDelegate'?. Obviously, I didn't mean UINavigationBarDelegate. But I'm not sure what I'm missing - I've done the necessary imports of UIKit, and WebKit in the Spec file, as well as the bridging header.

FYI, my main target compiles and runs fine, and I'm able to set up the delegate, etc. I'm stumped here.

akitchen commented 7 years ago

How is your WKNavigationDelegate declared? And if it is in an ObjC header, is it in a header search path which is visible within this project target?

On Mon, Sep 12, 2016 at 9:56 AM, Manu Kanthan notifications@github.com wrote:

I'm running into a compilation error when introducing a WKNavigationDelegate into my view controller.

My project config: I'm using Cedar in the "stand-alone" setup, with a separate Specs target. I have not been able to get Swift specs working properly (although this is probably a different issue). However, I can write Objective-C specs for Swift modules. So for now, my Spec files are all in Objective-C, and my main target is a mix of Swift and Objective-C which compiles and runs fine (with the correct bridging headers). I should add that my project is an older project started with Cedar/Objective-C from a while ago, so I'm not sure if there's other config that I'm lacking. I'm running Cedar 0.13.1.

Everything compiles and runs fine when my code is the following:

MyViewController.swift:

import Foundation

class MyViewController: UIViewController { var webView: WKWebView!

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.displayWKViewContent()
}

func displayWKViewContent() -> Void {
    let baseURLForRequest = kAccountsURL
    let url = NSURL(string: baseURLForRequest)
    let request = NSURLRequest(URL: url!)
    webView = WKWebView(frame: self.view.frame)
    webView.loadRequest(request)
    webView.frame = CGRect(origin: CGPoint(x: 43, y: 44), size: CGSize(width: 938, height: 680))
    self.view.addSubview(webView)
    self.view.sendSubviewToBack(webView)
}

}

MyViewControllerSpec.mm:

import

import <WebKit/WebKit.h>

using namespace Cedar::Matchers; using namespace Cedar::Doubles;

SPEC_BEGIN(MyViewController)

fdescribe(@"MyViewController", ^{ block MyViewController * wkController = nil; block WKWebView * webView = nil;

beforeEach(^{
    wkController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    [wkController view];
    webView = wkController.webView;
});

it(@"should have a WKWebView assigned to the webView", ^{
    expect(webView).to(be_instance_of([WKWebView class]));
});

});

SPEC_END

Things start to go awry when I introduce the WKNavigationDelegate:

class MyViewController: UIViewController, WKNavigationDelegate { ... }

Running my Specs target, I get a compiler error inside my auto-generated Swift header (My_App-Swift.h):

SWIFT_CLASS("_TtC24My_App25MyViewController") @interface MyViewController : UIViewController ... @end

On this line, I get the compiler error Cannot find protocol declaration for 'WKNavigationDelegate'; did you mean 'UINavigationBarDelegate'?. Obviously, I didn't mean UINavigationBarDelegate. But I'm not sure what I'm missing - I've done the necessary imports of UIKit, and WebKit in the Spec file, as well as the bridging header.

FYI, my main target compiles and runs fine, and I'm able to set up the delegate, etc. I'm stumped here.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pivotal/cedar/issues/397, or mute the thread https://github.com/notifications/unsubscribe-auth/AAXHGNsM-RUPeom7zIBvKeoQ7iJaExzvks5qpYQkgaJpZM4J6zKS .

mkanthan commented 7 years ago

I've declared it on MyViewController - MyViewController.swift (same file I posted above). It is not in an Objective-C header anywhere.

So, declaring it and running my main app target with the following compiles and runs fine (displays the log message some time after I execute loadRequest on the WKWebView):

import Foundation

class  MyViewController: UIViewController, WKNavigationDelegate {
  var webView: WKWebView!

  ...

  override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(animated)
      self.displayWKViewContent()
  }

  func displayWKViewContent() -> Void {
      ...
      webView.navigationDelegate = self
      ...
  }

  func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
      NSLog("WKWebView started to load.")
  }
}

However, with this same code, my Specs target does not even compile and gives me an error in my autogenerated -Swift.h file. If I were to remove the WKNavigationDelegate and the delegate method, the Specs target compiles and runs.

In case you want to see my bridging header (Specs-Bridging-Header.h), it's here:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "NSString+URLQueryParsing.h"
#import "WebViewStrings.h"
#import "UIKit/UIKit.h"
#import "WebKit/WebKit.h"

Importing WebKit.h here should be enough, correct?

Edit: Forgot to add that I set the delegate in displayWKViewContent().

mkanthan commented 7 years ago

@akitchen I fixed this issue - it seems I was missing an import in Specs-Prefix.pch. I added this line to Specs-Prefix.pch:

#import <WebKit/WebKit.h>

I think I can consider this issue closed. However, it's a bit of a mystery to me why it wasn't until I introduced the delegate that it stopped compiling. It was fine with WKWebView which is another WebKit framework class.

akitchen commented 7 years ago

Thank you for the update! Glad to hear you got things resolved.

I wonder if this had anything to do with the ancestry of your protocol declaration. We have definitely seen weird quirks around protocol loading/discovery in the past.

On Sep 13, 2016, at 12:34, Manu Kanthan notifications@github.com wrote:

@akitchen I fixed this issue - it seems I was missing an import in Specs-Prefix.pch. I added this line to Specs-Prefix.pch:

import <WebKit/WebKit.h>

I think I can consider this issue closed. However, it's a bit of a mystery to me why it wasn't until I introduced the delegate that it stopped compiling. It was fine with WKWebView which is another WebKit framework class.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

PoppyMan commented 7 years ago

I and you the same, I finally know how to solve, thank you very much!!!!!!

Gamshan commented 6 years ago

I am also struggle in same thing @mkanthan answer help me. thankyou

sashaweiss commented 5 years ago

I'm having a similar issue (not with Cedar specifically, but re: protocols not being found). Has anyone found anything more about the underlying issue, e.g. why importing to the prefix header helped?

Specifically, I'm trying to use a Swift protocol (declared in a Development Pod) on a Swift class (in my main project, which is mixed ObjC and Swift). I get the same issue as above, where in the <ProjectName>-Swift.h file the protocol declaration cannot be found. Swift classes I use from that Pod seem to be fine - at least, the <ProjectName>-Swift.h header forward declares them so it compiles ok.

Thanks in advance if anyone has pointers!