swift4.0解决setValuesForKeys无法赋值的问题

swift 4.0 以前字典转model我们使用 setValuesForKeysWithDictionary 代码如下

class Status: NSObject{
    //创建时间
    var created_at:String?

    //ID
    var id:Int = 0

    //信息内容
    var text:String?

    //来源
    var source:String?

    //配图
    var pic_urls:[[String:AnyObject]]?

   //字典转模型
    init(dict:[String:AnyObject]) {
        super.init()

        setValuesForKeysWithDictionary(dict)
    }

}

xcode升级到4.0以后发现系统方法改为了 setValuesForKeys,运行发现数据解析成功,但是转model的时候发现没有值

问题就是4.0后swift和oc区分的更严谨了导致定义的属性在这种情况下不能存储值, 解决办法如下

class Status: NSObject{
    //创建时间
@objc   var created_at:String?

    //ID
    @objc var id:Int = 0

    //信息内容
    @objc var text:String?

    //来源
    @objc var source:String?

    //配图
    @objc var pic_urls:[[String:AnyObject]]?

   //字典转模型
    init(dict:[String:AnyObject]) {
        super.init()

        setValuesForKeys(dict)
    }

    override func setValue(_ value: Any?, forKey key: String) {
        ///这句话很关键,一定要有
        super.setValue(value, forKey: key)   
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift 4.0中,系统没有提供直接的底部弹窗控件。但是你可以使用UIAlertController和UIAlertAction来创建一个自定义的底部弹窗。下面是一个示例代码: ```swift func showBottomAlert() { let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in // 处理点击 Action 1 的操作 } alertController.addAction(action1) let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in // 处理点击 Action 2 的操作 } alertController.addAction(action2) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in // 处理点击取消按钮的操作 } alertController.addAction(cancelAction) if let popoverPresentationController = alertController.popoverPresentationController { popoverPresentationController.sourceView = self.view popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0) popoverPresentationController.permittedArrowDirections = [] } present(alertController, animated: true, completion: nil) } ``` 在上面的代码中,我们创建了一个UIAlertController,并添加了两个UIAlertAction作为选项,以及一个UIAlertAction作为取消按钮。然后,我们通过present方法将底部弹窗显示在屏幕上。 注意,为了将弹窗显示在底部,我们设置了popoverPresentationController的sourceView为当前的view,并且设置了sourceRect的位置在屏幕底部中间。这样就能够实现底部弹窗的效果。 希望这可以帮助你实现底部弹窗!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值