returning swift protocol associated type in multiple methods
The solution you came up with by playing around is exactly what you need
As mentioned elsewhere, the main issue with your first protocol is that you're enforcing createSomeView()
createAnotherView()
both return the same type. While ViewA()
and ViewB()
are both candidates for V
, since they conform to View
they are still different types, and therefore cannot BOTH be V
in a given object.
By defining both V1
and V2
, you allow for each function to return a different type, or the same type, it's all acceptable. By making both V1
and V2
require View
conformance, you allow for the some View
syntax
protocol with same associated type name
Another workaround is to create a third, combined protocol:
protocol ReadWrite {
associatedtype R
associatedtype W
func read() -> R
func write(a: W)
}
It's not pretty, since it forces you to redeclare the protocol members, but it does keep it generic (you're not limited to String and Int).
Protocol conforming to type with associated value
Because this is not a current feature of Swift. Once there is an associated type, there is always an associated type. It doesn't go away just because you constrain it. And once it has an associated type, it is not concrete.
There is no way to "inherit" protocols this way. What you mean is:
protocol MyProtocol {
var id: UUID { get }
}
And then you can attach Identifiable to structs that require it:
struct X: MyProtocol, Identifiable {
var id: UUID
}
(note that no where
clause is required.)
There is no Swift feature today that allows you to say "types that conform to X implicitly conform to Y." There is also no Swift feature today that allows for an Array of "things that conform to Identifiable with ID==UUID." (That's called a generalized existential, and it's not currently available.)
Most likely you should go back to your calling code and explore why you require this. If you post the code that iterates over test
and specifically requires the Identifiable
conformance, then we may be able to help you find a design that doesn't require that.
Distinguishing between inherited `associatedtype`s in Swift protocols
This has been discussed in Multiple protocols associatedtype name collision in the Swift forum. Xiaodi Wu writes:
It is very much possible, but the identically named associated types must be, in the conforming type, fulfilled by the same type.
In the future, there may be syntax added to allow types to conform to two protocols with such clashing requirements, but it adds great complexity in terms of implementation and is not without its own pitfalls for users (for example, it can get very confusing for end users of your type).
So a type can conform to both A
and B
with identical associated type T
, e.g.
struct Foo: A, B {
typealias T = String
}
and a protocol can inherit from both A
and B
and restrict T
to the identical type:
protocol C: A, B where T == String {
}
Conforming to both protocols with distinct associated types is currently not supported.
How to use multiple protocols in Swift with same protocol variables?
The simple answer is that you can't.
Maybe one protocol depends on another, in which case you would use the dependent protocol for the type of your delegate.
Related Topics
Swift 3 Errors with Additional Data
What Are 3 Items in a Swift Method Parameter For
Ambiguous Method Overload with Closures in Swift, But Only When Closure Returns a Value
Performing a Completion Handler Before App Launches
Nsmanagedobject Subclasses Duplicate Declaration
Label Showing Top of Screen Instead of Being on the Inputaccessoryview
Interrupted Purchase Not Calling Delegate After Accepting T&C
Randomizing Node Movement Duration
How to Group an Array of Image Urls Together in Firebase Realtime Database? (Swift)
How to Use a Specific Gmt for a Function Which Will Be Recognised by Other Time Zones
Contacts Not Recognized When Body Is Changed from Circle to Rectangle
How to Parse JSON in JSON with Swiftyjson
Swift: Skspritekit, Using Storyboards, Uiviewcontroller and Uibutton to Set in Game Parameters
Any Way to Get a Gif as a Background with Swiftui
Compare Value from Two Struct in Swift
Is There a Neat Way to Represent a Fraction as an Attributed String