mercredi 20 juillet 2016

Can't cast WKWebView to my custom subclass of the WKWebView in Swift

I created a custom subclass for my WKWebView in my project called MyWebView, but when I try to cast a WKWebView into a MyWebView, app crashes. What might be the issue?

import UIKit
import WebKit
import SwiftyJSON

class MyWebView: WKWebView {

    var requestUrl: String?
    var row: Int?
    var data: JSON?

}

This is where I make the cast:

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    if message.name == "callbackHandler"{

        let mywebview = message.webView as! MyWebView


    }
}

And this is the error:

Could not cast value of type 'WKWebView' (0x1a0717408) to 'MyProjectName.MyWebView' (0x10026b950).

Aucun commentaire:

Enregistrer un commentaire