| C/C++ | Swift | |
| 선언 종료 | 반드시 ; 로 종료 | n/a |
| 데이터 타입 | short / unsigned short int / unsigned int float double bool char / unsigned char string |
Int 혹은(Int8, Int16, Int32, Int64) UInt (부호 없는 정수) 혹은(UInt8, UInt16, UInt32, UInt64) Float Double Bool Character String |
| 상수 선언 | const int a = 0; | let a = 0 혹은 let a : Int = 0 |
| 변수 선언 | int a = 0; | var a = 0 혹은 var a : Int = 0 |
| 변수선언시 null/nil 의 대응 |
없음 (컴파일러가 초기값을 넣어줌) |
var a : String? <- a = nil 의 의미 print(a ?? "dummy value") <- a 가 nil 일 경우는 "dummy value"를 사용 안전하게 ? 를 사용하는 방법 if let a { // a 가 nil 이 아닌 경우 } |
| 형 변환 | int a = 3; double b = (double)a; |
var a = 3 var b = (Double)a |
| IF 문 | if (조건 ) { } else { } |
if 조건 { } else { } |
| SWITCH 문 | switch( 변수 ) { case "A": break; case "B": break; default: break; } |
switch 변수 { case "A": case "B": default: } |
| WHILE | while (조건) { } |
while 조건 { } |
| DO .. WHILE | do { } while (조건); |
repeat { } while 조건 |
| FOR | for (int i = 0; i < 5; i++) { std::cout << i << std::endl; } |
for i in 0..<5 { // 5를 포함시키려면 0...5 print(i) } |
| std::vector<int> arr = {10, 20, 30}; for (int v : arr) { std::cout << v << std::endl; } |
let arr = [10, 20, 30] // 배열 for v in arr { print(v) } |
|
| for (size_t i = 0; i < arr.size(); i++) { std::cout << i << " " << arr[i] << std::endl; } |
for (i, v) in arr.enumerated() { // i 가 필요할 print(i, v) } |
'Programming Languages > Swift' 카테고리의 다른 글
| Concurrency (0) | 2025.12.26 |
|---|---|
| Protocols / Extensions/ Error Handling (0) | 2025.12.26 |
| Class / Struct (0) | 2025.12.26 |
| Functions (함수) / Closures (0) | 2025.12.26 |
| Swift / C++ Collection Types 비교 (0) | 2025.12.26 |