SwiftUI联合收割机无法转换类型
我不明白我为什么会出错: 无法将类型为“()”的返回表达式转换为返回类型为“AnyPublisher”SwiftUI联合收割机无法转换类型,swift,swiftui,combine,Swift,Swiftui,Combine,我不明白我为什么会出错: 无法将类型为“()”的返回表达式转换为返回类型为“AnyPublisher” 如果您想从这个函数返回publisher,您必须在橡皮擦处停止,如 func fetchCommetsPublisher(endpoint: EndPoint) -> AnyPublisher<[Comments], Never> { URLSession.shared.dataTaskPublisher(for: stringUrl(with: endpoint)
如果您想从这个函数返回publisher,您必须在橡皮擦处停止,如
func fetchCommetsPublisher(endpoint: EndPoint) -> AnyPublisher<[Comments], Never> {
URLSession.shared.dataTaskPublisher(for: stringUrl(with: endpoint)!)
.map{$0.data}
.decode(type: [Comments].self, decoder: JSONDecoder())
.replaceError(with: [])
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher() // << this returns AnyPublisher<[Comments], Never>
}
func fetchCommetsPublisher(端点:端点)->AnyPublisher{
URLSession.shared.dataTaskPublisher(用于:stringUrl(带:端点)!)
.map{$0.data}
.decode(类型:[注释].self,解码器:JSONDecoder())
.replaceError(带:[])
.receive(在:DispatchQueue.main上)
.eraseToAnyPublisher()//您打算如何使用fetchCommetsPublisher
?列表,.onAppear{my class.fuction}我删除了.eraseToAnyPublisher(),现在我有了其他错误。根据Asperi,您需要删除---.assign(to:\.isFormValid,on:self)。store(in:&cancel)。需要将最后一条语句保留为.eraseToAnyPublisher()
struct Comments: Codable, Identifiable, Hashable {
var postId: Int
var id: Int
var name: String
var email: String
var body: String
}
func fetchCommetsPublisher(endpoint: EndPoint) -> AnyPublisher<[Comments], Never> {
URLSession.shared.dataTaskPublisher(for: stringUrl(with: endpoint)!)
.map{$0.data}
.decode(type: [Comments].self, decoder: JSONDecoder())
.replaceError(with: [])
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher() // << this returns AnyPublisher<[Comments], Never>
}