티스토리 뷰

수업때 배운 익스텐션과 열거형을 이용해서 이전에 했던 과제 코드를 수정해보았음.

 

기존에 case만 있던 enum을 다음과 같이 수정했음. caseIterable을 채택하여 배열처럼 사용 가능하도록 함.

enum ButtonTag: Int, CaseIterable {
    
    case first, second, third, fourth, fifth, sixth, seventh, eighth, ninth
    
    var tagLabel: String {
        switch self {
        case .first:
            return "행복해 "
        case .second:
            return "사랑해 "
        case .third:
            return "좋아해 "
        case .fourth:
            return "당황해 "
        case .fifth:
            return "속상해 "
        case .sixth:
            return "우울해 "
        case .seventh:
            return "심심해 "
        case .eighth:
            return "미워해 "
        case .ninth:
            return "슬퍼해 "
        }
    }
}

caseIterable을 이용하니 인덱스로 접근할 수 있어 코드를 훨씬 짧게 수정할 수 있었음.

 

수정 전

수정 후

 

 

수정 전

수정 후

 

 

수정 전

수정 후

 

 

수정 전

수정 후

 

-> 감정 다이어리에서만 100줄 가까이 줄일 수 있었음.

 

또한 extension을 이용해 기념일 계산기에서도 반복적으로 alert를 계속 만들어주지 않게 하여 한줄로 가능하도록 수정했음.

extension UIViewController {
    
    func showAlert(alertTitle: String, alertMessage: String?, buttonTitle: String, buttonStyle: UIAlertAction.Style) {

        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)

        let ok = UIAlertAction(title: buttonTitle, style: buttonStyle, handler: nil)
        
        alert.addAction(ok)

        present(alert, animated: true, completion: nil)
    }
}
댓글
최근에 올라온 글
Total
Today
Yesterday