私は再利用したい機能があり、Decocable構造体のパラメータを受け入れる必要があります。たとえば、これは私の現在のコードを簡略化したものである(「MyDecodableStructは」復号可能な構造体は、アプリ内の他の場所で宣言されていると仮定):Swift 4で一般的なDecodable構造体を参照する方法
static func getResults(url: String, parameters: Parameters) {
// On success REST response
if response.result.isSuccess {
struct Results: Decodable {
let items: [MyDecodableStruct]
}
if let jsonResults = try? JSONDecoder().decode(Results.self, from: response.data!) {
//success
}
}
、代わりの「MyDecodableStructを」と言って、私はそれがどの復号可能になりたいですstruct私はパラメータとして渡します。このような何か:
static func getResults(url: String, parameters: Parameters, myStruct: Decodable) {
// On success REST response
if response.result.isSuccess {
struct Results: Decodable {
let items: [myStruct]
}
if let jsonResults = try? JSONDecoder().decode(Results.self, from: response.data!) {
//success
}
}
と私は、これはしかし動作するように取得する方法についての構文を把握することはできません
getResults(url: "url", parameters: nil, myStruct: MyDecodableStruct)
のようにそれを呼び出します。私が得るエラーは
Type 'Results' does not conform to protocol 'Decodable'
Expected element type
これを処理するための最良の方法はありますか?
:
は、としてそれを呼び出します! –