如何创建一个循环来查找可用值,直到它显示为真
how to create a loop to find available values until it shows up true
我正在开发一个时隙系统,该系统将提供可用时隙和不可用时隙,但我正在过滤可用时隙并加载阵列。有时很多天都没有可用的时间段。我想通过下面响应中“可用”布尔参数的循环找到最近可用日期的下一个可用插槽(我只粘贴了部分响应。
{
"timeslots" : [
{
"slots" : [
{
"day" : "tuesday",
"selected" : false,
"pk" : 160,
"available" : false,
"timeframe" : "10:00 - 12:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 161,
"available" : true,
"timeframe" : "12:00 - 14:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 162,
"available" : true,
"timeframe" : "15:00 - 17:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 163,
"available" : true,
"timeframe" : "17:00 - 19:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 164,
"available" : false,
"timeframe" : "19:00 - 21:00"
}
],
"day_name" : "tuesday",
"date" : "2022-04-26",
"available" : 3,
"day_abbr" : "26 Apr"
},
{
"slots" : [
{
"pk" : 160,
"day" : "wednesday",
"timeframe" : "10:00 - 12:00",
"selected" : false,
"available" : true
},
{
"pk" : 161,
"day" : "wednesday",
"timeframe" : "12:00 - 14:00",
"selected" : false,
"available" : true
},
{
"pk" : 162,
"day" : "wednesday",
"timeframe" : "15:00 - 17:00",
"selected" : false,
"available" : true
},
{
"pk" : 163,
"day" : "wednesday",
"timeframe" : "17:00 - 19:00",
"selected" : false,
"available" : true
},
{
"pk" : 164,
"day" : "wednesday",
"timeframe" : "19:00 - 21:00",
"selected" : false,
"available" : false
}
],
"day_name" : "wednesday",
"date" : "2022-04-27",
"available" : 4,
"day_abbr" : "27 Apr"
},
到目前为止我做了这样的迭代。
switch response.result {
case let .success(value):
let json = JSON(value)
print("Time slot response: \(json)")
self.slots.removeAll()
self.daySlots.removeAll()
self.typeOfSlot = slotType
if let slotArray = json[slotType][self.dayIndex ?? 0]["slots"].array{
if !slotArray.isEmpty || slotArray != [] {
for slotJSON in slotArray {
let slot = Slots.parseSlots(slotJSON: slotJSON)
self.slots.append(slot)
}
self.slots = self.slots.filter({ [=11=].available ?? false })
//
if self.slots.count != 0{
switch self.typeOfSlot{
case "seasonal_timeslots":
self.seasonalTimeSlotPK = self.slots.first?.pk
print("time slot pk \(self.seasonalTimeSlotPK ?? 0)")
UserDefaults.standard.set(self.seasonalTimeSlotPK, forKey:uds.kSeasonalTimeslotPK)
self.selectedSeasonalTimeRange = self.slots.first?.timeFrame
UserDefaults.standard.set(self.selectedSeasonalTimeRange, forKey:uds.kSeasonalTimeRange)
case "timeslots":
self.timeSlotPK = self.slots.first?.pk
print("time slot pk \(self.timeSlotPK ?? 0)")
UserDefaults.standard.set(self.timeSlotPK ?? 0, forKey:uds.kTimeslotPK)
self.selectedTimeRange = self.slots.first?.timeFrame
UserDefaults.standard.set(self.selectedTimeRange ?? "", forKey:uds.kTimeRange)
default:
break
}
}
// timeframe
}
else {
// self.tableView.setEmptyView(title: "No available slots found for current date!", message: "Please tap on other days for next available slots.")
self.view.makeToast("No Slots Found!", duration: 3.0, position: .bottom)
}
请给我代码示例,让我搜索可用的插槽和停止,直到找到它。
我不太确定您的代码中发生了什么。但是您要求在插槽数组中找到第一个可用插槽?
我制作了一些结构来跟踪数据并使用 Swift 4 的 Decodable 来更容易地表示数据:
struct Timeslots: Decodable {
let slots: [Slot]
var firstFreeTime: Slot? {
slots.first(where: { [=10=].available })
}
}
struct Slot: Decodable {
let day: String
let selected: Bool
let pk: Int
let available: Bool
let timeFrame: String
}
而不是使用 json 原始数据,
您可以 re-structure 您的代码并使用特定模型来完成您的要求。这是一些要显示的示例测试代码
如何获得插槽。
struct Response: Identifiable, Codable {
let id = UUID()
var timeslots: [Timeslot]
}
struct Timeslot: Codable {
let slots: [Slot]
let dayName: String
let date: String
let available: Int
let dayAbbr: String
enum CodingKeys: String, CodingKey {
case slots
case dayName = "day_name"
case date, available
case dayAbbr = "day_abbr"
}
}
struct Slot: Identifiable, Codable {
let id = UUID()
let day: String
let selected: Bool
let pk: Int
let available: Bool
let timeframe: String
}
struct ContentView: View {
var body: some View {
Text("testing")
.onAppear {
let json = """
{
"timeslots" : [
{
"slots" : [
{
"day" : "tuesday",
"selected" : false,
"pk" : 160,
"available" : false,
"timeframe" : "10:00 - 12:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 161,
"available" : true,
"timeframe" : "12:00 - 14:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 162,
"available" : true,
"timeframe" : "15:00 - 17:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 163,
"available" : true,
"timeframe" : "17:00 - 19:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 164,
"available" : false,
"timeframe" : "19:00 - 21:00"
}
],
"day_name" : "tuesday",
"date" : "2022-04-26",
"available" : 3,
"day_abbr" : "26 Apr"
},
{
"slots" : [
{
"pk" : 160,
"day" : "wednesday",
"timeframe" : "10:00 - 12:00",
"selected" : false,
"available" : true
},
{
"pk" : 161,
"day" : "wednesday",
"timeframe" : "12:00 - 14:00",
"selected" : false,
"available" : true
},
{
"pk" : 162,
"day" : "wednesday",
"timeframe" : "15:00 - 17:00",
"selected" : false,
"available" : true
},
{
"pk" : 163,
"day" : "wednesday",
"timeframe" : "17:00 - 19:00",
"selected" : false,
"available" : true
},
{
"pk" : 164,
"day" : "wednesday",
"timeframe" : "19:00 - 21:00",
"selected" : false,
"available" : false
}
],
"day_name" : "wednesday",
"date" : "2022-04-27",
"available" : 4,
"day_abbr" : "27 Apr"
}
]
}
"""
let data = json.data(using: .utf8)!
do {
let response = try JSONDecoder().decode(Response.self, from: data)
// print("\n--> response: \(response) \n")
// here loop over the timeslots
for tmslot in response.timeslots {
// here loop over the slots
for slot in tmslot.slots {
if slot.available {
print("slot available: \(slot.day) \(slot.timeframe)")
}
}
}
} catch {
print("error: \(error)")
}
}
}
}
我正在开发一个时隙系统,该系统将提供可用时隙和不可用时隙,但我正在过滤可用时隙并加载阵列。有时很多天都没有可用的时间段。我想通过下面响应中“可用”布尔参数的循环找到最近可用日期的下一个可用插槽(我只粘贴了部分响应。
{
"timeslots" : [
{
"slots" : [
{
"day" : "tuesday",
"selected" : false,
"pk" : 160,
"available" : false,
"timeframe" : "10:00 - 12:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 161,
"available" : true,
"timeframe" : "12:00 - 14:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 162,
"available" : true,
"timeframe" : "15:00 - 17:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 163,
"available" : true,
"timeframe" : "17:00 - 19:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 164,
"available" : false,
"timeframe" : "19:00 - 21:00"
}
],
"day_name" : "tuesday",
"date" : "2022-04-26",
"available" : 3,
"day_abbr" : "26 Apr"
},
{
"slots" : [
{
"pk" : 160,
"day" : "wednesday",
"timeframe" : "10:00 - 12:00",
"selected" : false,
"available" : true
},
{
"pk" : 161,
"day" : "wednesday",
"timeframe" : "12:00 - 14:00",
"selected" : false,
"available" : true
},
{
"pk" : 162,
"day" : "wednesday",
"timeframe" : "15:00 - 17:00",
"selected" : false,
"available" : true
},
{
"pk" : 163,
"day" : "wednesday",
"timeframe" : "17:00 - 19:00",
"selected" : false,
"available" : true
},
{
"pk" : 164,
"day" : "wednesday",
"timeframe" : "19:00 - 21:00",
"selected" : false,
"available" : false
}
],
"day_name" : "wednesday",
"date" : "2022-04-27",
"available" : 4,
"day_abbr" : "27 Apr"
},
到目前为止我做了这样的迭代。
switch response.result {
case let .success(value):
let json = JSON(value)
print("Time slot response: \(json)")
self.slots.removeAll()
self.daySlots.removeAll()
self.typeOfSlot = slotType
if let slotArray = json[slotType][self.dayIndex ?? 0]["slots"].array{
if !slotArray.isEmpty || slotArray != [] {
for slotJSON in slotArray {
let slot = Slots.parseSlots(slotJSON: slotJSON)
self.slots.append(slot)
}
self.slots = self.slots.filter({ [=11=].available ?? false })
//
if self.slots.count != 0{
switch self.typeOfSlot{
case "seasonal_timeslots":
self.seasonalTimeSlotPK = self.slots.first?.pk
print("time slot pk \(self.seasonalTimeSlotPK ?? 0)")
UserDefaults.standard.set(self.seasonalTimeSlotPK, forKey:uds.kSeasonalTimeslotPK)
self.selectedSeasonalTimeRange = self.slots.first?.timeFrame
UserDefaults.standard.set(self.selectedSeasonalTimeRange, forKey:uds.kSeasonalTimeRange)
case "timeslots":
self.timeSlotPK = self.slots.first?.pk
print("time slot pk \(self.timeSlotPK ?? 0)")
UserDefaults.standard.set(self.timeSlotPK ?? 0, forKey:uds.kTimeslotPK)
self.selectedTimeRange = self.slots.first?.timeFrame
UserDefaults.standard.set(self.selectedTimeRange ?? "", forKey:uds.kTimeRange)
default:
break
}
}
// timeframe
}
else {
// self.tableView.setEmptyView(title: "No available slots found for current date!", message: "Please tap on other days for next available slots.")
self.view.makeToast("No Slots Found!", duration: 3.0, position: .bottom)
}
请给我代码示例,让我搜索可用的插槽和停止,直到找到它。
我不太确定您的代码中发生了什么。但是您要求在插槽数组中找到第一个可用插槽?
我制作了一些结构来跟踪数据并使用 Swift 4 的 Decodable 来更容易地表示数据:
struct Timeslots: Decodable {
let slots: [Slot]
var firstFreeTime: Slot? {
slots.first(where: { [=10=].available })
}
}
struct Slot: Decodable {
let day: String
let selected: Bool
let pk: Int
let available: Bool
let timeFrame: String
}
而不是使用 json 原始数据, 您可以 re-structure 您的代码并使用特定模型来完成您的要求。这是一些要显示的示例测试代码 如何获得插槽。
struct Response: Identifiable, Codable {
let id = UUID()
var timeslots: [Timeslot]
}
struct Timeslot: Codable {
let slots: [Slot]
let dayName: String
let date: String
let available: Int
let dayAbbr: String
enum CodingKeys: String, CodingKey {
case slots
case dayName = "day_name"
case date, available
case dayAbbr = "day_abbr"
}
}
struct Slot: Identifiable, Codable {
let id = UUID()
let day: String
let selected: Bool
let pk: Int
let available: Bool
let timeframe: String
}
struct ContentView: View {
var body: some View {
Text("testing")
.onAppear {
let json = """
{
"timeslots" : [
{
"slots" : [
{
"day" : "tuesday",
"selected" : false,
"pk" : 160,
"available" : false,
"timeframe" : "10:00 - 12:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 161,
"available" : true,
"timeframe" : "12:00 - 14:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 162,
"available" : true,
"timeframe" : "15:00 - 17:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 163,
"available" : true,
"timeframe" : "17:00 - 19:00"
},
{
"day" : "tuesday",
"selected" : false,
"pk" : 164,
"available" : false,
"timeframe" : "19:00 - 21:00"
}
],
"day_name" : "tuesday",
"date" : "2022-04-26",
"available" : 3,
"day_abbr" : "26 Apr"
},
{
"slots" : [
{
"pk" : 160,
"day" : "wednesday",
"timeframe" : "10:00 - 12:00",
"selected" : false,
"available" : true
},
{
"pk" : 161,
"day" : "wednesday",
"timeframe" : "12:00 - 14:00",
"selected" : false,
"available" : true
},
{
"pk" : 162,
"day" : "wednesday",
"timeframe" : "15:00 - 17:00",
"selected" : false,
"available" : true
},
{
"pk" : 163,
"day" : "wednesday",
"timeframe" : "17:00 - 19:00",
"selected" : false,
"available" : true
},
{
"pk" : 164,
"day" : "wednesday",
"timeframe" : "19:00 - 21:00",
"selected" : false,
"available" : false
}
],
"day_name" : "wednesday",
"date" : "2022-04-27",
"available" : 4,
"day_abbr" : "27 Apr"
}
]
}
"""
let data = json.data(using: .utf8)!
do {
let response = try JSONDecoder().decode(Response.self, from: data)
// print("\n--> response: \(response) \n")
// here loop over the timeslots
for tmslot in response.timeslots {
// here loop over the slots
for slot in tmslot.slots {
if slot.available {
print("slot available: \(slot.day) \(slot.timeframe)")
}
}
}
} catch {
print("error: \(error)")
}
}
}
}