Swift Combine: Unable to infer complex closure return type error
Do what it asks you to do; tell it what type the closure is.
let sub2 = generateIn()
.map { (value:Int) -> Int in
print(value)
return Int(value)
}
Swift is unable to type infer variable containing closure that returns Bool
Swift can currently infer only single-return closures. You can rewrite your closure to:
let downloadNextPagination = {
return current.integerValue < amount.integerValue && current.integerValue != amount.integerValue - 1
}
Add explicit type to flatmap
You didn't provide a whole lot of information about the classes involved so I mocked something up to create the playground example below.
The bit that I have added is in the closure of the flatmap
operator. Basically I told the compiler what type that closure will return using the fragment input -> Future<[String], Error>
. To come up with that line I had to know the return type of StorageManager.shared.saveAll(items: input)
so I used the value my mocked up sample returns.
Your return value may be different.
import Foundation
import Combine
class StorageManager {
static let shared = StorageManager()
init() {
}
func saveAll(items: [String]) -> Future<[String], Error> {
Future<[String], Error> { fulfill in
fulfill(.success(items))
}
}
}
struct NetworkManager {
static func getAll(path: String) -> Future<[String], Error> {
Future<[String], Error> { fulfill in
fulfill(.success(["Duck", "Cow", "Chicken"]))
}
}
}
let path : String = "some path"
let publisher : AnyPublisher<[String], Error> =
NetworkManager.getAll(path: path)
.flatMap { input -> Future<[String], Error> in
if path == "menus" {
print("It's a menu")
}
return StorageManager.shared.saveAll(items: input)
}
.eraseToAnyPublisher()
Alamofire Unable to infer closure type in the current context
func log() -> Self {
let responseSerializer = DataRequest.jsonResponseSerializer()
return response(responseSerializer: responseSerializer) { [weak self] response in
guard let _self = self else { return }
_self.printRequestString(response)
}
}
Works well.
Related Topics
Cannot Invoke 'Join' with an Argument List of Type (String, [String]) in Swift 2.0
Getting String Name of Objective-C @Objc Enum Value in Swift
Find Nearest Smaller Number in Array
Custom Swift Encoder/Decoder for the Strings Resource Format
Exponentiation Operator in Swift
How to Open a Nspopover at a Distance from the System Bar
Swift/Uiview/Drawrect - How to Get Drawrect to Update When Required
Scene Size in Xcode 6/Spritekit/Swift
Osx Application: How to Make Window Maximized
Which Format File for 3D Model Scenekit/Arkit Better to Use
In Swift, How to Get Memory Back to Normal After an Skscene Is Removed
Swift: How to Call Cckeyderivationpbkdf from Swift
Differencebetween Swift 2.0 Do-Try-Catch and Regular Java/C#/C++ Exceptions
How to Set Top Left and Right Corner Radius with Desired Drop Shadow in Uitabbar