Fast, Safe, Expressive 목적으로 개발중인 언어
세미콜론(;) 생략 가능
변수출력
print(“변수 출력 \(변수명)“)
// 변수 선언 예시
var aaa:Int = 10
const ABC:String = “Constant”
Alias (별칭) 지정
typealias MyInt = Int
typealias MyDouble = Double
Tuple
var myTuple: (String, Int, Double) = ("a", 1, 1.0) // 선언
print("\(myTuple.0), \(myTuple.1), \(myTuple.2)") // 출력
Array
var fruitList:Array<String> = ["apple", "banana", "coconut"]
var fruits:[String] = ["apple", "banana", "coconut"]
// method
.first
.last
.append(value)
.append(contentsOf: [value1, value2]) // Array 삽입
.index(of: value)
Set
var animals:Set<String> = Set<String>() // init.
var animals:Set<String> = ["elephant", "monkey"] // init. with val.
// method
.remove(value)
.union(animals) // 합집합
.subtracting(animals) // 차집합
.intersection(animals) // 교집합
.isDisjoint(with: animals) // 상호 베타적인지
.isSubset(of: animals) // animals에 포함되는지 (animals의 부분집합인지)
.isSuperset(of: animals) // animals의 포함하는지 (animals의 전체집합인지)
Dictionary
Key, Value 자료구조
var myDict:Dictionary<String, Int> = Dictionary<String, Int>() // init.
var myDict:Dictionary<String, Int> = ["a": 1, "b":2] // init. with val.
print(myDict["a"])
// method
.removeValue(forKey: "key")
Declare
// 1. 일반 선언
enum Zoo {
case monkey, elephant, rabbit
}
// 2. Raw Value 선언 (일반 선언과 혼합 가능)
enum Zoo:String {
case monkey = "원숭이"
case elephant = "코끼리"
case rabbit = "토끼"
}
// 호출 시
Zoo.monkey
열거형
enum PastaTaste {case cream, tomato}
enum PizzaDough {case cheeseCrust, thin, original}
enum PizzaTopping {case pepperoni, cheese, bacon}
enum MainDish 1 {
case pasta (taste: PastaTaste)
case pizza(dough: PizzaDough, topping: PizzaTopping)
case chicken(withSauce: Bool)
case rice
}
모든 case 조회 (allCases)
enum AAA: CaseIterable {...}
let allCases: [AAA] = AAA.allCases
enum AAA: String CaseIterable{
case a = "a"
...
static var allCases: [AAA] {
let all: [AAA] = [.a, .b, ...]
// implement here
return all
}
}
Boolean
연산자 | 설명 |
---|---|
!A | A의 bool값 반대 |
A && B | A와 B의 AND 논리 연산 |
A || B | A와 B의 OR 논리 연산 |
Bit
연산자 | 설명 |
---|---|
~A | A의 bit 반전 |
A & B | A와 B의 AND 논리 연산 |
A | B | A와 B의 OR 논리 연산 |
A^B | A와 B의 XOR 논리 연산 |
A >> B | A를 B만큼 우측 이동 |
A << B | A를 B만큰 좌측 이동 |
전/중/후위 연산자
전위 연산자(prefix), 중위 연산자(infix), 후위 연산자(postfix)를 정의해 사용 가능
[option] + click : 퀵헬프, 함수옵션 및 설명