将 String 与 MVC 的 String 值进行比较
Compare String with String value of MVC
在 collectionView
的单元格中查看我的数据 我正在使用 MVC 架构来保持代码整洁
现在我有一个名为 TimeSelModel
的 class,它的手柄具有模型功能
它的结构是这样的
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
和我正在使用的模型值
例如,我需要比较 string
与模型值 "12" 中的值
let dataSec0 = [
TimeSelModel (hour: "09", minute: ": 30"),
TimeSelModel (hour: "17", minute: ": 00")
]
let dataSec1 = [
TimeSelModel (hour: "12", minute: ": 00"),
TimeSelModel (hour: "19", minute: ": 00")
]
将它放入我的控制器的最佳和最干净的方法是什么?
字符串的 unicode 值类似于 myTime.hour < "12"
。
这意味着 "0" < "1"
为真
但是"2" < "12"
是错误的
在您的情况下,您应该将字符串与基金会的函数进行比较 https://developer.apple.com/documentation/foundation/nsstring/1408732-compare
并给出 options
参数 .numeric
: https://developer.apple.com/documentation/foundation/nsstring/compareoptions/1415530-numeric
switch myTime.hour.compare("12", options: [.numeric]) {
case .orderedAscending:
// myTime.hour < "12"
case .orderedSame:
// myTime.hour == "12"
case .orderedDescending:
// myTime.hour > "12"
}
但我建议使用 Int 或 TimeInterval 更改您的模型,并使用一个函数将您的模型转换为您想要的可读字符串
由于您在问题中有一个示例,然后在此处的评论中有一个不同的示例,因此有两种方法可以在您的 data
数组中获取特定时间的索引。
let hourValue = "12"
if let index = data.firstIndex { section in section.model.contains { [=10=].hour == hourValue } } {
let section = data[index]
//...
}
和
let timeValue = "17:00"
if let index = data.firstIndex { section in section.model.contains { [=11=].time == timeValue } } {
let section = data[index]
//...
}
最后一个示例使用我添加到 TimeSelModel
的计算 属性
struct TimeSelModel {
let hour: String
let minute: String
var time: String {
"\(hour):\(minute)"
}
}
请注意,它添加了一个“:”,因为我认为在分钟字符串中包含冒号不是一个好主意,而不是“:00”或“: 00”,它应该是只是“00”。 (甚至可以争辩说小时和分钟应该是整数,但这超出了这个答案的范围)
另请注意,此答案基于我对 Section
类型的解释。
在 collectionView
的单元格中查看我的数据 我正在使用 MVC 架构来保持代码整洁
现在我有一个名为 TimeSelModel
的 class,它的手柄具有模型功能
它的结构是这样的
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
和我正在使用的模型值
例如,我需要比较 string
与模型值 "12" 中的值
let dataSec0 = [
TimeSelModel (hour: "09", minute: ": 30"),
TimeSelModel (hour: "17", minute: ": 00")
]
let dataSec1 = [
TimeSelModel (hour: "12", minute: ": 00"),
TimeSelModel (hour: "19", minute: ": 00")
]
将它放入我的控制器的最佳和最干净的方法是什么?
字符串的 unicode 值类似于 myTime.hour < "12"
。
这意味着 "0" < "1"
为真
但是"2" < "12"
是错误的
在您的情况下,您应该将字符串与基金会的函数进行比较 https://developer.apple.com/documentation/foundation/nsstring/1408732-compare
并给出 options
参数 .numeric
: https://developer.apple.com/documentation/foundation/nsstring/compareoptions/1415530-numeric
switch myTime.hour.compare("12", options: [.numeric]) {
case .orderedAscending:
// myTime.hour < "12"
case .orderedSame:
// myTime.hour == "12"
case .orderedDescending:
// myTime.hour > "12"
}
但我建议使用 Int 或 TimeInterval 更改您的模型,并使用一个函数将您的模型转换为您想要的可读字符串
由于您在问题中有一个示例,然后在此处的评论中有一个不同的示例,因此有两种方法可以在您的 data
数组中获取特定时间的索引。
let hourValue = "12"
if let index = data.firstIndex { section in section.model.contains { [=10=].hour == hourValue } } {
let section = data[index]
//...
}
和
let timeValue = "17:00"
if let index = data.firstIndex { section in section.model.contains { [=11=].time == timeValue } } {
let section = data[index]
//...
}
最后一个示例使用我添加到 TimeSelModel
struct TimeSelModel {
let hour: String
let minute: String
var time: String {
"\(hour):\(minute)"
}
}
请注意,它添加了一个“:”,因为我认为在分钟字符串中包含冒号不是一个好主意,而不是“:00”或“: 00”,它应该是只是“00”。 (甚至可以争辩说小时和分钟应该是整数,但这超出了这个答案的范围)
另请注意,此答案基于我对 Section
类型的解释。