【问题标题】:Bridging 'NSNumber' to 'Int' warning将“NSNumber”桥接到“Int”警告
【发布时间】:2017-04-08 01:45:42
【问题描述】:

这个警告有什么我应该关注的吗?

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

 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 提取数据。

【问题讨论】:

  • 尝试做类似的事情:destination.contact = userEBList[selectedRow].contact.intValue;除非它是可选的,否则您可能需要执行 destination.contact = userEBList[selectedRow].contact.intValue ?? 0 之类的操作(即默认为零)。
  • 为什么同一个代码重复了3次?
  • @Alexander 看起来像是三种不同的情况:userEBList、userSJList 和 userSFOList。
  • @backslash-f 所以应该有一个变量有条件地设置为这 3 个中的一个,然后重复的代码只对那个变量执行一次
  • @Alexander 我同意。

标签: ios swift xcode nsnumber bridging


【解决方案1】:

我的个人规则是始终 零警告
安全总比后悔好。

contactOptional 吗?如果是这样……

你可以使用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

【讨论】:

  • 这种类型的不匹配是否会成为优雅守卫的良好候选者,否则会失败?
  • @Kamil.S 我喜欢这样。我已根据您的建议更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 2020-02-07
相关资源
最近更新 更多