Search code examples
swiftswift3nsobject

Expression Type 'Set<NSObject>' is ambiguous without more Context


I recently switched to Swift 3 and I got an error with the following line that I didn't get in swift 2. The layerClient call refers to the layerkit api, but the error seems to deal more with typing than the api. The error itself is "Expression Type 'Set' is ambiguous without more ".

layerClient.autodownloadMIMETypes = Set<NSObject>(arrayLiteral: "image/png")

Solution

  • I assume you're using this framework.

    You don't need the <NSObject> when creating the Set. It can figure out with type it contains by the parameter you pass to the init method. Also autodownloadMIMETypes type in swift would be Set<String> which wouldnt match Set<NSObject>. This should work.

    layerClient.autodownloadMIMETypes = Set(arrayLiteral: "image/png")
    

    Also, since Set conforms to the ExpressibleByArrayLiteral protocol, you should be able to just create it like an array.

    layerClient.autodownloadMIMETypes = ["image/png"]