GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.