Trying to understand why my Xcode-beta 2 is not able to understand the underlying type for the following example
/// Protocol that has the idea of draw
protocol Drawable {
func draw()
}
/// Implementation of a circle which is drawable
struct Circle: Drawable {
func draw() {
print("Circle")
}
}
/// Implementation of a Triangle which is drawable
struct Triangle: Drawable {
func draw() {
print("Triangle")
}
}
/// Let make an instance as shown in the example in the `Embrace Swift Generics` Talk
func underlyingTypeDoesNotChangeWithinTheScop() {
var drawable: some Drawable = Circle()
drawable.draw()
drawable = Circle() /// 👈🏽 This line should compile, since the underlying type does not change within the scope
drawable.draw()
}
Why am I getting the following error cannot assign value of type 'Circle' to type 'some Drawable' when I try to assign Circle to var drawable, from what I understand the underlying type remains the same within the scope.
Is this an Xcode14-beta 2 issue?
Post not yet marked as solved
Using Swift 5.7 we're trying to use protocols for testing and mocking in our SwiftUI App.
With any and some we're able to hold a heterogeneous list of protocols with self constraints which works perfectly. What we're running into now is that we can't undox the any Protocol into a concrete type for the view.
Here's a basic example:
protocol ItemProtocol: ObservableObject {
var id: String { get }
}
struct ListSection {
var id: Int
let title: String
let items: [any ItemProtocol]
}
protocol ViewModelProtocol: ObservableObject {
var sections: [ListSection] { get }
}
struct MyView<T: ViewModelProtocol>: View {
@ObservedObject
var viewModel: T
init(viewModel: T) {
self.viewModel = viewModel
}
var body: some View {
List(viewModel.sections, id: \.id) { section in
Section {
ForEach(section.items, id: \.id) { item in
RowView(item: item)
// create view for some ItemProtocol
Text("Hello Item")
}
} header: {
Text(section.title)
}
}
}
}
struct RowView<T: ItemProtocol>: View {
@ObservedObject
var item: T
init(item: T) {
self.item = item
}
var body: some View {
Text("Row View")
}
}
This will result in an error:
Type 'any ItemProtocol' cannot conform to 'ItemProtocol'
I had hoped that the any ItemProtocol would be unboxed to it's concrete type and a concrete type of View would be created.
Post not yet marked as solved
I've copied the code as the video was coming along, and getting this error:
error: type 'any Animal' cannot conform to 'Animal'
note: only concrete types such as structs, enums and classes can conform to protocols
note: required by instance method 'feed' where 'some Animal' = 'any Animal'
is this functionality not yet available in Xcode 14b1?
Post not yet marked as solved
Sorry, I did not have a better title for this, I hope it gets clearer with code.
The idea is to build a simple facade for Persistent Storage of some objects:
class PersistantStorage<T, Codable, Identifiable> {
func store(_ object: T) throws { }
func objectFor(_ key: T.ID) -> T? {
return nil
}
}
As apparent, there is the generic type T, which is constrained to Codable and Identifiable. Now I want to use the later constraint to define my objectFor method, but the compiler complains:
'ID' is not a member type of type 'T'
How would I do this? Or is this completely the wrong approach?
Thanks
Alex