<unknown>:0: error: type 'Key' constrained to non-protocol type 'String' - swift

Migrating to Swift 3 from 2.3 and am running into this issue. The error is traceable to a view controller.
I do not see any extensions/protocols which would require a 'Key' constrained to 'String'.
I've tried to comment out code that might be causing the error, and have had strange results - ie removing an empty viewDidLoad() made the error appear in another class.
I'll update the thread if I make progress.

Key is a type inside a structure maybe a struct/class like Dictionary.
Use AnyHashable as Key to replace String if in Dictionary.

Related

How to resolve expected declaration error in swift?

I am new to swift and am trying to build an app. I am receiving "Expected Declaration" error on Xcode when I type the following code.
.edgesIgnoringSafeArea(.bottom)
Please guide me on how to resolve it. Thanks
A declaration is one of the things listed in https://docs.swift.org/swift-book/ReferenceManual/Declarations.html
If the compiler is saying it's expecting a declaration, it means you have written something that isn't a declaration in some place in the code where only a declaration is allowed.
In your case .edgesIgnoringSafeArea is a method on SwiftUI View type, so it would only be valid to put that right after a SwiftUI view, not on its own. It might be because you have brackets in the wrong place.

Can not satisfy generic constraint with subclass?

I have following compiler error:
Cannot convert value of type ‘PostTagViewModel<CreatePostMediaViewModel>’ to expected argument type ‘PostTagViewModel<PostMediaViewModel>’
CreatePostMediaViewModel is subclass of PostMediaViewModel, so on the first glance it seemed like this should work out of the box, but sadly it does not?
EDIT:
Some addtional info, swift array is generic and I can pass [Subclass] where parameter is declared [Baseclass]

Difference between wrapping a value and explicitly declaring value as a type in Swift

I came across a heterogeneous dictionary definition like this on a tutorial online:
var mixedMap4 = [AnyHashable(0): "Zero" as Any,
AnyHashable(1): 1.0 as Any,
AnyHashable("pi"): 3.14 as Any]
I was wondering why the author chose to write
AnyHashable(0) instead of 0 as AnyHashable. When I tried this on Swift playground, it also worked. However when I turned "Zero" as Any into Any(0) it gives the following
error: error: The Dictionary.xcplaygroundpage:41:34: error: protocol
type 'Any' cannot be instantiated var mixedMap4 = [AnyHashable(0):
Any("Zero") ,
Thank you for the answer
The clue is in the error message.
AnyHashable is a struct that type-erases the underlying hashable type, and so can be directly instantiated as an object
Any is a protocol and therefore can't be directly instantiated, although all other types can be complied with it, thus a String such as "Zero" can be cast as Any but Any(String) is meaningless.
To me it all just feels like a bucket load of trouble waiting to happen!

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")
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"]

Cannot pass dictionary to function in swift

Tearing my hair out on this one.
I'm trying to pass a dictionary of type [String:UIView] to a function in swift that's expecting a [NSObject:AnyObject] :
NOTE: I get the exact same error if I use NSString instead of String:
Any ideas what I'm doing wrong?
opponentImageView is a UIImageView....
PROBLEM RESOLVED
turns out the issue was actually with the 'options' argument being passed 0. Passing NSLayoutFormatOptions(0) made this misleading error go away. Here is what the code now looks like:
It builds fine now...
There is some problem when you try to cast String as NSObject implicitly in pure swift class. You need to define it explicitly
let viewsDict:[NSObject:AnyObject] = ["yourview":view]
and there is one error more options can not be 0.So define options as proper NSLayoutFormatOptions type.