TIL

[iOS] 2024.04.14 TIL

ioskkt 2025. 4. 14. 22:29
  • 에러 타이틀까지 미리 정해주면 파라미터로 쉽게 접근할 수 있음.
enum CustomError: Error {
        case emptyInput // 패스워드 없을 때
        case tooLongInput // 8자리 넘었을 때
        case wrongInput // 틀렸을 때
        
        var errorTitle: String {
            switch self {
            case .emptyInput:
                return "비밀번호를 입력해주세요"
            case .tooLongInput:
                return "비밀번호는 8자리 이하입니다."
            case .wrongInput:
                return "비밀번호가 틀렸습니다."
            }
        }
    }

 


  • 첫 글자 대문자 풀기 코드 -> autocapitalizationType
textField.autocapitalizationType = .none

  • 타입 컨버전 -> 형변환
  • 타입 캐스팅 -> 타입변환

  • do try catch
func validate(with input: String) throws -> Bool {
    let inputCount = input.count
    if inputCount > 8 {
        throw CustomError.tooLongInput // 8자리 넘었을 때 던지기.
    } else if inputCount == 0 {
        throw CustomError.emptyInput // 패스워드 없을 때 던지기.
    } else {
        return try passwordCheck(with: input)
    }       // 여기서 try는 passwordCheck 메서드를 호출할 때 쓰임.
}

private func passwordCheck(with input: String) throws -> Bool {
    if input == "password" {
        return true // 맞을 때 Bool 값 true 반환
    } else {
        throw CustomError.wrongInput // 패스워드 틀릴 때 던지기.
    }
}

throws :

에러를 던지는 함수를 정의하는 부분

 

throw :

던져질 에러

 

try :

throw 함수를 호출 할 때, 호출하기만 할때는 do - catch로 감싸주지 않아도 됨.

 

do - catch 구문 :

에러처리하는 부분, 에러처리하는 부분에 try가 있다면 감싸줌.


 

 

 

becomeFirstResponder

resignFirstResponder

은 공부를 더해봐야함.

 

 

 

 

'TIL' 카테고리의 다른 글

2025.05.19 트러블 슈팅(RxSwift 개인과제)  (0) 2025.05.19
[iOS]2025.05.12 RxSwift  (0) 2025.05.12
[iOS]2025.05.08 URLSession, Alamofire  (0) 2025.05.08
[iOS]2025.04.17 TIL  (0) 2025.04.17
[iOS]2025.04.15 TIL  (1) 2025.04.15