3

I'm trying to use a Swift in an Objective-C class, but having trouble with the syntax or even if it's possible. Below is what I'm trying.

Swift:

@objc protocol MyProtocol {

    var someManager: MyManager? { get set }
    var someState: MyState { get set }

}

extension MyProtocol {

    func didLoad(delegate: MyProtocol) {

    }

}

class ViewController: MyProtocol {

  var someManager: MyManager?
  var someState: MyState = .Unknown

  override func viewDidLoad() {
    super.viewDidLoad()
    didLoad(self as MyProtocol)
  }

}

Objective-C:

@interface ViewController : UIViewController<MyProtocol>

@end

@interface ViewController()

  @property (nonatomic, assign) MyManager *someManager; //?? Error: Illegal redeclaration of property in class extension ‘ViewController’ (attribute must be ‘readwrite’, while its primary must be ‘readonly’)
  @property (nonatomic, assign) MyState someState; //??

@end

@implementation HomeViewController

-(void)viewDidLoad {
  [super viewDidLoad];
  [self didLoad:(self as MyProtocol)] //??
}

How can I get the Objective-C ViewController to work? I have an example of how it should work in Swift. Thanks for any help.

TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • It looks like you're mixing Obj-c and Swift in the same view controller. What is `didLoad(self as MyProtocol)`? – fsb Apr 04 '16 at 20:28
  • It was my attempt for the Obj-C version. I updated the question with another attempt. I'm trying to call didLoad from the protocol, and pass itself in as the delegate but casted as the protocol. The pure Swift version above it works but was trying to get the Obj-C version to work like it. – TruMan1 Apr 05 '16 at 02:50
  • Simple question, but do you have your bridging header setup correctly? – fsb Apr 05 '16 at 11:45
  • Indeed, it's an inherited Obj-C project :( with sprinkled Swift in some places. – TruMan1 Apr 05 '16 at 11:46

0 Answers0