无法在范围内找到变量。 SwiftUI Switch 语句

问题描述 投票:0回答:1

我有一个 switch 语句需要在这里澄清。从 xCode 编译器返回的错误是 * 从附加的代码中无法在范围内找到“查询”。我有一系列 let 语句,具体取决于正确输入的 filterOptions。 我不明白为什么它会说变量不在范围内。任何帮助表示赞赏

class AirportsDataSource {
    
    var db: Connection?
    var filterOption: String = "IATA"
    
    init() {
        
        do {
            guard let fileURL = Bundle.main.url(forResource: "airports", withExtension: "sqlite") else 
            {
                print("Could not find the airports.sqlite file in the app bundle.")
                return
            }
            db = try Connection(fileURL.path)
            
        } catch {
            print(error)
        }
    }
    
    func search(_ text: String) -> [Airport] {
        guard let db = db else { return [] }
        
        do {
            let airports = Table("mytable")
            let airportId = Expression<Int?>("airportId")
            let codeIataAirport = Expression<String?>("codeIataAirport")
            let codeICAOAirport = Expression<String?>("codeICAOAirport")
            let name = Expression<String?>("name")
            let nameAirportName = Expression<String?>("nameAirport")
            let latitude = Expression<String?>("latitudeAirport")
            let longitude = Expression<String?>("longitudeAirport")
            
            switch filterOption {
                case "IATA":
                    let query = airports
                       .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
                       .where(
                           codeICAOAirport.like("%\(text)%")
                       )
                       .order(codeICAOAirport)
                       .limit(100)
                case "ICAO":
                    let query = airports
                        .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
                        .where(
                            name.like("%\(text)%") ||
                            nameAirportName.like("%\(text)%")
                        )
                        .order(codeICAOAirport)
                        .limit(100)
                case "Airport Name":
                     let query = airports
                        .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
                        .where(
                            name.like("%\(text)%") ||
                            nameAirportName.like("%\(text)%")
                        )
                        .order(nameAirportName)
                       .limit(100)
                default:
                     let query = airports
                        .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
                        .where(
                            codeIataAirport.like("%\(text)%") ||
                            codeICAOAirport.like("%\(text)%") ||
                            name.like("%\(text)%") ||
                            nameAirportName.like("%\(text)%")
                        )
                        .order(codeIataAirport.asc)
                        .limit(100)
            }
            
            return try db.prepare(query).map
            { .init(
                id: $0[airportId] ?? .random(in: 11000...12000),
                name: $0[nameAirportName] ?? "N/A",
                code: $0[codeIataAirport] ?? "N/A",
                lat: $0[latitude] ?? "0.0",
                long: $0[longitude] ?? "0.0"
            ) }
            
          
        } catch {
            print("error reading table", error)
            return []
        }
    }
    
}

我期望根据 switch 语句构建表达式和数据库查询,然后使用数据库查询来运行查询

swiftui switch-statement
1个回答
0
投票

您在

query
案例中本地创建
switch
。局部变量仅在其大括号内可见,或者在本例中是在 case 的范围内可见。

我不知道

query
的类型,但你必须在
switch
语句上方声明它,并删除案例中出现的
let

let query : Query
switch filterOption {
    case "IATA":
        query = airports
           .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
           .where(
               codeICAOAirport.like("%\(text)%")
           )
           .order(codeICAOAirport)
           .limit(100)
    case "ICAO":
        query = airports
            .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
            .where(
                name.like("%\(text)%") ||
                nameAirportName.like("%\(text)%")
            )
            .order(codeICAOAirport)
            .limit(100)
    case "Airport Name":
         query = airports
            .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
            .where(
                name.like("%\(text)%") ||
                nameAirportName.like("%\(text)%")
            )
            .order(nameAirportName)
           .limit(100)
    default:
         query = airports
            .select(airportId, codeIataAirport, nameAirportName, latitude, longitude)
            .where(
                codeIataAirport.like("%\(text)%") ||
                codeICAOAirport.like("%\(text)%") ||
                name.like("%\(text)%") ||
                nameAirportName.like("%\(text)%")
            )
            .order(codeIataAirport.asc)
            .limit(100)
}

return try db.prepare(query).map
{ .init(
    id: $0[airportId] ?? .random(in: 11000...12000),
    name: $0[nameAirportName] ?? "N/A",
    code: $0[codeIataAirport] ?? "N/A",
    lat: $0[latitude] ?? "0.0",
    long: $0[longitude] ?? "0.0"
) }
© www.soinside.com 2019 - 2024. All rights reserved.