하핫 앙녕 상남자 Philip입니당 데헷 - 'ios Development' 카테고리의 글 목록 (2 Page)
본문 바로가기

ios Development

(19)
tuple decomposition // tuple decomposition = 튜플 분해 let anything = ("person", 400, "John", "mingu", 10.24) // use tuple decomposition let (type, age, who, genius, IQ) = anything // if use decomposition, no need to call it such as anything.type or anything.agesimply call it by their names. since we gave them names. example :print (type) ---------> "person"print (genius) ---------> "mingu" EASY
unnamed tuple, named tuple unnamed tuple let company = ["Aaron", "Bobbie", "Charlie", "Daemon"]company.0 --------> "Aaron"company.3 --------> "Daemon" named tuple let company = [manager: "Aaron", supervisor: "Bobbie", weirdo: "Charlie", useless: "Daemon"]company.manager --------> "Aaron"company.weirdo --------> "Charlie"
syntax optimization 스위프트 문법 최적화 closure 1. parameter 형식과 return 형을 생략한다2. parameter 이름을 생략하고 in 키워드도 지운 후 shorthand argument name으로 대체한다 ($0, $1, $2 ...)3. closure 에 포함된 코드가 단일 return문이라면 return 키워드를 생략한다 (implicit return)4. closure 가 마지막 parameter라면 trailing closure로 작성한다.5. argument label이 남아있다면 삭제한다6. 괄호 사이에 parameter 가 더이상 없다면 괄호도 없애버리자 // import Foundation let list = ["Apple", "Banana", "Cactus", "DeerMeat"] // where 파라미터로 인라인 클로..
while 반복문의 두가지 // while 반복문 var num = 100while num false로 판별됨num += 1}print(num) ------> 위 조건이 false이기 때문에 num += 1 코드가 실행되지 않음. 값은 100 // repeat while 반복문 var num = 100repeat {num += 1 -------> 일단 코드를 먼저 실행하기 때문에 100 + 1 이 됨}while num 101
for in loop의 다양한 활용 // 가장 기본적인 1부터 10까지의 합var sum = 0for counter in 1 ... 10 {sum += counter}print(sum) ------- 결과 = 55 // 짝수만 나열하기 for evenNumbers in stride(from: 2, to: 11, by: 2) {print(evenNumbers)} --------- 결과 = 2, 4, 6, 8, 10 // 구구단 만들어보기 for i in 2...9 { ----------2단부터 9단 까지for j in 1...9 { ----------i 를 곱하기 1부터 9까지print(\(i) X \(j) = \(i * j)}}이렇게 하면 2단부터 9단까지 싸그리 다 출력 오케이
Type Alias Type alias = 일종의 별명을 만드는 것 let width: Double = 30.35let length: Double = 39.44 를 더 알아보기 쉽게 typealias square = Doublelet width: square = 30.35let length: square = 39.44 처럼 표현 가능.
Type safety 두 수의 데이터 타입이 같아야 연산을 실행할 수 있다 let a = 1.58let b = 100let result = a * b ------> 에러because a: Double, b: Int로 데이터타입이 다르기 때문 가능한 식let a = 1.58let b = 100let result = a * Double(b) print(result)------> Double 데이터타입으로 출력되기 때문에 값은 158.0print(Int(result)) -----> Int값으로 출력. 뒤에 소수점 없어짐 값 158 let result = Int(a * double(b)) -----> 값을 Int로 convertprint(result) ----> Int값 158
Type annotation 자료형을 직접 입력하는 것을 Type annotation이라고 한다. ex) let number: Int = 123 Type annotation is that we explicitly declare the datatype.