arrays - iOS/Swift:为什么 contains 不能检测 .firstIndex 函数中的子字符串?
问题描述
我正在使用函数 .firstIndex 来定位数组数组中的特定子字符串。但是当我放置整个字符串时,它可以工作,但是如果我只放置该字符串的一个子字符串,这将不起作用。
let index = programArray.firstIndex(where: {$0.contains("2021-14-09")}) //Index is nil => bad
let index = programArray.firstIndex(where: {$0.contains("2021-14-09 08:00:00")}) // Index is 0 => good
如果我不在 firstIndex 函数中使用它,它会起作用,这很奇怪......
let test = "2021-14-09 08:00:00"
let test2 = test.prefix(10).contains("2021-14-09") // true => good
我能怎么做?
解决方案
您使用 contains(_:),但如果您查看Apple Developer Documentation,您会看到:
返回一个布尔值,指示序列是否包含给定元素。
所以如果你使用包含,整个字符串必须匹配。
推荐阅读
- ssl - 从多个受信任的 CA 提供商处请求 ssl 证书是否会产生负面影响?
- sql - 如何将子查询结果(具有嵌套连接)与另一个表连接?
- vb.net - 嵌套列表/列表集合
- java - 出现键盘时 ScrollView 不起作用
- javascript - 如何将数据从一个html页面传递到另一个?
- python - 自定义 Spacy NER 模型的总体 F 分数与单个实体 F 分数之间的巨大差异
- python - 有没有办法让这个字符串减少到 3 个单词?
- ffmpeg - FFMPEG如何在视频拼接时添加虚拟音频
- r - 有没有办法在 R 的 for 循环中使用 which 参数?
- css - 无法弄清楚背景图像:url();