小编Wah*_*hib的帖子

在 Swift 中,如何在由 struct 对象数组组成的字典中进行搜索?

我有一个结构:

struct AlimentObject {
        var id = UUID().uuidString
        var nomAliment = "Nouvel aliment"
        var poids : Float = 100
        var calories : Float = 0
}
Run Code Online (Sandbox Code Playgroud)

以及由 AlimentObjet 组成的数组:

   var filteredData: [AlimentObject]!
Run Code Online (Sandbox Code Playgroud)

我有一个表格视图的搜索栏,但我不知道如何搜索“nomAliment”中的字符,这里是搜索栏的代码:

extension AjoutAlimentController: UISearchBarDelegate {
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filteredData = []
        for aliment in data {
            if aliment.nomAliment.lowercased().contains(searchText.lowercased) {
                filteredData.append(aliment)
            }
        }
        self.tableView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

错误说:类型 '() -> String' 不能符合 'StringProtocol'; 只有结构/枚举/类类型可以符合它在行中的协议:

if aliment.nomAliment.lowercased().contains(searchText.lowercased) {
Run Code Online (Sandbox Code Playgroud)

filter uitableview uisearchbar swift

0
推荐指数
1
解决办法
41
查看次数

在 swift 中,如何创建一个数组,其中包含与第一个数组中存在的一个月中所有天数相对应的所有数字?

我有一本这样的字典:

var dictionary : [Date: [[Objects]]]
Run Code Online (Sandbox Code Playgroud)

我需要创建一个包含特定年份特定月份的所有日期的数组。当我指定年份和月份时,结果将是这样的:

但在此之前是我创建的变量:

let year: Int = 2020
let month: Int = 5

var array : [Date] =[]

Run Code Online (Sandbox Code Playgroud)

结果可能是这样的:

array = [date1, date2, date3, date10, date25, date30] // There is only these days for the fifth month in the dictionary
Run Code Online (Sandbox Code Playgroud)

该数组将在初始字典中仅包含 2020 年第五个月的日期。

这是我尝试过但失败的方法:

let dateFormatter = DateFormatter()

dateFormatter = "MM dd yyyy" // to have the numbers of the month and not strings

func arrayOfdays(month: Int, year: Int) -> [Date] {
    for day in …
Run Code Online (Sandbox Code Playgroud)

arrays date swift

-1
推荐指数
1
解决办法
47
查看次数

标签 统计

swift ×2

arrays ×1

date ×1

filter ×1

uisearchbar ×1

uitableview ×1