美文网首页
Swift 遇到的坑

Swift 遇到的坑

作者: 天下林子 | 来源:发表于2018-08-30 11:46 被阅读77次

    在swift中字典转模型,创建的model 继承自 NSObject,如果是swift 3 ,则在模型中创建属性不需要前面加@objc, 而在swift 4 中如果model中不添加@objc,则会出现属性赋值不进去
    代码如下:

    
    class AnchorGroupModel : NSObject {
        
        /*
         {
         "error": 0,
         "data": [
         {
         "icon_url": "https://cs-op.douyucdn.cn/dycatr/game_cate/d3e0073bfb714186ab0c818744601963.jpg",
         "small_icon_url": "https://cs-op.douyucdn.cn/dycatr/game_cate/ffdc72ed97b50ad8011de9a779b8d83b.png",
         "tag_name": "英雄联盟",
         "tag_id": "1",
         "push_vertical_screen": "0",
         "push_nearby": "0",
         "room_list": []
         },
         {},
         {},
         ]
         }
         */
        
        //该组中对应的房间信息 -- 数组也要加上objc
       @objc var room_list : [[String : NSObject]]?
        //组显示的标题
       @objc var tag_name : String = ""
        //组显示的图标
       @objc var icon_url : String = ""
        
        init(dict : [String : Any ]) {
            super.init()
            
            setValuesForKeys(dict)
        }
        
        override func setValue(_ value: Any?, forUndefinedKey key: String) {
            
        }
    
    }
    
    

    获取数据赋值,在swift 4 中,下面的self.anchorGroup是有值的,但是AnchorGroupModel的属性不加@objc,也不会打印出tag_name的值

    extension RecommendViewModel {
        func requestData() {
            
            //请求推荐数据
            
            //请求颜值数据
            
            //请求游戏数据
            NetWorkTools.requestData(type: .GET, URLString: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: ["limit" : "4", "offset" : "0", "time" : NSDate.getCurrentTime() as NSString]) { (response) in
                
                print(response)
                
                guard let resultDic = response  as? [String : NSObject] else {
                    
                    return
                }
                
                guard let dataArray = resultDic["data"] as? [[String : Any]]  else {
                    return
                }
                
                for dict in dataArray {
                    
                    let group = AnchorGroupModel(dict: dict)
                    self.anchorGroup.append(group)
                    
                    let ss = self.anchorGroup.first
                    
                    print(ss?.tag_name ?? "rr")
                    
                     print("+++gggggg++\(self.anchorGroup)")
                }
                
                for group in self.anchorGroup {
                    
                    print("++++++" + group.tag_name)
                    print("++++++" + group.icon_url)
                    
                }
            }
        }
    }
    

    添加全局的pop手势

    实现方式:利用运行时机制,获取系统Pop手势target&action

    • 获取系统的手势监听view
    • 获取系统的手势target&action
    • 创建自己的手势,添加事件监听时, 使用上步中的target&action
    • 将手势,添加到系统手势监听的view中

    --------上代码-----------

    利用运行时机制查看所有的属性名称

    var count : UInt32 = 0
            let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)!
            for i in 0..<count {
                let ivar = ivars[Int(i)]
                //name 是一个指针
                let name = ivar_getName(ivar)
                //指针转成字符串
                print("++++++++++++\(String(cString: name!))")
            }
    
    

    打印出来的结果如下,我们使用到的就是_targets

    /*
             ++++++++++++_gestureFlags
             ++++++++++++_targets
             ++++++++++++_delayedTouches
             ++++++++++++_delayedPresses
             ++++++++++++_view
             ++++++++++++_lastTouchTimestamp
             ++++++++++++_firstEventTimestamp
             ++++++++++++_state
             ++++++++++++_allowedTouchTypes
             ++++++++++++_initialTouchType
             ++++++++++++_internalActiveTouches
             ++++++++++++_forceClassifier
             ++++++++++++_requiredPreviewForceState
             ++++++++++++_touchForceObservable
             ++++++++++++_touchForceObservableAndClassifierObservation
             ++++++++++++_forceTargets
             ++++++++++++_forcePressCount
             ++++++++++++_beganObservable
             ++++++++++++_failureRequirements
             ++++++++++++_failureDependents
             ++++++++++++_activeEvents
             ++++++++++++_keepTouchesOnContinuation
             ++++++++++++_delegate
             ++++++++++++_allowedPressTypes
             ++++++++++++_name
             ++++++++++++_gestureEnvironment
             */
    

    添加自己手势步骤如下:

    //获取系统的pop手势
            guard let  systemGes = interactivePopGestureRecognizer else {
                return
            }
            
            //获取手势添加到的view 中
            guard let gesView = systemGes.view else {
                return
            }
    
     let targets = systemGes.value(forKey: "_targets") as? [NSObject]
    
     guard let targetObject = targets?.first else {
                return
            }
            
            //取出target
            guard let target = targetObject.value(forKey: "target") else {
                return
            }
           
            
            //取出action
            let action = Selector(("handleNavigationTransition:"))
    
            //创建自己pan手势
            let panGes  = UIPanGestureRecognizer()
            gesView.addGestureRecognizer(panGes)
            panGes.addTarget(target, action: action)
    
    

    *********然后完美实现**********

    相关文章

      网友评论

          本文标题:Swift 遇到的坑

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