您可以这样做:
struct Price {
let roomType: String
let price: Double
}
struct Hotel {
let hotelName: String
let hotelType: Int
let prices: [Price]
}
let hotel1 = Hotel(hotelName: "Hotel 1", hotelType: 1, prices: [
Price(roomType: "Single Room", price: 1231),
Price(roomType: "Twin Room", price: 1232),
Price(roomType: "Triple Room", price: 1233)
])
let hotel2 = Hotel(hotelName: "Hotel 2", hotelType: 1, prices: [
Price(roomType: "Single Room", price: 1241),
Price(roomType: "Twin Room", price: 1242),
Price(roomType: "Triple Room", price: 1243)
])
func getHotelsInRange(_ hotels: [Hotel],
from min: Double,
to max: Double) -> [Hotel] {
return hotels.filter { h in
h.prices.contains{ p in
switch p.price {
case min...max:
return true
default:
return false
}
}
}
}
let result = getHotelsInRange([hotel1, hotel2], from: min, to: max)