Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

Type ‘AnyView’ cannot conform to ‘View’ on protocol with generics.

Home/ Questions/Q 518
Next
Answered
Type 'AnyView' cannot conform to 'View' on protocol with generics.
afonsotomasdafonseca
afonsotomasdafonseca Teacher

I’m currently building an iOS application using SwiftUI, and I’m running into issues when trying to conform to a protocol with generics. I have a protocol called `ListViewProtocol` that has an associated type `DataType` and requires a function called `setup(withData data: DataType)`. I then have a struct called `MyListView` that conforms to this protocol, and I’ve defined `DataType` as `String`.

However, when I try to create a view that uses `MyListView` and passes in an array of strings, I get the error message “Type ‘AnyView’ cannot conform to ‘View’ on protocol ‘ListViewProtocol'”. This error is occurring on the line `return AnyView(MyListView(data: myStringArray))`. I’m not sure why this error is being thrown, as I believe I’m conforming to the `ListViewProtocol` correctly.

Here is some example code to illustrate the issue:

“`swift
protocol ListViewProtocol {
associatedtype DataType
func setup(withData data: DataType)
}

struct MyListView: ListViewProtocol {
typealias DataType = String

func setup(withData data: DataType) {
// do something with data
}
}

struct ContentView: View {
let myStringArray = [“String 1”, “String 2”, “String 3”]

var body: some View {
return AnyView(MyListView(data: myStringArray))
}
}
“`

Any help with this issue would be greatly appreciated. Let me know if you need any more information or code snippets to better understand the problem.

generic-typesprotocolsswifttype-constraintsviews
  • 193
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    baronus9125 Teacher
    2017-08-23T01:13:22+00:00Added an answer about 6 years ago

    Hello there! I see that you are having trouble with the error message “Type ‘Any’ has no subscript members.” Let me help you out with that.

    Firstly, it is important to understand what this error message means. The error message is telling you that you are trying to use the subscript operator (the square brackets) on a type ‘Any’, which does not have any subscript members. Essentially, ‘Any’ is a type that Swift uses when it cannot determine the type of a value at compile time. It is a very general type, and therefore, it does not have any specific subscript members.

    Now, to resolve this error message, you will need to cast the value to a more specific type so that Swift can determine its subscript members. One approach would be to use optional binding to safely unwrap the value and then cast it to the correct type. Let me show you an example:

    “`
    if let myValue = myAnyValue as? [String: Any] {
    // You can now safely use the subscript operator on myValue
    let myString = myValue[“myKey”] as? String
    }
    “`

    In this example, we are first checking if myAnyValue can be cast to a dictionary with keys of type String and values of type Any. If the cast is successful, we then use the subscript operator to access the value associated with the key “myKey”.

    It’s also worth noting that it’s generally a good practice to avoid using ‘Any’ when possible, as it can make your code more error-prone. Whenever possible, try to use more specific types so that your code is better defined and easier to read.

    I hope this helps you resolve your error message! Let me know if you have any further questions.

    • 161
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. ficoni93 Begginer
    2017-08-29T20:33:30+00:00Added an answer about 6 years ago

    Sure, one possible solution to your problem could be to add an associatedtype to your protocol with a default value of View. For example:

    “`
    protocol MyProtocol {
    associatedtype MyView: View = View
    var myView: MyView { get }
    }
    “`

    This way, you can still use the protocol with any type that conforms to the View protocol, but if you don’t specify a custom view type, it defaults to View.

    I hope this helps! Let me know if you have any further questions.

    • 50
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. zeusso_98 Teacher
    2017-08-30T17:41:02+00:00Added an answer about 6 years ago

    One possible solution to this problem of type `AnyView` not conforming to a protocol with generics is to explicitly cast the view to the expected type before passing it as a parameter. For example, if you have a generic function that expects a `View` parameter, you can cast the `AnyView` to the expected type using the `as?` operator and then pass it to the function:

    “`swift
    func myFunction(view: V) {
    // Do something with the view
    }

    let myAnyView: AnyView = AnyView(Text(“Hello, World!”))
    if let myTextView = myAnyView as? Text {
    // Now we have the Text view, so we can call our function with it
    myFunction(view: myTextView)
    }
    “`

    This way, we ensure that the `AnyView` instance is converted to the expected type before being passed to the function. However, it’s important to note that this solution only works if we know the expected type beforehand. If we don’t know the type of the view, we might have to resort to other solutions such as using type erasure or modifying our code to avoid using `AnyView` altogether.

    • 17
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.