
마일스톤을 자세히 작성해달라고 요청하셔서, 아래와 같이 작성했다.
명노훈 :
UILabel 사용하여 장바구니 표시CollectionView 사용하여, 기본 틀 만들기Auto Layout을 이용해 위치 및 크기를 지정
오늘은 장바구니의 대략적인 UI를 CollectionView 로 구성하는 것 까지가 목표다!

CollectionView 를 사용하여 구현하기로 했다.// MARK: - 컬렉션 뷰 인스턴스 정의
lazy var collectionView: UICollectionView = {
// 1. 셀의 배치를 정해주는 레이아웃 객체 생성
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical // 세로 방향 스크롤
layout.minimumLineSpacing = 0 // 셀 사이의 간격 (줄 간격)
layout.itemSize = CGSize(width: 351, height: 40) // 셀 크기 설정
// 2. 컬렉션 뷰 생성
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .white
// 3. 델리게이트, 데이터소스 연결
collectionView.delegate = self
collectionView.dataSource = self
collectionView.layer.cornerRadius = 20
collectionView.translatesAutoresizingMaskIntoConstraints = false // 오토레이아웃 설정
// 4. 커스텀 셀 등록
collectionView.register(CartCollectionViewCell.self, forCellWithReuseIdentifier: "CartCell")
return collectionView
}()
// MARK: - 컬렉션 뷰 오토 레이아웃
func setCollectionView() {
view.addSubview(collectionView)
// 오토레이아웃 설정
NSLayoutConstraint.activate([
collectionView.widthAnchor.constraint(equalToConstant: 351),
collectionView.heightAnchor.constraint(equalToConstant: 130),
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 520),
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 12)
])
}
CartCollectionViewCell 이라는 파일을 만들었다.import UIKit
class CartCollectionViewCell: UICollectionViewCell {
// MARK: - Lifecycle
override init(frame: CGRect) {
super.init(frame: frame)
// 셀 디자인
contentView.backgroundColor = .white
contentView.clipsToBounds = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

3x 마을 사람들은 3을 저주의 숫자라고 생각하기 때문에 3의 배수와 숫자 3을 사용하지 않습니다. 3x 마을 사람들의 숫자는 다음과 같습니다.
10진법 3x 마을에서 쓰는 숫자 10진법 3x 마을에서 쓰는 숫자 1 1 6 8 2 2 7 10 3 4 8 11 4 5 9 14 5 7 10 16 정수
n이 매개변수로 주어질 때,n을 3x 마을에서 사용하는 숫자로 바꿔return하도록solution함수를 완성해주세요.
1 ≤
n≤ 100
import Foundation
func solution(_ n:Int) -> Int {
var result = n
let array = Array(String(result))
let mod = result % 3
while array.contains("3") || result % 3 == 0 {
result += 1
}
}
print(solution(15)) // 25
result 값이 올라가, 변수 array와 mod의 값도 변할줄 알았다. import Foundation
func solution(_ n:Int) -> Int {
var num = 1
var threeXTown = 1
while num < n + 1 {
if Array(String(threeXTown)).contains("3") || threeXTown % 3 == 0 {
threeXTown += 1
}
threeXTown += 1
num += 1
}
return threeXTown
}
print(solution(15)) // 25
디버깅 결과 새로운 사실을 알았다.
12는3의 배수13은3이 포함된 숫자..
연속으로 건너뛰어야는 숫자가 나와서 답이 이상하게 나온것이다!!!
🔥 조건문을 수정하자!!!
import Foundation
func solution(_ n:Int) -> Int {
var num = 1
var threeXTown = 1
while num < n + 1 {
while Array(String(threeXTown)).contains("3") || threeXTown % 3 == 0 {
threeXTown += 1
}
threeXTown += 1
num += 1
}
return threeXTown
}
print(solution(15)) // 25
if 문을 while문으로 변경했다. 연속해서 건너뛰어야 하는 숫자가 나와도 계속 건너 뛸수있게.import Foundation
func solution(_ n:Int) -> Int {
//guard n >= 1 && n <= 100 else { return }
var num = 1
var threeXTown = 1
while num < n + 1 {
while Array(String(threeXTown)).contains("3") || threeXTown % 3 == 0 {
threeXTown += 1
}
print("10진수 : \(result), 3x마을 : \(threeXTown)")
if num == n {
break
}
threeXTown += 1
num += 1
}
return threeXTown
}
num 변수가 n 과 같아지면 반복문에서 탈출하도록 바꿔주었다.
CollectionViewController 를 사용해보느라 고생을 좀 했지만, 그래도 배우는 것이 즐거웠다!!