1

I am new to iOS development. I created a swift class as follows:

import WatchConnectivity;
import HealthKit;
@objc class Blah : NSObject, WCSessionDelegate { 
...
}

I need @objc so that I could use this class from objective-C (that already exists). The problem is that when the compiler creates bridge [productName]-Swift.h, it complains that it cannot find WCSessionDelegate. Exact error:

Cannot find protocol declaration for 'WCSessionDelegate'; did you mean 'NSURLSessionDelegate'?

SWIFT_CLASS("_TtC8test8Blah")
@interface  Blah: NSObject <WCSessionDelegate>

Instead of implementing that delegate, if I change it to the following, it works.

@objc class Blah : NSObject {
  ...
  func setSessionDelegate(delegate:WCSessionDelegate) -> Blah {
    self.mDelegate = delegate;
    return(self)
  }
}

I prefer the former way. How do I resolve this compilation error? Thanks

1 Answers1

1

It looks like the [productName]-Swift.h file only adds the include if mosules are supported:

#if defined(__has_feature) && __has_feature(modules)
@import ObjectiveC;
@import WatchConnectivity;
@import Foundation;
#endif

In my case, and probably in yours too, they are disabled. If you can't or won't enable them, you can just make sure to include the connectivity header yourself each time, e.g.

#import <WatchConnectivity/WatchConnectivity.h>
#import "MyApp-Swift.h"

See https://stackoverflow.com/a/24064015/67824

Community
  • 1
  • 1
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198