加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 将’NSNumber’桥接到’Int’警告

发布时间:2020-12-14 17:47:45 所属栏目:百科 来源:网络整理
导读:这是警告我应该关注的吗? 如果是这样,什么是解决方案? 这是我的功能: override func prepare(for segue: UIStoryboardSegue,sender: Any?) { if let destination = segue.destination as? ProfileViewController{ let cell = sender as! UITableViewCell
这是警告我应该关注的吗?

如果是这样,什么是解决方案?
这是我的功能:

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    if let destination = segue.destination as? ProfileViewController{
    let cell = sender as! UITableViewCell
        let selectedRow = myTableView.indexPath(for: cell)!.row


        switch (mySegmentedControl.selectedSegmentIndex){
        case 0:
            destination.nameVar = userSFList[selectedRow].name!
            destination.imageOneURL = userSFList[selectedRow].image!
            destination.bioVar = userSFList[selectedRow].bio!

            if let image2 = userSFList[selectedRow].imageTwo  {
               destination.imageTwoUrl = image2                }

            if let contactInt = userSFList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }

            break

        case 1:

            destination.nameVar = userEBList[selectedRow].name!
            destination.imageOneURL = userEBList[selectedRow].image!
             destination.imageTwoUrl = userEBList[selectedRow].imageTwo!



            if let contactInt = userEBList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }


            break
        case 2:
            destination.nameVar = userSFOList[selectedRow].name!
            destination.imageOneURL = userSFOList[selectedRow].image!

            if let contactInt = userSFOList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }

            break
        case 3:
            destination.nameVar = userSJList[selectedRow].name!
            destination.imageOneURL = userSJList[selectedRow].image!
                     if let contactInt = userSJList[selectedRow].contact as? Int {
                destination.contact = contactInt
            }
            break
        default:
            break

    }
}

}

我正在使用具有四个不同段的分段控件并使用firebase提取数据.

解决方法

我的个人规则始终是零警告.
比抱歉更安全.

联系人是否可选?如果是这样…

你可以使用Optional Binding:

if let contactInt = userSFOList[selectRow].contact as? Int {
  destination.contact = contactInt
}

或者Nil-Coalescing Operator:

destination.contact = userSFOList[selectedRow].contact.intValue ?? <Your default Int here>

您也可以使用@ Kamil.S指出的guard,如:

guard let nameVar = userSFOList[selectedRow].name,let imageVar = userSFOList[selectedRow].image,let contactVar = contact as? Int else {
    // Conditions were failed. `return` or `throw`.
  }

destination.nameVar = nameVar
destination.imageOneURL = imageVar
destination.contact = contactVar

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读