パラメーターを初期化することはできませんが、その理由がわかりません
質問
このエラーが発生します:
Cannot initialize a parameter of type 'id<ZXingDelegate>'
with an lvalue of type 'FirstViewController *const __strong'
このコードの行から:
ZXingWidgetController *widController =
[[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES
OneDMode:NO];
どうすればこれを解決できますか?
解決
MacMadeのコメントのおかげで、私は問題を解決することができました。私はそれをこのように書くべきだった:
ZXingWidgetController *widController =
[[ZXingWidgetController alloc] initWithDelegate:***(id)** self showCancel:YES
OneDMode:NO];
ここで、(id)は彼が話していた橋渡しされたキャストです。
他のヒント
この問題には、この行のコードを使用してください
ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:(id<ZXingDelegate>)self showCancel:YES OneDMode:NO];
これを正しく理解している場合、問題はBridged Castが必要ではないということではなく、FirstViewControllerクラスはZxingDelegateインターフェイスクラスを定義していないため、問題です。
zxingdelegateは(名前に基づいて)インターフェイスクラス(プロトコル、またはデリゲート)で、それを継承するクラスで定義する必要がある関数(インターフェイス)を宣言します(@optionalでない限り)。 C ++の純粋な(抽象的)クラスのようなもの。
したがって、ヘッダーファイルには次のようなものが必要です。
@interface FirstViewController : UIViewController <ZXingDelegate>
そして、あなたの.mファイルで、このようなもの:
@implementation FirstViewController
//......
-(void) SomeFunctionThat_ZXingDelegate_declares
{
// .... do something here....
}
//......
@end
所属していません StackOverflow