DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

2 1

Day 53 of 100 Days of SwiftUI

I just completed day 53 of 100 days of SwiftUI. Today I learnt about type erasure using the AnyView class and also how to save data using CoreData.

We all know you are only allowed to return one kind of view in your SwiftUI function. Sometimes, you need to return multiple kinds of views. That's where AnyView comes in. It allows you to return multiple kinds of views all you have to do is wrap your view with an AnyView. For example, you can use an AnyView like so.

let switch = 3
var body: some View {
    if switch > 3 {
     return AnyView(TextView("View 1))
    } else {
     return AnyView(TextField("View 2"))
    }
}
Enter fullscreen mode Exit fullscreen mode

The code above returns two different kinds of views depending on the value of the switch. You should use AnyView sparingly because they reduce the performance of your app. Since SwiftUi no longer knows what kind of views your functions are returning. It won't be able to make any optimisations.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools