본문 바로가기
YAGOM CAREER STARTER

[TIL] 20221229: H.I.G, Extension, Enum

by Rhode 2022. 12. 29.

본문은 야곰 아카데미 커리어 스타터 캠프를 통해 학습한 내용을 회고한 글입니다.

 


Human Interface Guidelines(H.I.G)

모달리티 Modality

모달은 화면 전환의 한 방식이다. 네비게이션 인터페이스와는 달리 기존 화면과 연속성이 없을 때 사용한다. 모달로 나타낸 화면은 반드시 어떠한 선택을 해야 사라진다는 특징을 가지고 있다.

 

 

Extension

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you don’t have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions don’t have names.)
Extension은 새로운 기능을 이미 존재하는 클래스, 구조체, 열거형, 혹은 프로토콜 타입에 더해줄 수 있다. 이것은  원본 소스 코드에 접근성이 없는 타입들을 확장하는 능력을 포함한다.

swift에서의 Extension은 다음과 같은 일을 할 수 있다:

  • 연산 인스턴스 프로퍼티와 연산 타입 프로퍼티를 더해줌
  • 인스턴스 메서드와 타입 메서드를 정의해줌
  • 새로운 initializer들을 제공해줌
  • 서브스크립트를 정의함
  • 새로운 중첩된 타입(nested types)을 정의해줌
  • 현존하는 타입을 프로토콜에 순응하게 만듦

다음과 같은 형태로 쓴다.

extension 확장할클래스나구조체 {
  //새로운 기능들이 여기에 추가 됨
}

여러개의 프로토콜을 적용하며 쓸 수도 있다.

extension 확장할클래스나구조체: 프로토콜원, 프로토콜투 {
    // 프로토콜의 구현 요구사항이 여기에 들어간다
}

 

 

여러 raw value를 갖는 열거형 Enum with multiple raw values

코드를 짜다가 막혀서 여러 raw value를 갖는 열거형을 만드는 방법들에 대해서 공부하게 되었다. Thank you, 혜모리, 무리, kaki!

방법 1: 함수 활용 방법

enum InformationType {
    case age
    case weight
}

enum PersonalInformation: Int {
    case rhode, hodoo
    
    static func convertNumberToAnimals(_ number: Int, informationType: InformationType) -> PersonalInformation? {
        if informationType == InformationType.age {
            switch number {
            case 27:
                return .rhode
            case 5:
                return .hodoo
            default:
                return nil
            }
        } else {
            switch number {
            case 46:
                return .rhode
            case 6:
                return .hodoo
            default:
                return nil
            }
        }
    }
}
if let twentySeven = PersonalInformation.convertNumberToAnimals(27, informationType: .age) {
    print(twentySeven)
}  //rhode
if let six = PersonalInformation.convertNumberToAnimals(6, informationType: .weight) {
    print(six)
}  //hodoo

방법 2: Initializer 활용 방법

enum PersonalInformations {
    case rhode
    case hodoo
    
    init?(age: Int) {
        switch age {
        case 27:
            self = .rhode
        case 5:
            self = .hodoo
        default:
            return nil
        }
    }
    
    init?(weight: Int) {
        switch weight {
        case 46:
            self = .rhode
        case 6:
            self = .hodoo
        default:
            return nil
        }
    }
}
if let five = PersonalInformations(age: 5) {
    print(five)
}  //hodoo
if let fortySix = PersonalInformations(weight: 46) {
    print(fortySix)
}  //rhode

 

 

 

참조

https://developer.apple.com/design/human-interface-guidelines/platforms/designing-for-ios/

 

Designing for iOS - Platforms - Human Interface Guidelines - Design - Apple Developer

Designing for iOS People depend on their iPhone to help them stay connected, play games, view media, accomplish tasks, and track personal data in any location and while on the go. As you begin designing your app or game for iOS, start by understanding the

developer.apple.com

http://www.yes24.com/Product/Goods/78907450

 

스위프트 프로그래밍 - YES24

문법을 넘어 프로그래밍 패러다임도 익히는 스위프트 5스위프트 5의 핵심 키워드는 ‘안정화’다. ABI 안정화 덕분에 버전과 환경에 크게 영향받지 않고 더 유연하게 스위프트를 사용할 수 있게

www.yes24.com