UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

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.