데이터 타입

Hyunseok·2021년 6월 18일
0

Swift-기본문법정리

목록 보기
3/4
post-thumbnail

기본형

📌 Bool

참은 true 으로, 거짓은 false로 표현

📌 정수 Int, UInt

실행 환경에 따라 값의 범위 변경

  • 64비트 환경이면 Int는 Int64로 설정되어 64비트(8bytes) 정수형 타입

UInt양수만 할당 가능

📌 부동소수 Float, Double

Float32비트 부동소수형이고, Double64비트 부동소수형이다.

📌 문자 관련 Character, String

둘 다 유니코드 를 사용하며, 큰 따옴표(" ") 를 사용




Any, AnyObject, nil

📌 Any

함수 타입을 제외한 클래스 포함 Swift의 모든 타입 을 지칭하는 키워드

  • is로 어떤 타입을 가지고 있는지 확인 후, as 로 형변환하여 사용 가능
class Human {
    var weight: Double
    var tall: Double
    
    init(_ weight: Double, _ tall: Double){
        self.weight = weight
        self.tall = tall
    }
    
}


var any: Any = Human(13,12)

if any is Human {
    if let instance: Human = any as? Human {
        print("tall = \(instance)")
    }
}

📌 AnyObject

모든 클래스를 지칭하는 프로토콜

📌 nil

null 과 같이 없음 을 의미하는 키워드

  • Optional 타입의 변수나 상수에만 할당이 가능



컬렉션

📌 Array

순서(인덱스)가 있는 리스트 컬렉션

1. Array 생성

[Int]Array<Int>와 동일하게 사용

var integers: Array<Int> = Array<Int>()
// var integers: [Int] = Array<Int>()
// var integers: Array<Int> = [Int]()
// var integers: [Int] = [Int]()
// var integers: [Int] = []

2. 값 추가

integers.append(10)

3. 값 제거

integers.remove(at: 0)
integers.removeLast()
integers.removeAll()

4. 인덱스 i 의 값 조회

integers[i]

5. Array 길이

integers.count

📌 Dictionary

key와 value로 구성된 컬렉션

1. Dictionary 생성

[String: Any]Dictionary<String, Any>와 동일하게 사용

var anyDict: Dictionary<String, Any> = Dictionary<String, Any>()
// var anyDict: [String: Any] = [String: Any]()

2. 값 추가

anyDict["someKey"] = "value"

3. 값 제거

anyDict.removeKey(forKey: "someKey")
anyDict["someKey"] = nil

📌 Set

순서가 없고 중복된 값이 없는 컬렉션

1. Set 생성

var integerSet: Set<Int> = Set<Int>()

2. 값 추가

integerSet.insert(10)

3. 값 제거

integerSet.remove(value)
integerSet.removeFirst()

4. Set 길이

integerSet.count

5. 집합 관련 함수

  • 합집합 setA.union(setB)

  • 교집합 setA.intersection(setB)

  • 차집합 setA.substracting(setB)




🧷 참고한 강의 : "iOS 프로그래밍을 위한 스위프트 기초"

0개의 댓글

관련 채용 정보