比较 String 和 MVC 的 String 值 [英] Compare String with String value of MVC

查看:49
本文介绍了比较 String 和 MVC 的 String 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

collectionView 的单元格中查看我的数据我使用 MVC 架构来保持代码干净

to view my data inside the cells of a collectionView I am using MVC architecture to keep the code clean

现在我有一个名为 TimeSelModel 的类,它处理有模型函数

Now I have a class called TimeSelModel which handles has the model function

它的结构是这样的

struct Section<T> { 
   let model: [T] 
}


struct TimeSelModel {
    let hour: String
    let minute: String
}

let dataSec0 = [
    TimeSelModel(hour: "09", minute: ":30"),
    TimeSelModel(hour: "17", minute: ":00")
]

let dataSec1 = [
    TimeSelModel(hour: "12", minute: ":00"),
    TimeSelModel(hour: "19", minute: ":00")
]


我以这种方式使用这些数据在collectionView

private var data: [Section<TimeSelModel>] = []

private func fetchData() -> Void {
        data = [Section(model: dataSec0), Section(model: dataSec1)]
    }

func numberOfSections(in collectionView: UICollectionView) -> Int { data.count }
    
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        data[section].model.count }
    
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TimeSelCell.cellID, for: indexPath) as! TimeSelCell
        
        cell.dataModel = data[indexPath.section].model[indexPath.item]
              
        return cell
    }


现在我需要在一个单独的函数中将 string 与我正在使用的模型值进行比较


Now I need in a separate function to compare a string with the model values ​​I am using

例如,我需要将 string 与模型值中的值 12" 进行比较

For example I need to compare a string with value "12" within the model values

let dataSec0 = [
        TimeSelModel (hour: "09", minute: ": 30"),
        TimeSelModel (hour: "17", minute: ": 00")
    ]
    
let dataSec1 = [
        TimeSelModel (hour: "12", minute: ": 00"),
        TimeSelModel (hour: "19", minute: ": 00")
    ]

将其放入我的控制器的最佳和最干净的方法是什么?

What is the best and cleanest way to get this into my controller?

推荐答案

由于您在问题中有一个示例,然后在评论中有一个不同的示例,因此这里有两种方法可以在您的 中获取特定时间的索引数据数组.

Since you have one example in the question and then a different example in the comments here are 2 ways to get the index of a certain time in your data array.

let hourValue = "12"
if let index = data.firstIndex { section in section.model.contains { $0.hour == hourValue } } {
    let section = data[index]
    //...
}

let timeValue = "17:00"
if let index = data.firstIndex { section in section.model.contains { $0.time == timeValue } } {
    let section = data[index]
    //...
}

最后一个示例使用了我添加到 TimeSelModel

The last example uses a computed property that I added to TimeSelModel

struct TimeSelModel {
    let hour: String
    let minute: String

    var time: String {
        "\(hour):\(minute)"
    }
}

请注意,它添加了一个:";因为我认为在分钟字符串中包含冒号而不是:00"不是一个好主意;或:00"它应该只是00".(人们甚至可以争辩说小时和分钟应该是整数,但这超出了本答案的范围)

Note that it adds a ":" since I don't think it is a good idea to include the colon in the minute string as you do so instead of ":00" or ": 00" it should be just "00". (One could even argue that hour and minute should be integers but that is outside of the scope of this answer)

另请注意,此答案基于我对 Section 类型的解释.

Also note that this answer is based on my interpretation of what the type Section is.

这篇关于比较 String 和 MVC 的 String 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆