티스토리 뷰

새싹 프로그램에서 잭님께 받은 피드백 정리 및 코드 수정

 

 

감정 다이어리

 

 

1. 열거형을 Int로 채택한 경우 rawValue가 0부터 지정되므로 따로 선언하지 않아도 된다.

 

수정 전

enum ButtonTag: Int {
    
    case first = 0
    case second = 1
    case third = 2
    case fourth = 3
    case fifth = 4
    case sixth = 5
    case seventh = 6
    case eighth = 7
    case ninth = 8
    
}

수정 후

enum ButtonTag: Int {
    
    case first
    case second
    case third
    case fourth
    case fifth
    case sixth
    case seventh
    case eighth
    case ninth
    
}

 

 

2. 배열의 이름을 count라고 선언해서 배열이 가지고있는 프로퍼티와 헷갈릴 수 있다. 어떤 배열인지 알기 위해 조금 더 구체적인 이름이 필요하다.

 

생각해보면 배열의 원소 개수를 이용할 때가 종종 있는데, 이때 배열의 이름이 count라면 헷갈릴 수도 있을 것 같다.

 

수정 전

var count: [Int] = [UserDefaults.standard.integer(forKey: "\(ButtonTag.first.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.second.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.third.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.fourth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.fifth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.sixth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.seventh.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.eighth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.ninth.rawValue)")]

수정 후

var emotionTapCount: [Int] = [UserDefaults.standard.integer(forKey: "\(ButtonTag.first.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.second.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.third.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.fourth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.fifth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.sixth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.seventh.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.eighth.rawValue)"),
                        UserDefaults.standard.integer(forKey: "\(ButtonTag.ninth.rawValue)")]

감정 버튼을 눌렀을 때 올라가는 count이므로 이름을 더 구체적으로 바꾸었다.

 

 

3. UserDefaults.standard를 따로 클래스 내에 선언해서 사용해도 괜찮다.

 

위의 2번 코드에서도 느낄 수 있지만, 실제 코드에서 UserDefaults.standard가 굉장히 많이 반복된다. 따라서 미리 선언해서 쓰면 코드가 더 간결해질 것 같다.

 

수정 코드는 위의 코드로 보여주겠다. (실제로는 다 바꿈)

 

수정 전

firstLabel.text = "행복해 \(UserDefaults.standard.integer(forKey: "\(ButtonTag.first.rawValue)"))"

수정 후

let userDefaults = UserDefaults.standard
.
.
.
firstLabel.text = "행복해 \(userDefaults.integer(forKey: "\(ButtonTag.first.rawValue)"))"

예시로 일부분만 보여지기에 차이가 크지 않아 보이지만 코드 전체적으로 확실히 조금 더 짧아졌다.

 

 

4. setTitleText라는 함수의 switch문에서도 리터럴Int보다 열거형을 사용해보면 좋을 것 같다.

 

수정 전

func setTitleText(tag: Int) -> String {
        
        switch tag {
        case 0: return "행복해 \(emotionTapCount[tag])"
        case 1: return "사랑해 \(emotionTapCount[tag])"
        case 2: return "좋아해 \(emotionTapCount[tag])"
        case 3: return "당황해 \(emotionTapCount[tag])"
        case 4: return "속상해 \(emotionTapCount[tag])"
        case 5: return "우울해 \(emotionTapCount[tag])"
        case 6: return "심심해 \(emotionTapCount[tag])"
        case 7: return "미워해 \(emotionTapCount[tag])"
        case 8: return "슬퍼해 \(emotionTapCount[tag])"
        
        default:
            return "오류"
        }
    }

수정 후

func setTitleText(tag: Int) -> String {
        
        switch tag {
            
        case ButtonTag.first.rawValue: return "행복해 \(emotionTapCount[tag])"
        case ButtonTag.second.rawValue: return "사랑해 \(emotionTapCount[tag])"
        case ButtonTag.third.rawValue: return "좋아해 \(emotionTapCount[tag])"
        case ButtonTag.fourth.rawValue: return "당황해 \(emotionTapCount[tag])"
        case ButtonTag.fifth.rawValue: return "속상해 \(emotionTapCount[tag])"
        case ButtonTag.sixth.rawValue: return "우울해 \(emotionTapCount[tag])"
        case ButtonTag.seventh.rawValue: return "심심해 \(emotionTapCount[tag])"
        case ButtonTag.eighth.rawValue: return "미워해 \(emotionTapCount[tag])"
        case ButtonTag.ninth.rawValue: return "슬퍼해 \(emotionTapCount[tag])"
            
        default:
            return "오류"
        }
    }

 

 

5. 모든 값을 초기화하는 것이라면 전체 UserDefaults값을 한 번에 지울 수 있는 방법을 이용해봐라.

 

수정 전

userDefaults.removeObject(forKey: "\(ButtonTag.first.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.second.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.third.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.fourth.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.fifth.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.sixth.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.seventh.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.eighth.rawValue)")
userDefaults.removeObject(forKey: "\(ButtonTag.ninth.rawValue)")

수정 후

let domain = Bundle.main.bundleIdentifier!
UserDefaults.standard.removePersistentDomain(forName: domain)

print로 domain값을 확인해보니 Bundle Identifier가 나왔다. 즉, 위의 코드는 해당 Bundle Identifier를 가지는 프로젝트의 UserDefaults를 전부 지워준다.

 

 

6. 리셋함수에서 반복문을 0...8처럼 정해진 범위로 설정하는 것은 추후에 데이터가 추가될 경우에 인덱스 오류를 범하기 쉽다.

 

그래서 배열의 count를 이용해서 반복문을 수정했다.

 

수정 전

for i in 0...8 {
    emotionTapCount[i] = 0
}

수정 후

for i in 0..<emotionTapCount.count {
    emotionTapCount[i] = 0
}

 

 

기념일 계산기

 

 

수정할 내용들이 겹치는 것이 많으므로 혼자 찾아보며 수정해봄.

 

 

1. 반복문 돌릴때 배열의 count이용

 

수정 전

for i in 0...6 {
    oneHundredLabel[i].font = UIFont.systemFont(ofSize: 20, weight: .semibold)
}

수정 후

for i in 0..<oneHundredLabel.count {
    oneHundredLabel[i].font = UIFont.systemFont(ofSize: 20, weight: .semibold)
}

 

2. DateFormatter()를 선언하는 것이 반복되기때문에 클래스 내에 선언해서 사용.(아래 코드는 일부만 가져옴) 또한 필요없는 코드를 줄일 수 있을 것 같음.

 

수정 전

func dateToString(date: Date) -> String {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy년 M월 d일"
        
    let result = dateFormatter.string(from: date)
        
    return result
}

수정 후

func dateToString(date: Date) -> String {

    dateFormatter.dateFormat = "yyyy년 M월 d일"
        
    return dateFormatter.string(from: date)
}
댓글
최근에 올라온 글
Total
Today
Yesterday