美文网首页
Swift KVO 的使用, 取值问题

Swift KVO 的使用, 取值问题

作者: 会钓鱼的丶猫 | 来源:发表于2020-04-13 12:52 被阅读0次

本次使用的目的是监听 UIWebView 的变化, 但是监听值不能直接取出, 需要as 转换
1.Swift 的change 不能直接取出
2.eroor 取值

// 添加KVO 监听
self.wkWebView.addObserver(self, forKeyPath: "title", options: NSKeyValueObservingOptions.new, context: nil)
// MARK: - KVO  监听  取值: as 转换类型后, 和 OC 一样的操作
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        let changeDic = change! as NSDictionary
        let title:String = changeDic.value(forKey: "new")! as! String
        
        print("当前的 change", changeDic.value(forKey: "new")!)
        
        let js = "callJsAlert()"
    
        self.wkWebView.evaluateJavaScript(js) { (re, error) in
            
            let er = error! as NSError
            let url = er.userInfo["WKJavaScriptExceptionSourceURL"]
            
            print("当前的 URL", url!)
            
            if title.contains("监听的属性变化值") {
                // 出发事件
        
            }

        }
    
    
       }

相关文章

网友评论

      本文标题:Swift KVO 的使用, 取值问题

      本文链接:https://www.haomeiwen.com/subject/zisgmhtx.html