我正在研究一个时隙系统,它将给出可用的时隙和不可用的时隙,但我正在过滤可用的时隙并加载数组。有时候很多天没有空余的时间段。我想通过循环从"available"中找到最近可用天数的下一个可用插槽。Bool来自下面响应的参数(我只粘贴了响应的一部分。
{
"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({ $0.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)
}
请给我代码的例子,让我搜索可用的插槽和停止,直到它被发现。
而不是使用json原始数据,您可以重新构建代码并使用特定的模型来完成您的要求。下面是一些示例测试代码如何获得插槽。
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)")
}
}
}
}
我真的不知道你的代码中发生了什么。但是您要求在插槽数组中找到第一个可用的插槽?
我创建了一些结构体来跟踪数据,并使用Swift 4的decoable来更容易地表示数据:
struct Timeslots: Decodable {
let slots: [Slot]
var firstFreeTime: Slot? {
slots.first(where: { $0.available })
}
}
struct Slot: Decodable {
let day: String
let selected: Bool
let pk: Int
let available: Bool
let timeFrame: String
}