목록Swift/Swift 기초문법 (75)
HIT해

Any란?Any는 모든 타입을 나타내는 Swift의 특별한 타입.Int, String, Array와 같은기본 타입 뿐만 아니라 사용자 정의 타입, 클래스, 구조체, 열거형, 클로저 등모든 타입을 변수나 상수에 담을 수 있다. 사용예시var anyValue: Any = 42anyValue = "Hello"anyValue = [1, 2, 3] 타입이 미리 정해지지 않았을 떄 Any를 사용할 수 있다.Any를 사용하면 콜렉션에서 여러 타입을 다룰 때 즉 배열이나 딕셔너리에서 다양한 타입의 요소를 저장해야할 때 유용하다let mixedArray: [Any] = [1, "Hello", true, 3.14] 주의사항Any로 저장된 값은 구체적인 타입 정보를 잃게 되기에 값을 사용하려면 다운 캐스팅이 필요하다타입 안..

Set의 다양한 내장함수를 알아보자!union ( 합집합 )intersection ( 교집합 )symmetricDifference ( 차집합 ) 사용예시1. union중복을 제외하고 합친다import UIKitlet myFriends = ["철수", "영희", "수잔", "제시카"]let yourFriends = ["철수", "영희", "제니퍼", "존시나"]let myFriendsSet = Set(myFriends)let yourFriendsSet = Set(yourFriends)let totalFriends = myFriendsSet.union(yourFriends) 실행결과 2. intersection중복만을 가져온다import UIKitlet myFriends = ["철수", "영희", "수잔",..

Swift Dictionary 를 반복하는 방법을 알아보자.for inforeacckeys, values 사용예시1. for inlet myFriends : [String: Int] = ["철수" : 19, "수잔": 23, "제임스": 30]for (name, age) in myFriends { print("이름: \(name), 나이: \(age)")} 2. forEachmyFriends.forEach { (name: String, age: Int) in print("이름: \(name), 나이: \(age)")} 3. keys, valuesmyFriends.keys.forEach { name in print("이름: \(name)")}myFriends.values.forEach..
stride란?범위안에서 반복하는 것 stride tofrom에서 to가 되기 전까지!// 범위안에서 반복처리for countdown in stride(from: 5, to: 0, by: -1){ print("카운트 다운 ", countdown)} 결과카운트 다운 5카운트 다운 4카운트 다운 3카운트 다운 2카운트 다운 1 to가 0이기에 0이전인 1까지만 반복된다. stride throughfrom에서 through까지!for countdown in stride(from: 5, through: 0, by: -1){ print("카운트 다운 ", countdown)} 실행결과카운트 다운 5카운트 다운 4카운트 다운 3카운트 다운 2카운트 다운 1카운트 다운 0 strid..

isSubset내가 속해 있는지 체크isSuperset내가 포함하고 있는지 체크 사용해보자!let totalFriends = ["철수", "영희", "수잔", "제임스"]let foreignFriends = ["수잔", "제임스"]let koreanFriends = ["철수", "영희", "상철"] 이런 배열이 있다고 가정했을때 어떤 배열이 어디에 속할 수 있는지 체크할 수 있다.이때 Array로는 불가능하기에 Set으로 변환해서 알아볼 수 있다. let totalFriendsSet = Set(totalFriends)let foreignFriendsSet = Set(foreignFriends)let koreanFriendsSet = Set(koreanFriends) A.isSubset(of : B) ..

protocol Pet { func doCry() }class Cat : Pet { } 이렇게 구현하면 doCry가 정의되어 있지 않기때문애 Cat Class에서 반드시 정의해주어야한다. 이를 어떻게 해결 할 수 있는가. 1. extension을 통한 옵셔널protocol Pet { func doCry()}extension Pet { func doCry(){}}class Cat : Pet { } 2. Objective-C 을 활용한 옵셔널@objc protocol Animal { @objc func doMoreCry()}// 에러class Cat : Animal{ } 위와 같이 @objc로 프로토콜을 선언할 수 있는데objc의 경우 자체적으로 옵셔..

Nested Type이란?sturct나 enum 안에 다른 타입을 넣는 걸 뜻한다. 예시struct MyPet { enum Kind { case cat case dog var value : String { // 자기 자신을 switch구문으로 정의할 수 있다. switch self { case .cat : return "고양이" case .dog : return "강아지" } } } // Kind enum} value를 중첩타입이라고 부른다 struct MyPet { enum Kind { cas..

Swift에서 자료형을 체크하는 방법을 알아보려한다.if isguard isswitch case isif case isguard case is if isclass Cat {}class Dog {}let myCat = Cat()if myCat is Cat { print("고양이입니다")} guard is func checkIfIsCat(){ guard myCat is Dog else { print("고양이가 아닙니다") return } print("고양이입니다.")}checkIfIsCat() // 고양이가 아닙니다 switch case isswitch myCat {case is Dog: print("강아지입니다.") // 자료형 명을 변경해서 확인..