简体   繁体   中英

How to assign parameter's type of generic callback in swift?

Suppose I have following function:

func fetch<T: Codable>(completion: @escaping (_ response: SimpleResult<[T]>) -> Void) {
      completion(parse(result))
}

Xcode does not show any error on above function. But how to use it? If I write following, it shows error:

fetch { result in 
}

It says that "Generic parameter T could not be inferred". I suppose that I should indicate type here. But where should I write it?

You need to explicitly state the result type:

fetch { (result: SimpleResult<YOUR_CODABLE_TYPE>) in
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM