Swift 3 Nscache Generic Parameter 'Keytype' Could Not Be Inferred

Swift 3 NSCache Generic parameter 'KeyType' could not be inferred

  • In the first Swift 3 betas NSCache has been changed to Cache.
  • In the latest betas (currently 5) it has been reverted to NSCache.

Anyway NSCache is now a generic.

public class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject { ...

so the most general syntax is

private var dataCache = NSCache<AnyObject, AnyObject>()

The explicit init() is not needed (not even in Swift 2)

Initializing a Struct in Swift Error: Generic parameter could not be inferred

For a generic type, the generic parameter is actually considered to be part of the type. So the type of your struct isn't MyStruct, it's MyStruct<Int> (or whatever generic type you use. Therefore, you can't just declare the type by itself if there's not any information for the compiler to use to infer the actual type. Instead, you have to include the generic type in your declaration:

var myStruct: MyStruct<Int>?

Generic type 'Result' specialized with too few type parameters (got 1, but expected 2)

You can qualify the reference to Result in order to choose the one you want. The version with one parameter belongs to Alamofire. The one with two parameters belongs to Swift.

typealias Response<T> = (_ result: Alamofire.Result<T>) -> Void

... or ...

static func upload(
data: Data,
completion: @escaping (Swift.Result<Int, Error>) -> Void
)


Related Topics



Leave a reply



Submit