4: complex data types 2

그루두·2024년 4월 11일
0

100 days of SwiftUI

목록 보기
5/108

100 days of swiftui
https://www.hackingwithswift.com/100/swiftui/4

annotation

swift에서는 데이터 형태를 지정하는 방법이 두 가지가 있다.

  1. type inference: example1에 String 값을 직접 입력하여 String이라는 데이터 형태를 지정할 수 있다.
let example1 = "This is type inference."
  1. type annotation: example2: String을 입력하여 직접적으로 String이라는 데이터 형태를 지정할 수 있다.
let example2: String = "This is type annotation."
var tempList: [String] = [String]()
var tempDictionary: [String: String] = [String: String]()
var tempSet: Set<String> = Set([])

위처럼 annotation을 통해 직접적으로 데이터 형태를 지정할 수도 있지만, 필수는 아니다.

let username: String
// lots of complex logic
username = "@twostraws"
// lots more complex logic
print(username)

그러나 위 코드처럼 나중에 데이터 값을 입력받는 상수의 경우에는 annotation으로 데이터 형태를 설정해야 한다. 그래서 만약 값을 입력받지 못한다면 오류가 나고, 데이터 값도 나중에 String이 아닌 값을 입력받으면 오류가 난다.

var tempDouble: Double = 0

또 Double 0을 저장할 annotation하지 않고 0만 입력하면 swift는 Int 0으로 인식한다.

데이터 타입별 annotation
https://www.hackingwithswift.com/quick-start/beginners/how-to-use-type-annotations

코드 파일
https://github.com/soaringwave/Ios-studying/commit/5423144f3ec3f4a7664a8cb803d75f7511739acc

profile
계속 해보자

0개의 댓글

관련 채용 정보