23.12.15 TIL SwiftUI 13. How to use ForEach loops

Hay·2023년 12월 15일
0

SwiftUI_Beginner

목록 보기
9/19

How to use ForEach loops

ForEach loop을 이용해서 같은 것을 여러번 반복해서 타이핑하지 않고도 출력하는 방법

import SwiftUI

//we don't wanna rewrite the same text or code like 100 times -> ForEach 사용

struct ForEachBootcamp: View {
    
    //create a data set
    let data: [String] = ["Hi", "Hello", "Hey everyone"] //an empty array
    let myString: String = "Hello"
    
    
    var body: some View {

        //예시 2
        VStack {
            ForEach(data.indices) { index in
                Text("\(data[index]): \(index)")
                
            }
        }
        
//--------------------------------------------------------

        //예시 1
        VStack {
            //each of this loop has an index and index is just the loop number that it's on.
            ForEach(0..<10) { index in
                HStack{
                    Circle()
                        .frame(width: 30, height: )
                    Text("Index is: \(index)")
                }
  }
                
}


#Preview {
    ForEachBootcamp()
}

0개의 댓글