在 Swift 3 中使用未声明的类型

Use of undeclared type in Swift 3

我有以下class到return的NOAA气象观测站列表。我正在用它来学习如何处理 XML。但是,我在 func returnWxStation() -> (wxObservationStations) 收到一个 "Use of undeclared type 'wxObservationStations'" 作为错误。我正在使用 SWXMLHash 反序列化 XML,但我不认为这是我的问题(虽然我只是在学习,所以它可能是)。

class WxObservationStations {

let wxObserStationsURL = URL(string: "http://w1.weather.gov/xml/current_obs/index.xml")

struct wxStation: XMLIndexerDeserializable {
    let stationName: String
    let stationState: String
    let latitude: Double
    let longitude: Double

    static func deserialize(_ node: XMLIndexer) throws -> wxStation {
        return try wxStation(
            stationName: node["station_name"].value(),
            stationState: node["state"].value(),
            latitude: node["latitude"].value(),
            longitude: node["longitude"].value()
        )
    }
}

public var wxObservationStations: [wxStation] = []


private func getStationNamesAndLocations(url: URL, completion:@escaping (XMLIndexer) -> ()) {

    Alamofire.request(url).responseJSON { response in
        //                print(response) // To check XML data in debug window.
        let wxStationList = SWXMLHash.parse(response.data!)
        print(wxStationList)
        completion(wxStationList)

    }
}

//The error is here:
func returnWxStation() -> (wxObservationStations) {
    getStationNamesAndLocations(url: wxObserStationsURL!, completion: { serverResponse in
        do {
            self.wxObservationStations = try serverResponse["wx_station_index"]["station"].value()

        } catch {

        }
    })
      return self.wxObservationStations
}
}

有什么想法吗?该变量在 class 中声明,我想用它来将数据发送回请求对象。提前致谢。

wxObservationStations 不是类型,所以说

没有意义
func returnWxStation() -> (wxObservationStations) { ... }

您返回 self.wxObservationStations,类型为 [wxStation]。所以方法声明应该是

func returnWxStation() -> [wxStation] { ... }

顺便说一句,如果您坚持 Cocoa 命名约定,您的生活将会轻松得多,即类型应以大写字母开头。所以我建议 WxStation.

而不是 wxStation 类型

您的以下方法不会达到您想要的效果:

func returnWxStation() -> [wxStation] {
    getStationNamesAndLocations(url: wxObserStationsURL!, completion: { serverResponse in
        do {
            self.wxObservationStations = try serverResponse["wx_station_index"]["station"].value()
        } catch {

        }
    })

    return self.wxObservationStations
}

方法 getStationNamesAndLocations 异步运行,您的 self.wxObservationStations 不会在 returnWxStation 实际上 returns 时填充。

getStationNamesAndLocations 方法的全部目的是为您提供一个带有完成处理程序的漂亮的异步方法。我将从您的代码中完全删除 returnWxStation 。或者做类似的事情:

func returnWxStation(completionHandler: ([wxStation]?) -> Void) {
    getStationNamesAndLocations(url: wxObserStationsURL!) { serverResponse in
        do {
            let stations = try serverResponse["wx_station_index"]["station"].value()
            completionHandler(stations)
        } catch {
            completionHandler(nil)
        }
    }
}

你会像这样使用它:

returnWxStation() { stations in
    guard let stations = stations else {
        // handle error here
        return
    }

    // use `stations` here
}

// but not here