BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

SwiftUI, Core Data : How to display more than ten attributes of Entity in List

Forums > SwiftUI

Hello I have a Core Data model with 18 attributes, name of Entity is Item, its attributes are attr_1, attr_2, ... , attr_18 and all of them is String type, however, the following code didn't work :

struct ContentView: View {
  @Environment(\.managedObjectContext) private var viewContext

  @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.name, ascending: true)],
        animation: .default)
  private var items: FetchedResults<Item>

  var body: some View {
      List {
          Text(items.attr_1)
          Text(items.attr_2)
          // ...
          Text(items.attr_11)  // ERROR: Extra argument in call
          // ... 
          Text(items.attr_18)
      }
  }
}

So how could I make sure every attribute could show in the List? I would really appreciate it if you could help me or give me some hints.

2      

Create a computed property on your Item entity which returns an array of your attributes. Then us a ForEach to create your List. With this approach you can show more than 10 Views.

2      

Hi @Heyya-x! I am sure there is a better solution, but quick fix can be this version ->

List {
    ForEach(items) { item in
      Group {
        Text(item.att_1)
        ...
        Text(item.att_10)
      }
      Group {
        Text(item.att_11)
        ...
        Text(item.att_18)
      }
   }
}

Basically, you are grouping up to ten views in container...

if there is no need to loop through the items so you can just put views as static by grouping them:

List {
    Group {
      Text(item.att_1)
      ...
      Text(item.att_10)
    }
    Group {
      Text(item.att_11)
      ...
      Text(item.att_18)
    }
}

2      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.