LoginSignup
2
2

More than 5 years have passed since last update.

メソッド名が標準に従っていなかっただけで、SwiftとObjcのブリッジングエラーでハマった話

Last updated at Posted at 2016-07-26

問題

Objective-C で定義した protocol を、Swiftで書いた class で継承してみたらdoes not conform to protocol...エラーが出てしまった。

required method が実装されていないときのエラーなのだが、XCodeの補完に従って全てを定義したので、何が問題なのかさっぱりわからなかった。

問題の protocol

@protocol HogeDelegate <NSObject>
- (void) hogeAction:(Hoge*)hoge Error:(NSError*)error;
@end

問題の implementation

extension FugaViewController: HogeDelegate {
    func hogeAction(hoge: Hoge!, error: NSError!) {
        // nothing is implemented
    }
}

原因・解決策

こう整理すると「おや」と気づくかもしれないが・・・Objective-Cで定義した protocol の命名が一般的な規則からはずれておりErrorの先頭が大文字になっているのが原因。

XCodeが変換したメソッドはerrorが小文字になっているから、名前解決ができなかった模様。
標準的な下記の様な命名に直したらすんなりうまくいった。

@protocol HogeDelegate <NSObject>
- (void) hogeActionFailed:(Hoge*)hoge withError:(NSError*)error;
@end

結論

命名、大事

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2