【问题标题】:Swift - Array.first(where斯威夫特 - Array.first(在哪里
【发布时间】:2017-10-14 12:55:11
【问题描述】:

我遇到了userdataArray.first(where....) 的问题。基本上,我的目标是根据userId 提取userdata 的消息以填充单元格。

变量userdata 包含一个数组。但是,当我尝试使用 userdata?.name 配置单元时,模拟器会出错。

我做错了什么? userdata 不是数组,还是嵌套数组?

viewController中的代码:

let userdata = userdataArray.first(where:{$0.userId == activity.userId})
print("Array filter test")
dump(userdata)

let image = UIImage(named: "profile_image")

cell.configureCell(profileImage: image!, profileName: "(userdata?.name)!", activityDate: "8 October", name: activity.name, distance: "8 km", sightings: activity.sightings, kills: activity.kills)
return cell

Xcode 的输出和错误:

Array filter test
▿ Optional(Shoota_MapKit.Userdata)
  ▿ some: Shoota_MapKit.Userdata #0
    - userId: "NhZZGwJQCGe2OGaNTwGvpPuQKNA2"
    - name: "Christian"
    - city: "Oslo"
    - country: "Norway"
    - profileImage: "profile_image"

【问题讨论】:

  • 哪里出错了?我刚刚看到你userdataArray的日志。
  • @CharlesSrstka:你是对的,当我在 cell.configure 中使用 userdata?.name 时出现错误。 Xcode 然后得到这个错误 Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

标签: arrays swift xcode


【解决方案1】:

"(userdata?.name)!" 只是一个普通字符串,以左括号开头。这可能不是你想要的。

"\(userdata?.name)!" 是一个使用字符串插值的字符串。它将评估(userdata?.name)! 如果userdatanil,那么userdata?.namenil! 将使它崩溃,这是故意的。

建议使用if let ...guard let ...,如果找不到该项目,请使用一些替代代码。

【讨论】:

    【解决方案2】:

    确保您有用户数据:

    guard let userdata = userdataArray.first(where:{$0.userId == activity.userId}),
          let image = UIImage(named: "profile_image") else {
               //no userdata
               return cell
    }
    cell.configureCell(profileImage: image, profileName: userdata.name, activityDate: "8 October", name: activity.name, distance: "8 km", sightings: activity.sightings, kills: activity.kills)
    return cell
    

    【讨论】:

      【解决方案3】:

      @shallowThought:感谢您的帮助。我结束了以下,它似乎工作。

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          guard let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as? feedCell else {
      
            print("dequeueResuableCell return an else"); return UITableViewCell()
          }
      
          let activityDict = activityArray[indexPath.row]
      
          guard let userdata = userdataArray.first(where:{$0.userId == activityDict}) else {return cell}
      
           let image = UIImage(named: userdata.profileImage)
      
          cell.configureCell(profileImage: image!, profileName: userdata.name, activityDate: "8 October", name: activityDict.name, distance: "8 km", sightings: activityDict.sightings, kills: activityDict.kills)
          return cell
      
      }
      

      【讨论】:

      • 如果您的问题已经解决,则接受 shallowThought 作为已接受的答案。
      • 我也推荐guardif let image`,因为每个! 都可能崩溃。
      • @MandeepSingh 如果是我,我建议接受 gnasher729,因为它实际上解释了问题的原因,而不是仅仅发布一些没有解释的代码。
      • @CharlesSrstka,提问的用户如果理解浅层用户的答案,那么该用户将得到重点,而不是其他用户。因为我不这么认为,他理解 gnasher729 的答案。
      • @MandeepSingh 我对 gnasher729 的回答的问题是,因为它只是代码,所以 OP 根本不需要理解它,而可以复制并粘贴进去。
      猜你喜欢
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 2016-02-08
      • 2017-11-13
      • 2015-08-19
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多