enumerated()
를 이용하면 각 요소의 index
를 이용할 수 있다.
버튼들을 []
araay에 담아두고, enumerated()
를 통해 index, item
을 사용한다.
이 때, 버튼 자체에 대한 속성을 설정할 때에는 item
을, index를 사용할 때에는 index
를 이용한다.
아래와 같이 item.tag = index
를 사용하면, 한 번에 태그와 속성을 정해줄 수 있다.
[tasteStar00, tasteStar01, tasteStar02, tasteStar03, tasteStar04].enumerated().forEach { (index, item) in
item.tag = index
item.setImage(UIImage(systemName: "star"), for: .normal)
item.tintColor = .yellow
}
🔖 참고