子クラスにアクセス可能な親クラスで関数を作成しようとしています。私が持っている問題は、その関数の一部が、子クラスで発生する必要のあるinitを指していることです。私はエラーを取得する:親クラスのすべての子クラスにアクセス可能な関数を作成する
Argument passed to call that takes no arguments
私はコピーせずに、それの子クラスの機能が利用できるようにする方法を完全にはわからないと、それぞれの子クラスの中にそれを貼り付けます。ここで
は、親クラスである:
class JSONObject: NSObject {
static func updateResultsDictionary(urlExtension: String, completion:
@escaping (JSONObject?) -> Void) {
let nm = NetworkManager.sharedManager
_ = nm.getJSONData(urlExtension: urlExtension) {data in
guard let jsonDictionary = nm.parseJSONFromData(data), let
resultDictionaries = jsonDictionary["result"] as?
[[String : Any]] else {
completion(nil)
return
}
for resultsDictionary in resultDictionaries {
let jsonInfo = JSONObject(resultsDictionary: resultsDictionary)// Here is where the error "Argument passed to call that takes no arguments" happens
completion(jsonInfo)
}
}
}
}
これは、サンプルの子クラスである:
class AirBnBObject: JSONObject {
var airbnbUS: Int
var airbnbLocal: Int
init(airbnbUS: Int, airbnbLocal: Int){
self.airbnbUS = airbnbUS
self.airbnbLocal = airbnbLocal
}
init(resultsDictionary:[String: Any]){
guard let cost = resultsDictionary["cost"] as? [String: Any],
let airbnb = cost["airbnb_median"] as? [String : Any],
let usd = airbnb["USD"] as? Int,
let chf = airbnb["CHF"] as? Int
else {
airbnbUS = 0
airbnbLocal = 0
return
}
airbnbUS = usd
airbnbLocal = chf
}
}
エラーが発生しているのは、 'JSONObject'に引数を取るイニシャライザがないためです。 'AirBnBObject'クラスで初期化子を定義しているので、' resultsDictionary'と呼ばれます。 – nathan