1

I have this static function in a Swift class that I'd like to call in Objective-C:

static internal func photoActionSheet<T:UIViewController where T:UIImagePickerControllerDelegate, T:UINavigationControllerDelegate>(hostAndDelegateViewController:T)

I have other functions from the class that I'm able to call from ObjC, but it doesn't know of just this one

Thanks

3
  • 1
    May be because it's internal? Commented Nov 24, 2015 at 8:30
  • Nope...the other functions are internal as well and they're ok. Commented Nov 24, 2015 at 8:36
  • I'd appreciate it if you would remove that downvote. I'm trying to build up my rep :) Commented Nov 24, 2015 at 8:48

1 Answer 1

2

Functions with generic type constraints cannot be exported to Objective-C.

As a workaround, you could define the function like below and check for protocol conformance inside the function implementations:

static internal func photoActionSheet(hostAndDelegateViewController: UIViewController) {
  guard let vc = hostAndDelegateViewController as? protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate> else {
    fatalError("hostAndDelegateViewController must conform UINavigationControllerDelegate and UIImagePickerControllerDelegate")
  }

  // let picker = UIImagePickerController()
  // picker.delegate = vc
}

Likewise, Objective-C type UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate> * will be exported to Swift as UIViewController!

Sign up to request clarification or add additional context in comments.

2 Comments

Good to know. However, if I define the Swift function like what you did, I wouldn't be able to assign the UIViewController as a delegate of a UIImagePickerController (because it needs the two protocols) correct?
@theNotSoPro It can be done with a type cast trick. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.