0

Howdy I have a project that only needs to display the first 5 items in the loop of over 30 items, below is my code

struct Introductions: Codable, Identifiable {
   let id: String
   let topIntros: String?
   let image: String
   let date: String
}

ForEach(introductions) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}

I tried using this method but xcode crashed crashed after i scrolled past the fifth item

ForEach(introductions, id: \.topIntros) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}

Thanks

El Tomato
  • 6,479
  • 6
  • 46
  • 75
EMPIRE
  • 57
  • 7
  • That's not how we present code here. – El Tomato Oct 05 '21 at 00:22
  • I know i am looking for the formater, no luck yet on presenting the code the right way – EMPIRE Oct 05 '21 at 00:24
  • got it finally Ctrl K – EMPIRE Oct 05 '21 at 00:26
  • var body: some View { List { ForEach(introductions.startIndex..<5) { index in let introduction = introductions[index] ... } } } – El Tomato Oct 05 '21 at 00:37
  • Thanks for your reply but i'ld rather prefer using a LazyVStack in this case and i didnt understand your message completely – EMPIRE Oct 05 '21 at 00:43
  • You can add LazyVTack or whatever you want to your code for yourself, can't you? struct ContentView: View { var body: some View { ScrollView { LazyVStack { ForEach(introductions.startIndex..<5) { index in let introduction = introductions[index] Text(introduction.topIntros) } } } } } – El Tomato Oct 05 '21 at 01:13
  • @ElTomato: Why you are insisting `introductions.startIndex`? It would be always `0` so why not just `0`? Apple: `For an instance of Array, startIndex is always zero.` Since ForEach take just Array or Range! and using Set or dic are odd! – ios coder Oct 05 '21 at 01:24

3 Answers3

0

Here is a simple way for you:

struct ContentView: View {
    
    @State private var array: [Int] = Array(100...130)

    var body: some View {
        
        if (array.count >= 5) {
            
            ForEach(0...4, id:\.self) { index in
                
                Text(String(describing: array[index]))
      
            }
            
        }

    }
    
}

Here another way for you:

struct ContentView: View {
    
    @State private var array: [Int] = Array(100...130)

    var body: some View {
        
        ForEach(array.dropLast(array.count - 5), id:\.self) { item in
            
            Text(String(describing: item))

        }

    }
    
}
ios coder
  • 1
  • 4
  • 31
  • 91
0

you could try something like this:

if (introductions.count >= 5) {
    ForEach(introductions.prefix(upTo: 5), id:\.id) { introduction in
       NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
          IntroductionsView(introduction: introduction)
       }
    }
}
0

Using @workingdog 's comment i was able to fix it and posting the answer below in case it comes in handy.

struct Introductions: Codable, Identifiable {
   let id: String
   let topIntros: String?
   let image: String
   let date: String
}

ForEach(introductions) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}


struct ContentView: View {

    var body: some View {
        VStack { 
if (introductions.count >= 5) {
    ForEach(introductions.prefix(upTo: 5), id:\.topIntros) { introduction in
       NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
          IntroductionsView(introduction: introduction)
       }
    }
} else {
ForEach(introductions) { introduction in
   NavigationLink(destination: IntroductionDetailView(introduction: introduction)) {
      IntroductionsView(introduction: introduction)
   }
}
}
}

    }
    
}

using .id broke the view hierarchy so i used the optional .topIntros and voila every turned out fine.

EMPIRE
  • 57
  • 7