Arrays Swift-数组。第一个(在哪里

Arrays Swift-数组。第一个(在哪里,arrays,swift,xcode,Arrays,Swift,Xcode,我对userdataArray有一个问题。首先(其中….)。基本上,我的目标是根据userId提取userdata以填充单元格 变量userdata包含一个数组。但是,当我尝试使用userdata?.name配置单元格时,模拟器会给出一个错误 我做错了什么?是userdata数组还是嵌套数组 查看控制器中的代码: let userdata = userdataArray.first(where:{$0.userId == activity.userId}) print("Array filter

我对
userdataArray有一个问题。首先(其中….)
。基本上,我的目标是根据
userId
提取
userdata
以填充单元格

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

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

查看控制器中的代码:

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"
“(userdata?.name)!”
只是一个普通字符串,以左括号开头。这可能不是您想要的

“\(userdata?.name)!”
是一个使用字符串插值的字符串。它将计算
(userdata?.name)!
如果
userdata
nil
,则
userdata?.name
为nil
,并且
将使其崩溃,这是故意的

它建议使用
如果let…
guard let…
,如果找不到项目,则使用一些替代代码。

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

“\(userdata?.name)!”
是一个使用字符串插值的字符串。它将计算
(userdata?.name)!
如果
userdata
nil
,则
userdata?.name
为nil
,并且
将使其崩溃,这是故意的


建议使用
如果让…
保护让…
,如果找不到项目,则使用一些替代代码。

确保您有用户数据:

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

确保您有用户数据:

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

@肤浅的想法:谢谢你的帮助。我最终得到了下面的答案,这似乎很有效

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

}

@肤浅的想法:谢谢你的帮助。我最终得到了下面的答案,这似乎很有效

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

}

错误在哪里?我只是看到了你的
userdataArray
@CharlesSrstka的日志:你是对的,我在cell.configure.Xcode中使用userdata.name时得到了错误,然后得到这个错误线程1:EXC\u BAD\u指令(code=EXC\u I386\u INVOP,subcode=0x0)错误在哪里?我只是看到了你的
userdataArray
@CharlesSrstka的日志:你是对的,我在cell.configure.Xcode中使用userdata.name时得到了错误,然后得到这个错误线程1:EXC\u BAD\u指令(code=EXC\u I386\u INVOP,subcode=0x0)如果你的问题已经解决,那么接受Shallowthough作为公认的答案。我建议
guard
if let
image`也这样做,因为每个
都可能崩溃。@MandeepSingh如果是我,我建议接受gnasher729,因为它实际上解释了问题的原因,而不是仅仅发布一些code没有解释。@CharlesSrstka,一个问问题的用户,如果他理解浅层用户的答案,那么该用户将得到重点,而不是另一个。因为我不这么认为,他理解gnasher729的答案。@MandeepSingh我对gnasher729的答案有个问题,因为它只是代码,OP不必取消如果你的问题已经解决,那就接受浅色思维作为公认的答案。我建议
guard
如果让
image`也这样做,因为每个
都可能崩溃。@MandeepSingh如果是我,我建议接受gnasher729,因为它实际上解释了原因关于这个问题,而不是仅仅发布一些没有解释的代码。@CharlesSrstka,询问问题的用户,如果他理解浅薄用户的答案,那么该用户将得到重点,而不是另一个。因为我不这样认为,他理解gnasher729的答案。@MandeepSingh关于gnasher729的答案,我的问题是因为它只是代码,OP根本不需要理解它,只需复制并粘贴它即可。