2017-08-10 14 views
0

私は従来のSwift 2.2プロジェクトに取り組んでいます。よく知られたプロトコル指向のプラクティスを自分のコードに実装したいと思います。UIViewControllerの自己制約プロトコルの拡張

protocol SuccessPresenting { 
    func presentSucess(title: String, message: String) 
} 

extension SuccessPresenting where Self: UIViewController { 
    func presentSucess(title: String?, message: String) { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     let dismissAction = UIAlertAction(title: "ОК", style: .Default, handler: nil) 
     alertController.addAction(dismissAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
} 

class NewViewController: UIViewController, SuccessPresenting { 

    func foo() { 
     presentSucess(nil, message: "Done!") 
    } 
} 

けれども、それはスウィフト3.1上の作品で、ここで私はエラーを取得する:The NewViewController doesn't conform to protocol SuccessPresenting

しかし、私はすでにプロトコル拡張を使用していることを行っているとして、なぜ私は、私のVCでのプロトコルの実装を記述する必要がありますか? 何か助けていただければ幸いです。 これはSwiftです注意してください2.2

+1

あなたの 'NewViewController'から' SuccessPresenting'適合性制約を削除してみてください。つまり、 'class NewViewController:UIViewController {// presentSuccessを呼び出すコード}' –

+0

@ NandiinBaoそれも私を助けなかった –

答えて

1

これは直接ペーストですか? extensionには通常の文字列の代わりにオプションの文字列が含まれていますが、プロトコルには通常のStringが含まれています。これにより、コンパイラが別の方法であるとみなされ、この特定のケースではプロトコルのoptionallnessが無効になることがあります。

+0

ああ、神StackOverFlowが存在します。もちろん、あなたは正しい、私は慎重だった) –

関連する問題