iOS
Swift: A Swift Tour - Simple Values
Dev Arthur
2023. 4. 7. 14:30
- You don’t need to import a separate library for functionality like input/output or string handling.
= 입출력 또는 문자열 처리와 같은 기능을 위해 별도의 라이브러리를 가져올 필요가 없다
- Code written at global scope is used as the entry point for the program, so you don’t need a main() function.
= 전역 범위에서 쓰여진 코드는 프로그램의 진입점으로 사용되기에, main() 함수가 필요없다
- You also don’t need to write semicolons at the end of every statement.
= 모든 구문의 끝에 세미콜론(;)을 쓸 필요도 없다
<Simple Values>
- Use let to make a constant and var to make a variable.
= let으로 상수를 만들고 var로 변수를 만든다 - The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.
= 상수의 값은 컴파일 단계에서 알 필요 없지만, 값은 한 번만 할당되어야한다 - This means you can use constants to name a value that you determine once but use in many places.
= 상수는 값의 이름을 한 번만 결정하지만, 사용은 여러 곳에서 할 수 있다 - A constant or variable must have the same type as the value you want to assign to it.
= 상수나 변수에 값을 부여하기 위해서는 타입이 값과 같아야한다 - However, you don’t always have to write the type explicitly.
= 그렇다고 타입을 항상 명시적으로 쓸 필요는 없다 - Providing a value when you create a constant or variable lets the compiler infer its type.
= 상수나 변수를 만들 때 값을 넣으면, 컴파일러에게 타입을 유추하도록 한다 - If the initial value doesn’t provide enough information (or if there isn’t an initial value), specify the type by writing it after the variable, separated by a colon.
= 만약 초기값을 제공하지 않거나 정보가 충분하지 않다면, 변수 뒤에 콜론(:)으로 구분해서 타입을 명시해야한다 - ex)
//타입 명시적
let explicitFloat1 : Float
let explicitFloat2 : Float = 4.0
//타입 암시적
let implicitDouble = 4.0
//※타입을 명시하지 않았을 때, 부동소수점의 숫자인 경우 default type은 Double이다
- Values are never implicitly converted to another type.
= 값은 절대로 암시적으로 다른 타입으로 변환되지 않는다 - If you need to convert a value to a different type, explicitly make an instance of the desired type.
= 만약 값을 다른 타입으로 변환해야한다면, 명시적으로 원하는 타입의 인스턴스를 만들어야한다 - There’s an even simpler way to include values in strings: Write the value in parentheses, and write a backslash (\) before the parentheses.
= 문자열에 값을 포함하는 더 간단한 방법: 괄호 안에 값을 쓰고 괄호 앞에 백슬러쉬(\)를 써라 - ex)
let a : Float = 10
let b : Float = 20
let c : String = "\(a) + \(b) = 30.0"
let name : String = "Arthur"
let hello : String = "Hello, \(name)"
- Use three double quotation marks (""") for strings that take up multiple lines.
= 여러줄을 사용하는 문자열을 쓰려면 큰 따옴표(")를 3번 사용해라 - Indentation at the start of each quoted line is removed, as long as it matches the indentation of the closing quotation marks.
= 닫는 따옴표의 들여쓰기와 일치하는 한(닫는 따옴표와 시작라인이 같다면), 각 인용 줄의 시작 부분의 들여쓰기는 제거된다 - ex)
let quotation = """
Hello World,
Hello iOS.
Hello Arthur
"""
※각 라인의 시작은 닫는 따옴표의 시작보다 앞에 있을 수 없다
※각 라인의 시작을 닫는 따옴표의 시작보다 들여쓴다면, 들여쓰기가 가능하다
- Create arrays and dictionaries using brackets ([])
= 대괄호를 이용해서 배열과 딕셔너리를 만든다 - Access their elements by writing the index or key in brackets.
= 대괄호 안에 인덱스나 키를 넣어서 배열이나 딕셔너리의 요소에 접근한다 - Comma is allowed after the last element.
= 마지막 요소 뒤에 쉼표는 허용된다 - Arrays automatically grow as you add elements.
= 배열은 요소를 추가하면 자동으로 커진다
(딕셔너리도 그렇다) - You also use brackets to write an empty array or dictionary. For an array, write [], and for a dictionary, write [:].
= 배열은 []을 써서, 딕셔너리는 [:] 써서, 빈 배열과 빈 딕셔너리를 만들 수 있다 - If you’re assigning an empty array or dictionary to a new variable, or another place where there isn’t any type information, you need to specify the type.
= 새로운 변수 또는 타입에 대한 정보가 없는 장소에 빈 배열이나 빈 딕셔너리를 넣기 위해서는, 타입을 지정해줘야한다 - ex)
var arr = ["a", "b", "c"]
arr[2] = "z" // ["a", "b", "z"]
var dic = [1 : "a", 2: "b", 3: "c",]
dic[1] = "abb" // [1 : "abb", 2: "b", 3: "c"]
arr.append("cc") // ["a", "b", "z", "cc"]
dic[4] = "zz" // [1 : "abb", 2: "b", 3: "c", 4: "zz"]
arr = [] // []
dic = [:] // [:]
let arr2 : [String] = []
let dic2 : [Int : String] = [:]
반응형