공식문서

    [Swift] Swift 공식문서 정리 - 5(옵셔널 체이닝)

    옵셔널이란? 옵셔널이란 nil을 사용할 수 있는 Type을 옵셔널 타입이라고 부르며, nil을 가질 수 있는 값은 옵셔널 타입이다. 옵셔널 체이닝(Optional Chaining) class Person { var residence: Residence? } if let roomCount = john.residence?.numberOfRooms { print("John's residence has \(roomCount) room(s).") } else { print("Unable to retrieve the number of rooms.") } // Prints "Unable to retrieve the number of rooms." 강제 언래핑(Forced Wrapping) var name: Strin..

    [Swift] Swift 공식문서 정리 - 4(열거형)

    열거형이란 ? - 같은 주제로 연관된 데이터들을 멤버로 구성하여 나타내는 자료형 열거형의 선언 enum CompassPoint { case north case south case east case west } var directionToHead = CompassPoint.west // 이후엔 생략 가능 directionToHead = .east Switch 구문에서 열거형의 사용 directionToHead = .south switch directionToHead { case .north: print("Lots of planets have a north") case .south: print("Watch out for penguins") case .east: print("Where the sun rises..

    [Swift] Swift 공식문서 정리 - 3(클래스 & 구조체)

    클래스와 구조체의 공통점 - 값을 정의하기 위한 프로퍼티 정의 - 기능을 정의하기 위한 메소드 정의 - subscript 정의 - 이니셜라이저 정의 - 기본 구현에서 기능 확장 - 프로토콜 제공 클래스만 가능한 기능 - 상속 : 클래스의 여러 속성을 다른 클래스에 물려 줌 - 타입 캐스팅 : 런타임에 클래스 인스턴스의 타입을 확인 - 소멸자 : 할당된 자원을 해제 시킴 - 참조 카운트 : 클래스 인스턴스에 하나 이상의 참조가 가능 선언 문법 class SomeClass { // 클래스 내용은 여기에 } struct SomeStructure { // 구조체 내용은 여기에 } 예시 struct Resolution { var width = 0 var height = 0 } class VideoMode { va..

    [Swift] Swift 공식문서 정리 - 2(제어문, 함수)

    제어문 For-In Loops 순서대로 순회하기 위해 사용 for index in 1...5 { print("\(index) times 5 is \(index * 5)") } // 1 times 5 is 5 // 2 times 5 is 10 // 3 times 5 is 15 // 4 times 5 is 20 // 5 times 5 is 25 While 조건이 거짓일 때 까지 반복 var square = 0 var diceRoll = 0 while square < finalSquare { // roll the dice diceRoll += 1 if diceRoll == 7 { diceRoll = 1 } // move by the rolled amount square += diceRoll if square ..

    [Swift] Swift 공식문서 정리 - 1(기본연산자, 문자열, 콜렉션 타입)

    [Swift] Swift 공식문서 정리 - 1(기본연산자, 문자열, 콜렉션 타입)

    연산자 nil 병합 연산자 - 형태 : a ?? b - 의미 : a가 nil(값이 없다)일 경우, b를 반환한다는 의미. let userColor = "green" var nilColor: String? var colerNilCheck = nilColor ?? userColor // nilColor가 nil 값, 즉 userColor가 들어간다. 문자열과 문자 문자열 리터럴 - 형태 : let something = "something" 여러줄 문자열 리터럴 let quotation = """ The White Rabbit put on his spectacles. "Where shall I begin, please your Majesty?" he asked. "Begin at the beginning," ..