TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Get String from Text view content

Forums > SwiftUI

In one of my generics views, I define a "Content" parameter as View to be passed, as I need to present on this generics view row content from a FetchRequest in Core Data. For a swipe actions of that ForEach, I need to get the string passed from that view:

struct MyView<Object: NSManagedObject, Content: View>: View {
  var items: FetchedResults<Object>
  var rowContent: (Object) -> Content

  init(items: FetchedResults<Object>, rowContent: @escaping (Object) -> Content) {
    self.items = items
    self.rowContent = rowContent
  }

  @State private var editingItemName = ""

  var body: some View {
    ForEach(items, id: \.self) { item in
        rowContent(item)

        .swipeActions(edge: .leading) {
           Button {
              editingItemName = rowContent(item)  
              //Here I can't get just string value. Just something like "Text(storage: SwiftUI.Text.Storage.verbatim("text_I_need_to_get"), modifiers: [])"

Which is called from another view with:

struct ParentView: View {
@FetchRequest(sortDescriptors: []) private var items: FetchedResults<MyEntity>

var body: some View {
    Form {
      NavigationLink(destination: MyView(items: instruments, rowContent: { item in
          Text(item.name ?? "")
          })) {
            Label("Items", systemImage: "star")
          }
          //More code here

rowContent(item) in the ForEach works perfectly, but how could I grab just the string "text_I_need_to_get"?

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.