在 indexPath.row 的数组中快速打印元素

标签 swift xcode didselectrowatindexpath

想在数组的元素处打印字符串

var twoDimensionalArray = [
    ExpandableNames(isExpanded: false, names: ["Antiques",
        "Art",
        "Collectables",
        "Other Antiques , Art & Collectables"]),


    ExpandableNames(isExpanded: false, names: ["Baby Carriers",
        "Baby Clothing",
        "Baths",
        "Safety"]),
]


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    let mm = twoDimensionalArray[indexPath.row]

  if indexPath == [1,1]{
 print("this is mm:",mm)
 }

        }

//打印语句打印

mm 是:ExpandableNames(isExpanded: true, names: ["Baby Clothing","Baths","Safety"])

我只是想让它打印“Baby Carriers”

最佳答案

假设 ExpandableNames 是这样定义的

struct ExpandableNames {
    var isExpanded: Bool
    var names: [String]
} 

您可以像这样检索 indexPath.row 中的第一项

if let firstName = twoDimensionalArray[indexPath.row].names.first {
    //do something with firstName 
}

或者如果你想访问特定索引处的元素

var index = 0
let firstName = twoDimensionalArray[indexPath.row].names[index]

关于在 indexPath.row 的数组中快速打印元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673573/

相关文章:

ios - Xcode8.3-无效的位码签名

ios - Codemagic iOS Flutter 部署在存档时失败

ios - didSelectRowAtIndexPath 只被调用一次

ios - Swift:触发 TableViewCell 以导致另一个 ViewController 中的 UIWebView 中的链接

ios - 在 tableView 中选择单元格后布局发生变化

regex - Swift中字符串中子字符串的出现次数

swift - ARAnchor 和 AnchorEntity 有什么区别?

ios - UITableViewCell 配件选择

ios - 尝试提交带有 Siri 扩展的应用程序时,来自 App Store 的错误 "Invalid Intent Vocabulary"

swift - 使用 prepareForSegue 时 indexPathForSelectedRow 错误