SlideShare a Scribd company logo
Swift의 함수와 메소드
!
창원대학교 정보통신공학과 교수 박동규
!
http://ivis.cwnu.ac.kr/tc/dongupak
https://www.facebook.com/groups/smartphone.forum/
참고문서
http://lean-tra.github.io/Swift-Korean/
함수 Function
• 프로그램의 기본적인 구성요소
• 어떤 작업을 수행하는 문장의 그룹
• 하나의 프로그램은 여러개의 함수들이 모여서 이루어진
다
• 프로그램에서 레고 블럭과 같은 요소
• 가독성의 증대, 유지관리가 쉽다.
함수
함수
Swift의 함수
Swift의 함수
import	 Foundation
Swift의 함수
import	 Foundation
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
n	 =	 Add10(n)
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
n	 =	 Add10(n)
println("After	 Add10(10),	 n	 =	 (n)")
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
n	 =	 Add10(n)
println("After	 Add10(10),	 n	 =	 (n)")
Before Add10(10), n = 21
Swift의 함수
import	 Foundation
func	 Add10(num:	 Int)	 ->	 (Int)	 {
	 	 	 	 var	 result	 =	 num	 +	 10;
	 	 	 	 return	 result
}
var	 n	 =	 21
println("Before	 Add10(10),	 n	 =	 (n)")
n	 =	 Add10(n)
println("After	 Add10(10),	 n	 =	 (n)")
Before Add10(10), n = 21
After Add10(10), n = 31
Swift의 함수
Swift의 함수
Swift의 함수
함수명
Swift의 함수
Swift의 함수
Swift의 함수
매개변수(인자)
Swift의 함수
Swift의 함수
Swift의 함수
반환값
Swift의 함수
Swift의 함수
Swift의 함수
함수 몸체
Swift의 함수
Swift의 함수
Swift의 함수
함수 호출
Swift의 함수
함수 호출
다중 반환값
• C/C++/Objective-C/Java 등의 언어에서는 함수가
하나의 리턴값만을 가질수 밖에 없어 불편함.
• 튜플(tuple)을 사용하면 여러 개의 값을 반환할 수 있다.
• swap 함수의 예를 통해 알아보자
swap 함수
swap 함수
swap 함수
을
swap 함수
을
tuple을 이용하여 받아온다
swap 함수
을
tuple을 이용하여 받아온다
튜플 Tuple
• Swift에서 사용하는 Grouping of Values
• 괄호를 사용하여 아이템을 정의함
• 함수에서 매우 유용하게 사용할 수 있음
튜플 Tuple
(1, 2, 3) // (Int, Int, Int)
(3.14, 1.414) // (Double, Double)
(123, “Busan”, “Seagull”) // (Int, String, String)
…
튜플 Tuple
(1, 2, 3) // (Int, Int, Int)
(3.14, 1.414) // (Double, Double)
(123, “Busan”, “Seagull”) // (Int, String, String)
…
() 안에 ,(쉼표)를 이용해서 동일한 형, 또는 서로 다음 형
의 자료값을 묶어 하나의 자료형으로 취급할 수 있음
튜플 재조합
import	 Foundation	 
!
func	 refreshWebPage()	 ->	 (Int,	 String)	 {	 
	 	 	 	 //	 …	 
	 	 	 	 return	 (200,	 "Success")	 
}	 
!
let	 (statusCode,	 message)	 =	 refreshWebPage()	 
!
println("Received	 (statusCode):	 (message)")
튜플 재조합
import	 Foundation	 
!
func	 refreshWebPage()	 ->	 (Int,	 String)	 {	 
	 	 	 	 //	 …	 
	 	 	 	 return	 (200,	 "Success")	 
}	 
!
let	 (statusCode,	 message)	 =	 refreshWebPage()	 
!
println("Received	 (statusCode):	 (message)")
Received 200: Success
함수 매개변수
func greetings1() {
println("Hello, Guys!")
}
!
func greetings2(name :String) {
println("Hello, (name)!")
}
!
greetings1()
greetings2("Busan Seagulls")
함수 매개변수
func greetings1() {
println("Hello, Guys!")
}
!
func greetings2(name :String) {
println("Hello, (name)!")
}
!
greetings1()
greetings2("Busan Seagulls")
Hello, Guys!
Hello, Busan Seagulls!
함수 매개변수
func greetings1() {
println("Hello, Guys!")
}
!
func greetings2(name :String) {
println("Hello, (name)!")
}
!
greetings1()
greetings2("Busan Seagulls")
함수 매개변수
func greetings1() {
println("Hello, Guys!")
}
!
func greetings2(name :String) {
println("Hello, (name)!")
}
!
greetings1()
greetings2("Busan Seagulls")
Hello, Guys!
Hello, Busan Seagulls!
함수 매개변수
import Foundation
!
func greetings1() {
println("Hello, Guys!")
}
!
func greetings2(name :String) {
println("Hello, (name)!")
}
!
func greetings3(name :String, message :String) {
println("Hello, (name)! (message)")
}
!
greetings1()
greetings2("Busan Seagulls")
greetings3("Park", "Nice to see you!")
Hello, Guys!
Hello, Busan Seagulls!
Hello, Park! Nice to see you!
C로 만든 Swap 함수
#include	 <stdio.h>	 
!
void	 swap(int	 *num1,	 int	 *num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 *num1;	 
	 	 	 	 *num1	 =	 *num2;	 
	 	 	 	 *num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 printf("Before	 :	 n1	 =	 %d	 n",n1);	 
	 	 	 	 printf("Before	 :	 n2	 =	 %d	 n",n2);	 
	 	 	 	 	 
	 	 	 	 swap(&n1,	 &n2);	 
	 	 	 	 	 
	 	 	 	 printf("After	 :	 n1	 =	 %d	 n",n1);	 
	 	 	 	 printf("After	 :	 n2	 =	 %d	 n",n2);	 
	 	 	 	 	 
	 	 	 	 return	 0;	 
}
C로 만든 Swap 함수
#include	 <stdio.h>	 
!
void	 swap(int	 *num1,	 int	 *num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 *num1;	 
	 	 	 	 *num1	 =	 *num2;	 
	 	 	 	 *num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 printf("Before	 :	 n1	 =	 %d	 n",n1);	 
	 	 	 	 printf("Before	 :	 n2	 =	 %d	 n",n2);	 
	 	 	 	 	 
	 	 	 	 swap(&n1,	 &n2);	 
	 	 	 	 	 
	 	 	 	 printf("After	 :	 n1	 =	 %d	 n",n1);	 
	 	 	 	 printf("After	 :	 n2	 =	 %d	 n",n2);	 
	 	 	 	 	 
	 	 	 	 return	 0;	 
}	  포인터 연산자를 이용하여 외부 변수값에 접근함
C++로 만든 Swap 함수#include	 <iostream>	 
using	 namespace	 std;	 
!
void	 swap(int	 &num1,	 int	 &num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 num1;	 
	 	 	 	 num1	 =	 num2;	 
	 	 	 	 num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "Before	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "Before	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
	 	 	 	 	 
	 	 	 	 swap(n1,	 n2);	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "After	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "After	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
!
	 	 	 	 return	 0;	 
}
C++로 만든 Swap 함수#include	 <iostream>	 
using	 namespace	 std;	 
!
void	 swap(int	 &num1,	 int	 &num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 num1;	 
	 	 	 	 num1	 =	 num2;	 
	 	 	 	 num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "Before	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "Before	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
	 	 	 	 	 
	 	 	 	 swap(n1,	 n2);	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "After	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "After	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
!
	 	 	 	 return	 0;	 
}
참조 연산자 &를 이용하여 외부 변수값을 참조함
C++로 만든 Swap 함수#include	 <iostream>	 
using	 namespace	 std;	 
!
void	 swap(int	 &num1,	 int	 &num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 num1;	 
	 	 	 	 num1	 =	 num2;	 
	 	 	 	 num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "Before	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "Before	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
	 	 	 	 	 
	 	 	 	 swap(n1,	 n2);	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "After	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "After	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
!
	 	 	 	 return	 0;	 
}
참조 연산자 &를 이용하여 외부 변수값을 참조함
C++로 만든 Swap 함수#include	 <iostream>	 
using	 namespace	 std;	 
!
void	 swap(int	 &num1,	 int	 &num2)	 {	 
	 	 	 	 int	 temp;	 
	 	 	 	 temp	 =	 num1;	 
	 	 	 	 num1	 =	 num2;	 
	 	 	 	 num2	 =	 temp;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 n1	 =	 100,	 n2	 =	 200;	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "Before	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "Before	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
	 	 	 	 	 
	 	 	 	 swap(n1,	 n2);	 
	 	 	 	 	 
	 	 	 	 cout	 <<	 "After	 :	 n1	 =	 "	 <<	 n1	 <<	 endl;	 
	 	 	 	 cout	 <<	 "After	 :	 n2	 =	 "	 <<	 n2	 <<	 endl;	 
!
	 	 	 	 return	 0;	 
}
참조 연산자 &를 이용하여 외부 변수값을 참조함
Swift로 만든 Swap 함수1
import	 Foundation	 
!
func	 swap(n1:Int,	 n2:Int)	 ->	 (Int,	 Int){	 
	 	 	 	 return	 (n2,	 n1)	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 swap(n1,	 n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap 함수1
import	 Foundation	 
!
func	 swap(n1:Int,	 n2:Int)	 ->	 (Int,	 Int){	 
	 	 	 	 return	 (n2,	 n1)	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 swap(n1,	 n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
튜플을 반환하는 방식으로 간단하게 구현가능
Swift로 만든 Swap 함수1
import	 Foundation	 
!
func	 swap(n1:Int,	 n2:Int)	 ->	 (Int,	 Int){	 
	 	 	 	 return	 (n2,	 n1)	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 swap(n1,	 n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
튜플을 반환하는 방식으로 간단하게 구현가능
Swift로 만든 Swap 함수2
import	 Foundation	 
!
func	 swap(inout	 a:	 Int,	 inout	 b:	 Int)	 
{	 
	 	 	 	 let	 temporaryA	 =	 a	 
	 	 	 	 a	 =	 b	 
	 	 	 	 b	 =	 temporaryA	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
swap(&n1,	 &n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap 함수2
import	 Foundation	 
!
func	 swap(inout	 a:	 Int,	 inout	 b:	 Int)	 
{	 
	 	 	 	 let	 temporaryA	 =	 a	 
	 	 	 	 a	 =	 b	 
	 	 	 	 b	 =	 temporaryA	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
swap(&n1,	 &n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap 함수2
import	 Foundation	 
!
func	 swap(inout	 a:	 Int,	 inout	 b:	 Int)	 
{	 
	 	 	 	 let	 temporaryA	 =	 a	 
	 	 	 	 a	 =	 b	 
	 	 	 	 b	 =	 temporaryA	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
swap(&n1,	 &n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap 함수2
import	 Foundation	 
!
func	 swap(inout	 a:	 Int,	 inout	 b:	 Int)	 
{	 
	 	 	 	 let	 temporaryA	 =	 a	 
	 	 	 	 a	 =	 b	 
	 	 	 	 b	 =	 temporaryA	 
}	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
swap(&n1,	 &n2)	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
inout과 & 연산자를 사용하여 외부값을 참조할 수 있음
inout
• 매개변수 중에서 함수내에서 변경된 매개변수가 함수 호
출이 종료된 시점에서도 계속되기를 원한다면 inout 키
워드를 파라메터 정의할 때 사용한다
• 파라메터의 값은 함수 안으로 전달되어 변경되고, 함수에
서 나와서 원래의 값을 대체함
• 인수를 입출력 파라메터로 넘길때 변수의 이름 앞에 앰퍼
샌드(&)를 붙인다
Swift로 만든 Swap
import	 Foundation	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 (n2,	 n1)	 	 //	 튜플사용	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap
import	 Foundation	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 (n2,	 n1)	 	 //	 튜플사용	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
Swift로 만든 Swap
import	 Foundation	 
!
var	 n1	 =	 100	 
var	 n2	 =	 200	 
!
println("Before	 :	 n1	 =	 (n1)")	 
println("Before	 :	 n2	 =	 (n2)")	 
!
(n1,	 n2)	 =	 (n2,	 n1)	 	 //	 튜플사용	 
!
println("After	 :	 n1	 =	 (n1)")	 
println("After	 :	 n2	 =	 (n2)")
!
(Int, Int) 튜플을 (Int, Int) 튜플에 복사함
함수 레이블
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
join("hello",	 "world",	 ",	 ")
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
join("hello",	 "world",	 ",	 ")
vs
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
join("hello",	 "world",	 ",	 ")
vs
	  func	 join(string	 s1:	 String,	 toString	 s2:	 String,	 withJoiner	 joiner:	 String)	 
	  	 	 	 	 ->	 String	 {	 
	  	 	 	 	 	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  }
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
join("hello",	 "world",	 ",	 ")
vs
	  func	 join(string	 s1:	 String,	 toString	 s2:	 String,	 withJoiner	 joiner:	 String)	 
	  	 	 	 	 ->	 String	 {	 
	  	 	 	 	 	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  }
join(string:	 "hello",	 toString:	 "world",	 withJoiner:	 ",	 ")
함수 레이블
	  	  func	 join(s1:	 String,	 s2:	 String,	 joiner:	 String)	 ->	 String	 {	 
	  	  	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  	  }
join("hello",	 "world",	 ",	 ")
vs
	  func	 join(string	 s1:	 String,	 toString	 s2:	 String,	 withJoiner	 joiner:	 String)	 
	  	 	 	 	 ->	 String	 {	 
	  	 	 	 	 	 	 	 	 return	 s1	 +	 joiner	 +	 s2	 
	  }
join(string:	 "hello",	 toString:	 "world",	 withJoiner:	 ",	 ")
join 함수를 통해 매개변수로 입력된 String을 조합할 수 있다
레이블링을 통해 매개변수의 역할을 명확하게 이해할 수 있다.
Objective-C와의 호환성을 확보할 수 있다
variadic 함수
• 함수구문에서 함수에 전달되는 매개변수의 개수를 정확
하게 알 수 없을 경우에 사용하는 문법이 적용된 함수
	  	  func	 arithmeticMean(numbers:	 Double...)	 ->	 Double	 {	 
	  	  	 	 	 	 var	 total:	 Double	 =	 0	 
	  	  	 	 	 	 for	 number	 in	 numbers	 {	 
	  	  	 	 	 	 	 	 	 	 total	 +=	 number	 
	  	  	 	 	 	 }	 
	  	  	 	 	 	 return	 total	 /	 Double(numbers.count)	 
	  	  }
arithmeticMean(1, 2, 3, 4, 5)
// 다섯 정수의 산술평균 3.0을 반환함
arithmeticMean(3, 8, 19)
// 3,8,19의 산술평균 10.0을 반환함
variadic 함수
• 함수구문에서 함수에 전달되는 매개변수의 개수를 정확
하게 알 수 없을 경우에 사용하는 문법이 적용된 함수
	  	  func	 arithmeticMean(numbers:	 Double...)	 ->	 Double	 {	 
	  	  	 	 	 	 var	 total:	 Double	 =	 0	 
	  	  	 	 	 	 for	 number	 in	 numbers	 {	 
	  	  	 	 	 	 	 	 	 	 total	 +=	 number	 
	  	  	 	 	 	 }	 
	  	  	 	 	 	 return	 total	 /	 Double(numbers.count)	 
	  	  }
arithmeticMean(1, 2, 3, 4, 5)
// 다섯 정수의 산술평균 3.0을 반환함
arithmeticMean(3, 8, 19)
// 3,8,19의 산술평균 10.0을 반환함
함수 타입
• 함수타입의 상수나 변수를 만들어 함수를 할당할 수 있음
두	 개의	 Int	 값을	 매개변수로	 취하며	 Int	 값을	 반환하는	 함수	 타입	 변
수	 mathFuntion을	 선언하고,	 이	 변수가	 addTwoInts	 함수를	 참조(refer)하
도록	 한다.
!
var mathFunction: (Int, Int) -> Int = addTwoInts
함수 타입
import	 Foundation	 
!
func	 addTwoInts(a:	 Int,	 b:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 a	 +	 b	 
}	 
!
var	 mathFunction:	 (Int,	 Int)	 ->	 Int	 =	 addTwoInts	 
!
println("Result:	 (mathFunction(2,	 3))")
Result: 5
함수 타입 변수 mathFunction
함수 타입
import	 Foundation	 
!
func	 addTwoInts(a:	 Int,	 b:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 a	 +	 b	 
}	 
!
var	 mathFunction:	 (Int,	 Int)	 ->	 Int	 =	 addTwoInts	 
!
println("Result:	 (mathFunction(2,	 3))")
Result: 5
함수 타입 변수 mathFunction
Swift 언어의 함수 타입
import	 Foundation	 
!
func	 addTwoInts(a:	 Int,	 b:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 a	 +	 b	 
}	 
!
var	 mathFunction:	 (Int,	 Int)	 ->	 Int	 =	 addTwoInts	 
var	 newMathFunction:	 (Int,	 Int)	 ->	 Int	 
!
newMathFunction	 =	 mathFunction	 
println("Result:	 (mathFunction(2,	 3))")	 
println("Result:	 (newMathFunction(3,	 3))")
Result: 5
Result: 6
두	 개의	 Int	 값을	 취하며	 Int	 값을	 반환하는	 함수	 타입	 변수	 mathFuntion
을	 newMathFunction	 변수가	 참조한다
C 언어의 함수 타입
#include	 <stdio.h>	 
!
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
	 	 	 	 printf("Result	 :	 %dn",	 (*mathFunction)(2,3));	 
	 	 	 	 printf("Result	 :	 %dn",	 (*newMathFunction)(3,3));	 
	 	 	 	 	 
	 	 	 	 return	 0;	 
}	 
Result: 5
Result: 6
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
함수 포인터
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
함수 포인터
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
함수 포인터
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
함수 포인터
C 언어의 함수 타입
…	 
int	 addTwoInts(int	 a,	 int	 b)	 {	 
	 	 	 	 return	 a	 +	 b;	 
}	 
!
int	 main(int	 argc,	 const	 char	 *	 argv[])	 {	 
	 	 	 	 int	 (*mathFunction)(int,	 int)	 =	 &addTwoInts;	 
	 	 	 	 int	 (*newMathFunction)(int,	 int);	 
	 	 	 	 newMathFunction	 =	 mathFunction;	 
	 	 	 	 	 
…
함수 포인터
함수 포인터를 다른 함수 포인터에 복사할 수 있음
함수를 파라메터로 사용
함수를 파라메터로 사용
func	 printMathResult(mathFunction:	 (Int,	 Int)	 ->	 Int,	 a:	 Int,	 b:	 Int)	 {	 
	 	 	 	 println("Result:	 (mathFunction(a,	 b))")	 
}	 
printMathResult(addTwoInts,	 3,	 5)	 
//	 prints	 "Result:	 8"
함수를 파라메터로 사용
func	 printMathResult(mathFunction:	 (Int,	 Int)	 ->	 Int,	 a:	 Int,	 b:	 Int)	 {	 
	 	 	 	 println("Result:	 (mathFunction(a,	 b))")	 
}	 
printMathResult(addTwoInts,	 3,	 5)	 
//	 prints	 "Result:	 8"
함수를 파라메터로 사용
func	 printMathResult(mathFunction:	 (Int,	 Int)	 ->	 Int,	 a:	 Int,	 b:	 Int)	 {	 
	 	 	 	 println("Result:	 (mathFunction(a,	 b))")	 
}	 
printMathResult(addTwoInts,	 3,	 5)	 
//	 prints	 "Result:	 8"
함수를 파라메터로 사용
func	 printMathResult(mathFunction:	 (Int,	 Int)	 ->	 Int,	 a:	 Int,	 b:	 Int)	 {	 
	 	 	 	 println("Result:	 (mathFunction(a,	 b))")	 
}	 
printMathResult(addTwoInts,	 3,	 5)	 
//	 prints	 "Result:	 8"
Result: 8
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
반환형- Int형을 매개변수로 받아
Int 값을 반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
반환형- Int형을 매개변수로 받아
Int 값을 반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
}
반환형- Int형을 매개변수로 받아
Int 값을 반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
함수의 반환
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward	 
} 매개변수 backwards가 true이면
stepBackward 함수가, false이면
stepForward 함수가 반환된다
반환형- Int형을 매개변수로 받아
Int 값을 반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
Int형 값을 매개변수로 받아 Int 값을
반환하는 함수
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
!
!
!
!
!
}
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
!
!
!
!
!
}
func	 stepForward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 +	 1	 
}	 
func	 stepBackward(input:	 Int)	 ->	 Int	 {	 
	 	 	 	 return	 input	 -	 1	 
}
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
!
!
!
!
!
}
중첩 함수
• 함수 내부에 다른 함수를 중첩할 수 있다
func	 chooseStepFunction(backwards:	 Bool)	 ->	 (Int)	 ->	 Int	 {	 
!
!
!
!
!
}
	 	 	 	 func	 stepForward(input:	 Int)	 ->	 Int	 {	 return	 input	 +	 1	 }	 
	 	 	 	 func	 stepBackward(input:	 Int)	 ->	 Int	 {	 return	 input	 -	 1	 }	 
	 	 	 	 return	 backwards	 ?	 stepBackward	 :	 stepForward
제너릭스 Generics
• 함수의 자료형에 상관없이 일관된 알고리즘만으로 효율
적으로 함수 작성이 가능함
• Swift의 많은 기본 라이브러리들이 제너릭으로 작성되어
있다. 예) Array, Dictionary 타입등
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
제너릭스
• swap 함수의 값이 Int, Double, String과 같이 다양하다
func	 swapTwoInts(inout	 a:	 Int,	 inout	 b:	 Int)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
	 func	 swapTwoStrings(inout	 a:	 String,	 inout	 b:	 String)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
func	 swapTwoDoubles(inout	 a:	 Double,	 inout	 b:	 Double)	 {
	 	 	 	 	 	 	 	 let	 temporaryA	 =	 a
	 	 	 	 	 	 	 	 a	 =	 b
	 	 	 	 	 	 	 	 b	 =	 temporaryA
	 	 	 	 }
매개변수는 다르지만 함수의 로직은 동일함
자료형 개수 만큼의 함수가 필요하다.
매우 불편하다
제너릭 함수
제너릭 함수
func	 swapTwoValues<T>(inout	 a:	 T,	 inout	 b:	 T)	 {	 
	 	 let	 temporaryA	 =	 a	 
	 	 a	 =	 b	 
	 	 b	 =	 temporaryA	 
}
제너릭 함수
func	 swapTwoValues<T>(inout	 a:	 T,	 inout	 b:	 T)	 {	 
	 	 let	 temporaryA	 =	 a	 
	 	 a	 =	 b	 
	 	 b	 =	 temporaryA	 
}
하나의 함수를 통해서
모든 종류의 자료형에 대한 알고리즘을
만들 수 있다.
제너릭 함수
func	 swapTwoValues<T>(inout	 a:	 T,	 inout	 b:	 T)	 {	 
	 	 let	 temporaryA	 =	 a	 
	 	 a	 =	 b	 
	 	 b	 =	 temporaryA	 
}
var	 someInt	 =	 3	 
var	 anotherInt	 =	 107	 
swapTwoValues(&someInt,	 &anotherInt)	 
//	 someInt	 is	 now	 107,	 and	 anotherInt	 is	 now	 3	 
!
var	 someString	 =	 "hello"	 
var	 anotherString	 =	 "world"	 
swapTwoValues(&someString,	 &anotherString)	 
//	 someString	 is	 now	 "world",	 and	 anotherString	 is	 now	 "hello"
하나의 함수를 통해서
모든 종류의 자료형에 대한 알고리즘을
만들 수 있다.
제너릭 함수
func	 repeat<ItemType>(item:	 ItemType,	 times:	 Int)	 ->	 ItemType[]	 {	 
	 	 	 	 var	 result	 =	 ItemType[]()	 
	 	 	 	 for	 i	 in	 0..times	 {	 
	 	 	 	 	 	 	 	 result	 +=	 item	 
	 	 	 	 }	 
	 	 	 	 return	 result	 
}	 
var	 repeat_knock	 =	 repeat("knock",	 4)	 
var	 repeat_ten	 =	 repeat(10,	 4)	 
!
println("repeat_knock	 =	 (repeat_knock)")	 
println("repeat_ten	 =	 (repeat_ten)")
제너릭 함수
func	 repeat<ItemType>(item:	 ItemType,	 times:	 Int)	 ->	 ItemType[]	 {	 
	 	 	 	 var	 result	 =	 ItemType[]()	 
	 	 	 	 for	 i	 in	 0..times	 {	 
	 	 	 	 	 	 	 	 result	 +=	 item	 
	 	 	 	 }	 
	 	 	 	 return	 result	 
}	 
var	 repeat_knock	 =	 repeat("knock",	 4)	 
var	 repeat_ten	 =	 repeat(10,	 4)	 
!
println("repeat_knock	 =	 (repeat_knock)")	 
println("repeat_ten	 =	 (repeat_ten)")
repeat_knock = [knock, knock, knock, knock]
repeat_ten = [10, 10, 10, 10]
메소드 Method
• 메소드는 클래스나 구조체 타입에 의존적인 함수로 클래
스와 구조체의 인스턴스는 메소드를 정의할 수 있다.
• 또한 타입 자체에 관여하는 타입 메소드를 정의할 수 있
다
메소드
class	 Counter	 {	 
	 	 var	 count	 =	 0	 
	 	 func	 increment()	 {	 
	 	 	 	 	 	 count++	 
	 	 }	 
	 	 func	 incrementBy(amount:	 Int)	 {	 
	 	 	 	 	 	 count	 +=	 amount	 
	 	 }	 
	 	 func	 reset()	 {	 
	 	 	 	 	 	 count	 =	 0	 
	 	 }	 
}
let	 counter	 =	 Counter()	 
//	 초기	 counter값은	 0입니다	 
counter.increment()	 
//	 counter값은	 이제	 1입니다	 
counter.incrementBy(5)	 
//	 counter값은	 이제	 6입니다	 
counter.reset()	 
//	 counter값은	 이제	 0입니다
메소드와 레이블
메소드와 레이블
함수
메소드와 레이블
import	 Foundation	 
!
func	 say(name:String,	 msg:String)	 {	 
	 	 	 	 println("(name)	 say	 (msg)")	 
}	 
!
say("Henry","Hi,Swift")
함수
메소드와 레이블
import	 Foundation	 
!
func	 say(name:String,	 msg:String)	 {	 
	 	 	 	 println("(name)	 say	 (msg)")	 
}	 
!
say("Henry","Hi,Swift")
함수 메소드
메소드와 레이블
import	 Foundation	 
!
func	 say(name:String,	 msg:String)	 {	 
	 	 	 	 println("(name)	 say	 (msg)")	 
}	 
!
say("Henry","Hi,Swift")
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 var	 name:	 String	 =	 ""	 
	 	 	 	 func	 say(name:String,	 msg:String){	 
	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 }	 
}	 
!
var	 hello	 =	 Hello()	 
hello.say("Henry",	 "Hi,Swift")
함수 메소드
메소드와 레이블
import	 Foundation	 
!
func	 say(name:String,	 msg:String)	 {	 
	 	 	 	 println("(name)	 say	 (msg)")	 
}	 
!
say("Henry","Hi,Swift")
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 var	 name:	 String	 =	 ""	 
	 	 	 	 func	 say(name:String,	 msg:String){	 
	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 }	 
}	 
!
var	 hello	 =	 Hello()	 
hello.say("Henry",	 "Hi,Swift")
함수 메소드
메소드와 레이블
import	 Foundation	 
!
func	 say(name:String,	 msg:String)	 {	 
	 	 	 	 println("(name)	 say	 (msg)")	 
}	 
!
say("Henry","Hi,Swift")
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 var	 name:	 String	 =	 ""	 
	 	 	 	 func	 say(name:String,	 msg:String){	 
	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 }	 
}	 
!
var	 hello	 =	 Hello()	 
hello.say("Henry",	 "Hi,Swift")
함수 메소드
메소드와 레이블
• 함수의 경우 매개변수마다 레이블
을 달 필요는 없다.
• 메소드의 경우 두번째 이상의 레이
블에 대해서는 반드시 레이블을 달
아야 한다
• Objective-C의 영향을 받은 것
• 매개변수의 역할을 정확하게 알 수
있어, 에러를 줄일 수 있다.
메소드와 레이블
• 함수의 경우 매개변수마다 레이블
을 달 필요는 없다.
• 메소드의 경우 두번째 이상의 레이
블에 대해서는 반드시 레이블을 달
아야 한다
• Objective-C의 영향을 받은 것
• 매개변수의 역할을 정확하게 알 수
있어, 에러를 줄일 수 있다.
메소드
메소드와 레이블
• 함수의 경우 매개변수마다 레이블
을 달 필요는 없다.
• 메소드의 경우 두번째 이상의 레이
블에 대해서는 반드시 레이블을 달
아야 한다
• Objective-C의 영향을 받은 것
• 매개변수의 역할을 정확하게 알 수
있어, 에러를 줄일 수 있다.
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 var	 name:	 String	 =	 ""	 
	 	 	 	 func	 say(name:String,	 msg:String){	 
	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 }	 
}	 
!
var	 hello	 =	 Hello()	 
hello.say("Henry",	 msg:"Hi,Swift")
메소드
메소드와 레이블
• 함수의 경우 매개변수마다 레이블
을 달 필요는 없다.
• 메소드의 경우 두번째 이상의 레이
블에 대해서는 반드시 레이블을 달
아야 한다
• Objective-C의 영향을 받은 것
• 매개변수의 역할을 정확하게 알 수
있어, 에러를 줄일 수 있다.
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 var	 name:	 String	 =	 ""	 
	 	 	 	 func	 say(name:String,	 msg:String){	 
	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 }	 
}	 
!
var	 hello	 =	 Hello()	 
hello.say("Henry",	 msg:"Hi,Swift")
Henry say Hi,Swift
메소드
레이블
class	 Counter	 {	 
	 	 var	 count:	 Int	 =	 0	 
	 	 func	 incrementBy(amount:	 Int,	 numberOfTimes:	 Int)	 {	 
	 	 	 	 	 	 count	 +=	 amount	 *	 numberOfTimes	 
	 	 }	 
}
let	 counter	 =	 Counter()	 
Counter.incrementBy(5,	 numberOfTimes:	 3)	 
//	 Counter	 value	 is	 now	 15
레이블
class	 Counter	 {	 
	 	 var	 count:	 Int	 =	 0	 
	 	 func	 incrementBy(amount:	 Int,	 numberOfTimes:	 Int)	 {	 
	 	 	 	 	 	 count	 +=	 amount	 *	 numberOfTimes	 
	 	 }	 
}
let	 counter	 =	 Counter()	 
Counter.incrementBy(5,	 numberOfTimes:	 3)	 
//	 Counter	 value	 is	 now	 15
메소드의 매개변수인 numberOfTime 를 호출시의
레이블로 사용할 수 있다. 이 레이블을 통해 두번째
매개변수의 목적을 명확하게 알 수 있다
레이블
class	 Counter	 {	 
	 	 var	 count:	 Int	 =	 0	 
	 	 func	 incrementBy(amount:	 Int,	 #numberOfTimes:	 Int)	 {	 
	 	 	 	 	 	 count	 +=	 amount	 *	 numberOfTimes	 
	 	 }	 
}
let	 counter	 =	 Counter()	 
Counter.incrementBy(5,	 numberOfTimes:	 3)	 
//	 Counter	 value	 is	 now	 15
레이블
class	 Counter	 {	 
	 	 var	 count:	 Int	 =	 0	 
	 	 func	 incrementBy(amount:	 Int,	 #numberOfTimes:	 Int)	 {	 
	 	 	 	 	 	 count	 +=	 amount	 *	 numberOfTimes	 
	 	 }	 
}
let	 counter	 =	 Counter()	 
Counter.incrementBy(5,	 numberOfTimes:	 3)	 
//	 Counter	 value	 is	 now	 15
numberOfTime 와 같은 매개변수 이름을
레이블로 사용할 수 있다.(디폴트 매개변수명)
이때 매개변수의 이름에 #을 붙여서 이를 명시적으로
만들 수 있다-Objective-C의 문법과 동일함.
레이블
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 …	 
	 	 	 	 func	 say(name:String,	 msg:String,	 ofCount	 n:Int){	 
	 	 	 	 	 	 	 	 for	 index	 in	 1...n	 {	 
	 	 	 	 	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 	 	 	 	 }	 
	 	 	 	 }	 
}	 
…	 
hello.say("Henry",	 msg:"Hi,Swift",	 ofCount:3)
레이블
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 …	 
	 	 	 	 func	 say(name:String,	 msg:String,	 ofCount	 n:Int){	 
	 	 	 	 	 	 	 	 for	 index	 in	 1...n	 {	 
	 	 	 	 	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 	 	 	 	 }	 
	 	 	 	 }	 
}	 
…	 
hello.say("Henry",	 msg:"Hi,Swift",	 ofCount:3)	 
위 메소드의 ofCount는 레이블이며, n은 매개변수이다
레이블과 매개변수는 다를 수 있다
함수 레이블(익명 레이블)
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 …	 
	 	 	 	 func	 say(name:String,	 _	 msg:String,	 _	 n:Int){	 
	 	 	 	 	 	 	 	 for	 index	 in	 1...n	 {	 
	 	 	 	 	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 	 	 	 	 }	 
	 	 	 	 }	 
}	 
…	 
hello.say("Henry","Hi,Swift",	 3)
함수 레이블(익명 레이블)
레이블 대신 _(언더바)를 사용하여 레이블을 생력할 수 있다.
import	 Foundation	 
!
class	 Hello	 {	 
	 	 	 	 …	 
	 	 	 	 func	 say(name:String,	 _	 msg:String,	 _	 n:Int){	 
	 	 	 	 	 	 	 	 for	 index	 in	 1...n	 {	 
	 	 	 	 	 	 	 	 	 	 	 	 println("(name)	 say	 (msg)")	 
	 	 	 	 	 	 	 	 }	 
	 	 	 	 }	 
}	 
…	 
hello.say("Henry","Hi,Swift",	 3)
self 속성
• self는 내재적인 속성으로 모든 인스턴스가 가지고 있다.
• self는 인스턴스 자체를 지시한다
func	 increment()	 {	 
	 	 	 self.count++	 
}
self 속성
• self는 내재적인 속성으로 모든 인스턴스가 가지고 있다.
• self는 인스턴스 자체를 지시한다
func	 increment()	 {	 
	 	 	 self.count++	 
}
위와같이 메소드에 self를 반드시 달 필요는 없으며
self. 가 없을 경우 Swift는 현재 인스턴스의 메소드나 속성을
가리키는 것으로 가정한다
self 속성
self 속성
	  	  struct	 Point	 {	 
	  	  	 	 	 	 var	 x	 =	 0.0,	 y	 =	 0.0	 
	  	  	 	 	 	 func	 isToTheRightOfX(x:	 Double)	 ->	 Bool	 {	 
	  	  	 	 	 	 	 	 	 	 return	 	 	 	 	 x	 >	 x	 
	  	  	 	 	 	 }	 
	  	  }
self 속성
	  	  struct	 Point	 {	 
	  	  	 	 	 	 var	 x	 =	 0.0,	 y	 =	 0.0	 
	  	  	 	 	 	 func	 isToTheRightOfX(x:	 Double)	 ->	 Bool	 {	 
	  	  	 	 	 	 	 	 	 	 return	 	 	 	 	 x	 >	 x	 
	  	  	 	 	 	 }	 
	  	  }
self.
self 속성
	  	  struct	 Point	 {	 
	  	  	 	 	 	 var	 x	 =	 0.0,	 y	 =	 0.0	 
	  	  	 	 	 	 func	 isToTheRightOfX(x:	 Double)	 ->	 Bool	 {	 
	  	  	 	 	 	 	 	 	 	 return	 	 	 	 	 x	 >	 x	 
	  	  	 	 	 	 }	 
	  	  }
self.
위와 같은 메소드에서 x > x라고 할 경우 x가 매개변수 x인지
Point의 멤버 변수 x인지를 알 수 없다
self.x > x 를 통해서 self.x 는 멤버변수 x임을 알 수 있다
mutating 키워드
	  	  struct	 Point	 {	 
	  	  	 	 	 	 var	 x	 =	 0.0,	 y	 =	 0.0	 
	  	  	 	 	 	 mutating	 func	 moveByX(deltaX:	 Double,	 y	 deltaY:	 Double)	 {	 
	  	  	 	 	 	 	 	 	 	 x	 +=	 deltaX	 
	  	  	 	 	 	 	 	 	 	 y	 +=	 deltaY	 
	  	  	 	 	 	 }	 
	  	  }	 
	  	  var	 somePoint	 =	 Point(x:	 1.0,	 y:	 1.0)	 
	  	  somePoint.moveByX(2.0,	 y:	 3.0)	 
	  	  println("The	 point	 is	 now	 at	 ((somePoint.x),	 (somePoint.y))")
mutating 키워드
	  	  struct	 Point	 {	 
	  	  	 	 	 	 var	 x	 =	 0.0,	 y	 =	 0.0	 
	  	  	 	 	 	 mutating	 func	 moveByX(deltaX:	 Double,	 y	 deltaY:	 Double)	 {	 
	  	  	 	 	 	 	 	 	 	 x	 +=	 deltaX	 
	  	  	 	 	 	 	 	 	 	 y	 +=	 deltaY	 
	  	  	 	 	 	 }	 
	  	  }	 
	  	  var	 somePoint	 =	 Point(x:	 1.0,	 y:	 1.0)	 
	  	  somePoint.moveByX(2.0,	 y:	 3.0)	 
	  	  println("The	 point	 is	 now	 at	 ((somePoint.x),	 (somePoint.y))")
메소드 moveByX를 통해 인스턴스의 속성을 변경시키고자 할 경우
mutating 키워드를 사용하여 인스턴스의 속성 x, y를 변경
용어정리
• C나 C++와 같은 프로그래밍 언어에서는 일반적으로 클
래스의 인스턴스를 객체(오브젝트)라고 한다.
• Swift는 다른 언어와 달리 클래스와 구조체의 기능이 유
사하다. 클래스와 구조체의 인스턴스 모두 함수를 가질
수 있다.
• 이 때문에 Swift에서는 객체라는 용어보다 일반적인
인스턴스라는 용어를 사용한다
클래스와 구조체
• 클래스(class)와 구조체(struct)는 모두 범용적이고 가
변적인 프로그램의 구성 성분이다
• 클래스와 구조체 모두 상수, 변수, 함수를 가지는데, 이때
문법은 동일하다.
클래스와 구조체의 유사성
• 값을 저장할 속성을 정의한다
• 기능을 위한 메소드를 정의한다
• subscript 구문을 사용하여 이들의 값에 접근할 수 있도록
subscript를 제공한다
• 초기상태를 설정하기 위한 초기설정자를 제공한다
• 기본 구현내용을 확장하기 위한 기능을 제공한다
• 어떤 표준적인 기능을 제공하는 프로토콜을 따른다
클래스와 구조체의 차이점
• 클래스는 구조체가 가지지 못한 다음 기능을 가진다
• 한 클래스의 특성을 상속받는 기능이 가능하다
• 클래스 인스턴스를 형을 검사하고 번영하여 런타임시
에 형변환을 할 수 있다
• 할당된 임의의 리소스를 해지하는 deinitializer를 가
진다
• 클래스 인스턴스는 참조 카운터를 하나이상 허용한다
정리
• Swift의 함수는 C언어의 함수보다 간결하면서도 쉽다
• 튜플타입을 사용하여 함수를 유연성있게 사용할 수 있다
• Objective-C의 레이블 기능을 메소드에서도 사용할 수
있도록 하여 호환성을 확보하였다
• 제너릭 타입은 자료형에 무관한 유연한 코딩이 가능하다
• Swift의 철학은 간결하면서도 안전한 프로그램을 추구한
다
Q & A
dongupak@gmail.com

More Related Content

What's hot

Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Circulus
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
Circulus
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
Jong Wook Kim
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기
jongho jeong
 
7가지 동시성 모델 4장
7가지 동시성 모델 4장7가지 동시성 모델 4장
7가지 동시성 모델 4장
HyeonSeok Choi
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
Circulus
 
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
흥배 최
 
Do swift: Swift 무작정 해보기
Do swift: Swift 무작정 해보기Do swift: Swift 무작정 해보기
Do swift: Swift 무작정 해보기
YoonBong Steve Kim
 
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
Yongha Yoo
 
GDG 2014 - RxJava를 활용한 Functional Reactive Programming
GDG 2014 - RxJava를 활용한 Functional Reactive Programming GDG 2014 - RxJava를 활용한 Functional Reactive Programming
GDG 2014 - RxJava를 활용한 Functional Reactive Programming
waynejo
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
HyeonSeok Choi
 
6 swift 고급함수
6 swift 고급함수6 swift 고급함수
6 swift 고급함수
Changwon National University
 
일단 시작하는 코틀린
일단 시작하는 코틀린일단 시작하는 코틀린
일단 시작하는 코틀린
Park JoongSoo
 
EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overview
yongwoo Jeon
 
React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기
Kim Hunmin
 
UML distilled 1장 스터디 발표 자료
UML distilled 1장 스터디 발표 자료UML distilled 1장 스터디 발표 자료
UML distilled 1장 스터디 발표 자료
beom kyun choi
 
7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍
Hyunsoo Jung
 
골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료
욱진 양
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOS
Eunjoo Im
 

What's hot (19)

Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
 
Startup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JSStartup JavaScript 8 - NPM, Express.JS
Startup JavaScript 8 - NPM, Express.JS
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기
 
7가지 동시성 모델 4장
7가지 동시성 모델 4장7가지 동시성 모델 4장
7가지 동시성 모델 4장
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
 
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
[KGC 2012]Boost.asio를 이용한 네트웍 프로그래밍
 
Do swift: Swift 무작정 해보기
Do swift: Swift 무작정 해보기Do swift: Swift 무작정 해보기
Do swift: Swift 무작정 해보기
 
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
스위프트, 코틀린과 모던언어의 특징 (Swift, Kotlin and Modern Languages)
 
GDG 2014 - RxJava를 활용한 Functional Reactive Programming
GDG 2014 - RxJava를 활용한 Functional Reactive Programming GDG 2014 - RxJava를 활용한 Functional Reactive Programming
GDG 2014 - RxJava를 활용한 Functional Reactive Programming
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
6 swift 고급함수
6 swift 고급함수6 swift 고급함수
6 swift 고급함수
 
일단 시작하는 코틀린
일단 시작하는 코틀린일단 시작하는 코틀린
일단 시작하는 코틀린
 
EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overview
 
React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기
 
UML distilled 1장 스터디 발표 자료
UML distilled 1장 스터디 발표 자료UML distilled 1장 스터디 발표 자료
UML distilled 1장 스터디 발표 자료
 
7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍7가지 동시성 모델 - 3장. 함수형 프로그래밍
7가지 동시성 모델 - 3장. 함수형 프로그래밍
 
골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOS
 

Similar to Swift의 함수와 메소드

5 swift 기초함수
5 swift 기초함수5 swift 기초함수
5 swift 기초함수
Changwon National University
 
20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crow
Jaeseung Ha
 
DEVIEW-FULL-감독판.pptx
DEVIEW-FULL-감독판.pptxDEVIEW-FULL-감독판.pptx
DEVIEW-FULL-감독판.pptx
hanbeom Park
 
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613KTH, 케이티하이텔
 
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
Jaeseung Ha
 
Node.js and react
Node.js and reactNode.js and react
Node.js and react
HyungKuIm
 
함수형 프로그래밍? 그래서 어떻게
함수형 프로그래밍? 그래서 어떻게함수형 프로그래밍? 그래서 어떻게
함수형 프로그래밍? 그래서 어떻게
Gyooha Kim
 
NDC11_슈퍼클래스
NDC11_슈퍼클래스NDC11_슈퍼클래스
NDC11_슈퍼클래스
noerror
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
명신 김
 
ffmpeg optimization using CUDA
ffmpeg optimization using CUDAffmpeg optimization using CUDA
ffmpeg optimization using CUDA
yyooooon
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기
지수 윤
 
Naver api for android
Naver api for androidNaver api for android
Naver api for androidSangon Lee
 
Spark 소개 1부
Spark 소개 1부Spark 소개 1부
Spark 소개 1부
Jinho Yoo
 
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
Seok-joon Yun
 
병렬 프로그래밍
병렬 프로그래밍병렬 프로그래밍
병렬 프로그래밍
준혁 이
 
Airflow를 이용한 데이터 Workflow 관리
Airflow를 이용한  데이터 Workflow 관리Airflow를 이용한  데이터 Workflow 관리
Airflow를 이용한 데이터 Workflow 관리
YoungHeon (Roy) Kim
 
아두이노 2015-2 한동대학교 공학설계입문
아두이노 2015-2 한동대학교 공학설계입문아두이노 2015-2 한동대학교 공학설계입문
아두이노 2015-2 한동대학교 공학설계입문
Sangjun Han
 
Basic git-commands
Basic git-commandsBasic git-commands
Basic git-commands
insanehong Kim
 

Similar to Swift의 함수와 메소드 (20)

5 swift 기초함수
5 swift 기초함수5 swift 기초함수
5 swift 기초함수
 
20150212 c++11 features used in crow
20150212 c++11 features used in crow20150212 c++11 features used in crow
20150212 c++11 features used in crow
 
DEVIEW-FULL-감독판.pptx
DEVIEW-FULL-감독판.pptxDEVIEW-FULL-감독판.pptx
DEVIEW-FULL-감독판.pptx
 
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613
KTH_Detail day_화성에서 온 개발자 금성에서 온 기획자 시리즈_5차_데이터분석_조범석_20120613
 
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
 
Node.js and react
Node.js and reactNode.js and react
Node.js and react
 
함수형 프로그래밍? 그래서 어떻게
함수형 프로그래밍? 그래서 어떻게함수형 프로그래밍? 그래서 어떻게
함수형 프로그래밍? 그래서 어떻게
 
NDC11_슈퍼클래스
NDC11_슈퍼클래스NDC11_슈퍼클래스
NDC11_슈퍼클래스
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
ffmpeg optimization using CUDA
ffmpeg optimization using CUDAffmpeg optimization using CUDA
ffmpeg optimization using CUDA
 
Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기Javascript개발자의 눈으로 python 들여다보기
Javascript개발자의 눈으로 python 들여다보기
 
Naver api for android
Naver api for androidNaver api for android
Naver api for android
 
Spark 소개 1부
Spark 소개 1부Spark 소개 1부
Spark 소개 1부
 
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
 
병렬 프로그래밍
병렬 프로그래밍병렬 프로그래밍
병렬 프로그래밍
 
Ndc12 2
Ndc12 2Ndc12 2
Ndc12 2
 
Airflow를 이용한 데이터 Workflow 관리
Airflow를 이용한  데이터 Workflow 관리Airflow를 이용한  데이터 Workflow 관리
Airflow를 이용한 데이터 Workflow 관리
 
06장 함수
06장 함수06장 함수
06장 함수
 
아두이노 2015-2 한동대학교 공학설계입문
아두이노 2015-2 한동대학교 공학설계입문아두이노 2015-2 한동대학교 공학설계입문
아두이노 2015-2 한동대학교 공학설계입문
 
Basic git-commands
Basic git-commandsBasic git-commands
Basic git-commands
 

More from Changwon National University

생성인공지능둘러보기.pdf
생성인공지능둘러보기.pdf생성인공지능둘러보기.pdf
생성인공지능둘러보기.pdf
Changwon National University
 
2011 app center Changwon National Univ.
2011 app center Changwon National Univ.2011 app center Changwon National Univ.
2011 app center Changwon National Univ.
Changwon National University
 
인공지능의 파도가 온다
인공지능의 파도가 온다인공지능의 파도가 온다
인공지능의 파도가 온다
Changwon National University
 
Mobile Healthcare Application
Mobile Healthcare ApplicationMobile Healthcare Application
Mobile Healthcare Application
Changwon National University
 
바다 즐기기
바다 즐기기바다 즐기기
알아두면 편리한 macOS 에디터 단축키와 기능
알아두면 편리한 macOS 에디터  단축키와 기능알아두면 편리한 macOS 에디터  단축키와 기능
알아두면 편리한 macOS 에디터 단축키와 기능
Changwon National University
 
키보드 기호의 이름 알아보기(한국어, 영어)
키보드 기호의 이름 알아보기(한국어, 영어)키보드 기호의 이름 알아보기(한국어, 영어)
키보드 기호의 이름 알아보기(한국어, 영어)
Changwon National University
 
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
Changwon National University
 
20 2 강의를 마치며
20 2 강의를 마치며20 2 강의를 마치며
20 2 강의를 마치며
Changwon National University
 
20 1 코딩스타일
20 1 코딩스타일20 1 코딩스타일
20 1 코딩스타일
Changwon National University
 
18 2 파이썬표준라이브러리
18 2 파이썬표준라이브러리18 2 파이썬표준라이브러리
18 2 파이썬표준라이브러리
Changwon National University
 
18 1 파이썬패키지
18 1 파이썬패키지18 1 파이썬패키지
18 1 파이썬패키지
Changwon National University
 
17 2 필터함수와 맵함수
17 2 필터함수와 맵함수17 2 필터함수와 맵함수
17 2 필터함수와 맵함수
Changwon National University
 
17 1 람다함수
17 1 람다함수17 1 람다함수
17 1 람다함수
Changwon National University
 
16 1 상속과super()
16 1 상속과super()16 1 상속과super()
16 1 상속과super()
Changwon National University
 
15 2 클래스정의와self
15 2 클래스정의와self15 2 클래스정의와self
15 2 클래스정의와self
Changwon National University
 
14 4 슬라이싱
14 4 슬라이싱14 4 슬라이싱
14 4 슬라이싱
Changwon National University
 
14 2 iterator
14 2 iterator14 2 iterator
14 3 리스트함수
14 3 리스트함수14 3 리스트함수
14 3 리스트함수
Changwon National University
 
14 1 리스트의 메소드
14 1 리스트의 메소드14 1 리스트의 메소드
14 1 리스트의 메소드
Changwon National University
 

More from Changwon National University (20)

생성인공지능둘러보기.pdf
생성인공지능둘러보기.pdf생성인공지능둘러보기.pdf
생성인공지능둘러보기.pdf
 
2011 app center Changwon National Univ.
2011 app center Changwon National Univ.2011 app center Changwon National Univ.
2011 app center Changwon National Univ.
 
인공지능의 파도가 온다
인공지능의 파도가 온다인공지능의 파도가 온다
인공지능의 파도가 온다
 
Mobile Healthcare Application
Mobile Healthcare ApplicationMobile Healthcare Application
Mobile Healthcare Application
 
바다 즐기기
바다 즐기기바다 즐기기
바다 즐기기
 
알아두면 편리한 macOS 에디터 단축키와 기능
알아두면 편리한 macOS 에디터  단축키와 기능알아두면 편리한 macOS 에디터  단축키와 기능
알아두면 편리한 macOS 에디터 단축키와 기능
 
키보드 기호의 이름 알아보기(한국어, 영어)
키보드 기호의 이름 알아보기(한국어, 영어)키보드 기호의 이름 알아보기(한국어, 영어)
키보드 기호의 이름 알아보기(한국어, 영어)
 
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
AI 로봇 아티스트의 비밀(창원대학교 정보통신공학과 특강)
 
20 2 강의를 마치며
20 2 강의를 마치며20 2 강의를 마치며
20 2 강의를 마치며
 
20 1 코딩스타일
20 1 코딩스타일20 1 코딩스타일
20 1 코딩스타일
 
18 2 파이썬표준라이브러리
18 2 파이썬표준라이브러리18 2 파이썬표준라이브러리
18 2 파이썬표준라이브러리
 
18 1 파이썬패키지
18 1 파이썬패키지18 1 파이썬패키지
18 1 파이썬패키지
 
17 2 필터함수와 맵함수
17 2 필터함수와 맵함수17 2 필터함수와 맵함수
17 2 필터함수와 맵함수
 
17 1 람다함수
17 1 람다함수17 1 람다함수
17 1 람다함수
 
16 1 상속과super()
16 1 상속과super()16 1 상속과super()
16 1 상속과super()
 
15 2 클래스정의와self
15 2 클래스정의와self15 2 클래스정의와self
15 2 클래스정의와self
 
14 4 슬라이싱
14 4 슬라이싱14 4 슬라이싱
14 4 슬라이싱
 
14 2 iterator
14 2 iterator14 2 iterator
14 2 iterator
 
14 3 리스트함수
14 3 리스트함수14 3 리스트함수
14 3 리스트함수
 
14 1 리스트의 메소드
14 1 리스트의 메소드14 1 리스트의 메소드
14 1 리스트의 메소드
 

Swift의 함수와 메소드

  • 1. Swift의 함수와 메소드 ! 창원대학교 정보통신공학과 교수 박동규 ! http://ivis.cwnu.ac.kr/tc/dongupak https://www.facebook.com/groups/smartphone.forum/
  • 3. 함수 Function • 프로그램의 기본적인 구성요소 • 어떤 작업을 수행하는 문장의 그룹 • 하나의 프로그램은 여러개의 함수들이 모여서 이루어진 다 • 프로그램에서 레고 블럭과 같은 요소 • 가독성의 증대, 유지관리가 쉽다.
  • 9. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) {
  • 10. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10;
  • 11. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result
  • 12. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result }
  • 13. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result }
  • 14. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21
  • 15. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)")
  • 16. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)")
  • 17. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)") n = Add10(n)
  • 18. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)") n = Add10(n) println("After Add10(10), n = (n)")
  • 19. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)") n = Add10(n) println("After Add10(10), n = (n)") Before Add10(10), n = 21
  • 20. Swift의 함수 import Foundation func Add10(num: Int) -> (Int) { var result = num + 10; return result } var n = 21 println("Before Add10(10), n = (n)") n = Add10(n) println("After Add10(10), n = (n)") Before Add10(10), n = 21 After Add10(10), n = 31
  • 37. 다중 반환값 • C/C++/Objective-C/Java 등의 언어에서는 함수가 하나의 리턴값만을 가질수 밖에 없어 불편함. • 튜플(tuple)을 사용하면 여러 개의 값을 반환할 수 있다. • swap 함수의 예를 통해 알아보자
  • 43. 튜플 Tuple • Swift에서 사용하는 Grouping of Values • 괄호를 사용하여 아이템을 정의함 • 함수에서 매우 유용하게 사용할 수 있음
  • 44. 튜플 Tuple (1, 2, 3) // (Int, Int, Int) (3.14, 1.414) // (Double, Double) (123, “Busan”, “Seagull”) // (Int, String, String) …
  • 45. 튜플 Tuple (1, 2, 3) // (Int, Int, Int) (3.14, 1.414) // (Double, Double) (123, “Busan”, “Seagull”) // (Int, String, String) … () 안에 ,(쉼표)를 이용해서 동일한 형, 또는 서로 다음 형 의 자료값을 묶어 하나의 자료형으로 취급할 수 있음
  • 46. 튜플 재조합 import Foundation ! func refreshWebPage() -> (Int, String) { // … return (200, "Success") } ! let (statusCode, message) = refreshWebPage() ! println("Received (statusCode): (message)")
  • 47. 튜플 재조합 import Foundation ! func refreshWebPage() -> (Int, String) { // … return (200, "Success") } ! let (statusCode, message) = refreshWebPage() ! println("Received (statusCode): (message)") Received 200: Success
  • 48. 함수 매개변수 func greetings1() { println("Hello, Guys!") } ! func greetings2(name :String) { println("Hello, (name)!") } ! greetings1() greetings2("Busan Seagulls")
  • 49. 함수 매개변수 func greetings1() { println("Hello, Guys!") } ! func greetings2(name :String) { println("Hello, (name)!") } ! greetings1() greetings2("Busan Seagulls") Hello, Guys! Hello, Busan Seagulls!
  • 50. 함수 매개변수 func greetings1() { println("Hello, Guys!") } ! func greetings2(name :String) { println("Hello, (name)!") } ! greetings1() greetings2("Busan Seagulls")
  • 51. 함수 매개변수 func greetings1() { println("Hello, Guys!") } ! func greetings2(name :String) { println("Hello, (name)!") } ! greetings1() greetings2("Busan Seagulls") Hello, Guys! Hello, Busan Seagulls!
  • 52. 함수 매개변수 import Foundation ! func greetings1() { println("Hello, Guys!") } ! func greetings2(name :String) { println("Hello, (name)!") } ! func greetings3(name :String, message :String) { println("Hello, (name)! (message)") } ! greetings1() greetings2("Busan Seagulls") greetings3("Park", "Nice to see you!") Hello, Guys! Hello, Busan Seagulls! Hello, Park! Nice to see you!
  • 53. C로 만든 Swap 함수 #include <stdio.h> ! void swap(int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; printf("Before : n1 = %d n",n1); printf("Before : n2 = %d n",n2); swap(&n1, &n2); printf("After : n1 = %d n",n1); printf("After : n2 = %d n",n2); return 0; }
  • 54. C로 만든 Swap 함수 #include <stdio.h> ! void swap(int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; printf("Before : n1 = %d n",n1); printf("Before : n2 = %d n",n2); swap(&n1, &n2); printf("After : n1 = %d n",n1); printf("After : n2 = %d n",n2); return 0; } 포인터 연산자를 이용하여 외부 변수값에 접근함
  • 55. C++로 만든 Swap 함수#include <iostream> using namespace std; ! void swap(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; cout << "Before : n1 = " << n1 << endl; cout << "Before : n2 = " << n2 << endl; swap(n1, n2); cout << "After : n1 = " << n1 << endl; cout << "After : n2 = " << n2 << endl; ! return 0; }
  • 56. C++로 만든 Swap 함수#include <iostream> using namespace std; ! void swap(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; cout << "Before : n1 = " << n1 << endl; cout << "Before : n2 = " << n2 << endl; swap(n1, n2); cout << "After : n1 = " << n1 << endl; cout << "After : n2 = " << n2 << endl; ! return 0; } 참조 연산자 &를 이용하여 외부 변수값을 참조함
  • 57. C++로 만든 Swap 함수#include <iostream> using namespace std; ! void swap(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; cout << "Before : n1 = " << n1 << endl; cout << "Before : n2 = " << n2 << endl; swap(n1, n2); cout << "After : n1 = " << n1 << endl; cout << "After : n2 = " << n2 << endl; ! return 0; } 참조 연산자 &를 이용하여 외부 변수값을 참조함
  • 58. C++로 만든 Swap 함수#include <iostream> using namespace std; ! void swap(int &num1, int &num2) { int temp; temp = num1; num1 = num2; num2 = temp; } ! int main(int argc, const char * argv[]) { int n1 = 100, n2 = 200; cout << "Before : n1 = " << n1 << endl; cout << "Before : n2 = " << n2 << endl; swap(n1, n2); cout << "After : n1 = " << n1 << endl; cout << "After : n2 = " << n2 << endl; ! return 0; } 참조 연산자 &를 이용하여 외부 변수값을 참조함
  • 59. Swift로 만든 Swap 함수1 import Foundation ! func swap(n1:Int, n2:Int) -> (Int, Int){ return (n2, n1) } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = swap(n1, n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 60. Swift로 만든 Swap 함수1 import Foundation ! func swap(n1:Int, n2:Int) -> (Int, Int){ return (n2, n1) } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = swap(n1, n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)") 튜플을 반환하는 방식으로 간단하게 구현가능
  • 61. Swift로 만든 Swap 함수1 import Foundation ! func swap(n1:Int, n2:Int) -> (Int, Int){ return (n2, n1) } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = swap(n1, n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)") 튜플을 반환하는 방식으로 간단하게 구현가능
  • 62. Swift로 만든 Swap 함수2 import Foundation ! func swap(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! swap(&n1, &n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 63. Swift로 만든 Swap 함수2 import Foundation ! func swap(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! swap(&n1, &n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 64. Swift로 만든 Swap 함수2 import Foundation ! func swap(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! swap(&n1, &n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 65. Swift로 만든 Swap 함수2 import Foundation ! func swap(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! swap(&n1, &n2) ! println("After : n1 = (n1)") println("After : n2 = (n2)") inout과 & 연산자를 사용하여 외부값을 참조할 수 있음
  • 66. inout • 매개변수 중에서 함수내에서 변경된 매개변수가 함수 호 출이 종료된 시점에서도 계속되기를 원한다면 inout 키 워드를 파라메터 정의할 때 사용한다 • 파라메터의 값은 함수 안으로 전달되어 변경되고, 함수에 서 나와서 원래의 값을 대체함 • 인수를 입출력 파라메터로 넘길때 변수의 이름 앞에 앰퍼 샌드(&)를 붙인다
  • 67. Swift로 만든 Swap import Foundation ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = (n2, n1) // 튜플사용 ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 68. Swift로 만든 Swap import Foundation ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = (n2, n1) // 튜플사용 ! println("After : n1 = (n1)") println("After : n2 = (n2)")
  • 69. Swift로 만든 Swap import Foundation ! var n1 = 100 var n2 = 200 ! println("Before : n1 = (n1)") println("Before : n2 = (n2)") ! (n1, n2) = (n2, n1) // 튜플사용 ! println("After : n1 = (n1)") println("After : n2 = (n2)") ! (Int, Int) 튜플을 (Int, Int) 튜플에 복사함
  • 71. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 }
  • 72. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 } join("hello", "world", ", ")
  • 73. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 } join("hello", "world", ", ") vs
  • 74. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 } join("hello", "world", ", ") vs func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2 }
  • 75. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 } join("hello", "world", ", ") vs func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2 } join(string: "hello", toString: "world", withJoiner: ", ")
  • 76. 함수 레이블 func join(s1: String, s2: String, joiner: String) -> String { return s1 + joiner + s2 } join("hello", "world", ", ") vs func join(string s1: String, toString s2: String, withJoiner joiner: String) -> String { return s1 + joiner + s2 } join(string: "hello", toString: "world", withJoiner: ", ") join 함수를 통해 매개변수로 입력된 String을 조합할 수 있다 레이블링을 통해 매개변수의 역할을 명확하게 이해할 수 있다. Objective-C와의 호환성을 확보할 수 있다
  • 77. variadic 함수 • 함수구문에서 함수에 전달되는 매개변수의 개수를 정확 하게 알 수 없을 경우에 사용하는 문법이 적용된 함수 func arithmeticMean(numbers: Double...) -> Double { var total: Double = 0 for number in numbers { total += number } return total / Double(numbers.count) } arithmeticMean(1, 2, 3, 4, 5) // 다섯 정수의 산술평균 3.0을 반환함 arithmeticMean(3, 8, 19) // 3,8,19의 산술평균 10.0을 반환함
  • 78. variadic 함수 • 함수구문에서 함수에 전달되는 매개변수의 개수를 정확 하게 알 수 없을 경우에 사용하는 문법이 적용된 함수 func arithmeticMean(numbers: Double...) -> Double { var total: Double = 0 for number in numbers { total += number } return total / Double(numbers.count) } arithmeticMean(1, 2, 3, 4, 5) // 다섯 정수의 산술평균 3.0을 반환함 arithmeticMean(3, 8, 19) // 3,8,19의 산술평균 10.0을 반환함
  • 79. 함수 타입 • 함수타입의 상수나 변수를 만들어 함수를 할당할 수 있음 두 개의 Int 값을 매개변수로 취하며 Int 값을 반환하는 함수 타입 변 수 mathFuntion을 선언하고, 이 변수가 addTwoInts 함수를 참조(refer)하 도록 한다. ! var mathFunction: (Int, Int) -> Int = addTwoInts
  • 80. 함수 타입 import Foundation ! func addTwoInts(a: Int, b: Int) -> Int { return a + b } ! var mathFunction: (Int, Int) -> Int = addTwoInts ! println("Result: (mathFunction(2, 3))") Result: 5 함수 타입 변수 mathFunction
  • 81. 함수 타입 import Foundation ! func addTwoInts(a: Int, b: Int) -> Int { return a + b } ! var mathFunction: (Int, Int) -> Int = addTwoInts ! println("Result: (mathFunction(2, 3))") Result: 5 함수 타입 변수 mathFunction
  • 82. Swift 언어의 함수 타입 import Foundation ! func addTwoInts(a: Int, b: Int) -> Int { return a + b } ! var mathFunction: (Int, Int) -> Int = addTwoInts var newMathFunction: (Int, Int) -> Int ! newMathFunction = mathFunction println("Result: (mathFunction(2, 3))") println("Result: (newMathFunction(3, 3))") Result: 5 Result: 6 두 개의 Int 값을 취하며 Int 값을 반환하는 함수 타입 변수 mathFuntion 을 newMathFunction 변수가 참조한다
  • 83. C 언어의 함수 타입 #include <stdio.h> ! int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; printf("Result : %dn", (*mathFunction)(2,3)); printf("Result : %dn", (*newMathFunction)(3,3)); return 0; } Result: 5 Result: 6
  • 84. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 85. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 86. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … C 언어의 함수
  • 87. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … C 언어의 함수
  • 88. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 89. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 90. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … 함수 포인터
  • 91. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … 함수 포인터
  • 92. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 93. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; …
  • 94. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … 함수 포인터
  • 95. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … 함수 포인터
  • 96. C 언어의 함수 타입 … int addTwoInts(int a, int b) { return a + b; } ! int main(int argc, const char * argv[]) { int (*mathFunction)(int, int) = &addTwoInts; int (*newMathFunction)(int, int); newMathFunction = mathFunction; … 함수 포인터 함수 포인터를 다른 함수 포인터에 복사할 수 있음
  • 98. 함수를 파라메터로 사용 func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: (mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8"
  • 99. 함수를 파라메터로 사용 func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: (mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8"
  • 100. 함수를 파라메터로 사용 func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: (mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8"
  • 101. 함수를 파라메터로 사용 func printMathResult(mathFunction: (Int, Int) -> Int, a: Int, b: Int) { println("Result: (mathFunction(a, b))") } printMathResult(addTwoInts, 3, 5) // prints "Result: 8" Result: 8
  • 102. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward }
  • 103. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 104. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 105. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 106. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } 반환형- Int형을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 107. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } 반환형- Int형을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 108. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } 반환형- Int형을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 109. 함수의 반환 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } func chooseStepFunction(backwards: Bool) -> (Int) -> Int { return backwards ? stepBackward : stepForward } 매개변수 backwards가 true이면 stepBackward 함수가, false이면 stepForward 함수가 반환된다 반환형- Int형을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수 Int형 값을 매개변수로 받아 Int 값을 반환하는 함수
  • 110. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다
  • 111. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다 func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 }
  • 112. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다 func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ! ! ! ! ! } func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 }
  • 113. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다 func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ! ! ! ! ! } func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 }
  • 114. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다 func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ! ! ! ! ! }
  • 115. 중첩 함수 • 함수 내부에 다른 함수를 중첩할 수 있다 func chooseStepFunction(backwards: Bool) -> (Int) -> Int { ! ! ! ! ! } func stepForward(input: Int) -> Int { return input + 1 } func stepBackward(input: Int) -> Int { return input - 1 } return backwards ? stepBackward : stepForward
  • 116. 제너릭스 Generics • 함수의 자료형에 상관없이 일관된 알고리즘만으로 효율 적으로 함수 작성이 가능함 • Swift의 많은 기본 라이브러리들이 제너릭으로 작성되어 있다. 예) Array, Dictionary 타입등
  • 117. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다
  • 118. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) {
  • 119. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a
  • 120. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b
  • 121. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA
  • 122. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA }
  • 123. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) {
  • 124. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a
  • 125. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b
  • 126. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA
  • 127. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 128. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 129. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 130. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a a = b func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 131. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a a = b b = temporaryA func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 132. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 133. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA }
  • 134. 제너릭스 • swap 함수의 값이 Int, Double, String과 같이 다양하다 func swapTwoInts(inout a: Int, inout b: Int) { let temporaryA = a a = b b = temporaryA } func swapTwoStrings(inout a: String, inout b: String) { let temporaryA = a a = b b = temporaryA } func swapTwoDoubles(inout a: Double, inout b: Double) { let temporaryA = a a = b b = temporaryA } 매개변수는 다르지만 함수의 로직은 동일함 자료형 개수 만큼의 함수가 필요하다. 매우 불편하다
  • 136. 제너릭 함수 func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA }
  • 137. 제너릭 함수 func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA } 하나의 함수를 통해서 모든 종류의 자료형에 대한 알고리즘을 만들 수 있다.
  • 138. 제너릭 함수 func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA } var someInt = 3 var anotherInt = 107 swapTwoValues(&someInt, &anotherInt) // someInt is now 107, and anotherInt is now 3 ! var someString = "hello" var anotherString = "world" swapTwoValues(&someString, &anotherString) // someString is now "world", and anotherString is now "hello" 하나의 함수를 통해서 모든 종류의 자료형에 대한 알고리즘을 만들 수 있다.
  • 139. 제너릭 함수 func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] { var result = ItemType[]() for i in 0..times { result += item } return result } var repeat_knock = repeat("knock", 4) var repeat_ten = repeat(10, 4) ! println("repeat_knock = (repeat_knock)") println("repeat_ten = (repeat_ten)")
  • 140. 제너릭 함수 func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] { var result = ItemType[]() for i in 0..times { result += item } return result } var repeat_knock = repeat("knock", 4) var repeat_ten = repeat(10, 4) ! println("repeat_knock = (repeat_knock)") println("repeat_ten = (repeat_ten)") repeat_knock = [knock, knock, knock, knock] repeat_ten = [10, 10, 10, 10]
  • 141. 메소드 Method • 메소드는 클래스나 구조체 타입에 의존적인 함수로 클래 스와 구조체의 인스턴스는 메소드를 정의할 수 있다. • 또한 타입 자체에 관여하는 타입 메소드를 정의할 수 있 다
  • 142. 메소드 class Counter { var count = 0 func increment() { count++ } func incrementBy(amount: Int) { count += amount } func reset() { count = 0 } } let counter = Counter() // 초기 counter값은 0입니다 counter.increment() // counter값은 이제 1입니다 counter.incrementBy(5) // counter값은 이제 6입니다 counter.reset() // counter값은 이제 0입니다
  • 145. 메소드와 레이블 import Foundation ! func say(name:String, msg:String) { println("(name) say (msg)") } ! say("Henry","Hi,Swift") 함수
  • 146. 메소드와 레이블 import Foundation ! func say(name:String, msg:String) { println("(name) say (msg)") } ! say("Henry","Hi,Swift") 함수 메소드
  • 147. 메소드와 레이블 import Foundation ! func say(name:String, msg:String) { println("(name) say (msg)") } ! say("Henry","Hi,Swift") import Foundation ! class Hello { var name: String = "" func say(name:String, msg:String){ println("(name) say (msg)") } } ! var hello = Hello() hello.say("Henry", "Hi,Swift") 함수 메소드
  • 148. 메소드와 레이블 import Foundation ! func say(name:String, msg:String) { println("(name) say (msg)") } ! say("Henry","Hi,Swift") import Foundation ! class Hello { var name: String = "" func say(name:String, msg:String){ println("(name) say (msg)") } } ! var hello = Hello() hello.say("Henry", "Hi,Swift") 함수 메소드
  • 149. 메소드와 레이블 import Foundation ! func say(name:String, msg:String) { println("(name) say (msg)") } ! say("Henry","Hi,Swift") import Foundation ! class Hello { var name: String = "" func say(name:String, msg:String){ println("(name) say (msg)") } } ! var hello = Hello() hello.say("Henry", "Hi,Swift") 함수 메소드
  • 150. 메소드와 레이블 • 함수의 경우 매개변수마다 레이블 을 달 필요는 없다. • 메소드의 경우 두번째 이상의 레이 블에 대해서는 반드시 레이블을 달 아야 한다 • Objective-C의 영향을 받은 것 • 매개변수의 역할을 정확하게 알 수 있어, 에러를 줄일 수 있다.
  • 151. 메소드와 레이블 • 함수의 경우 매개변수마다 레이블 을 달 필요는 없다. • 메소드의 경우 두번째 이상의 레이 블에 대해서는 반드시 레이블을 달 아야 한다 • Objective-C의 영향을 받은 것 • 매개변수의 역할을 정확하게 알 수 있어, 에러를 줄일 수 있다. 메소드
  • 152. 메소드와 레이블 • 함수의 경우 매개변수마다 레이블 을 달 필요는 없다. • 메소드의 경우 두번째 이상의 레이 블에 대해서는 반드시 레이블을 달 아야 한다 • Objective-C의 영향을 받은 것 • 매개변수의 역할을 정확하게 알 수 있어, 에러를 줄일 수 있다. import Foundation ! class Hello { var name: String = "" func say(name:String, msg:String){ println("(name) say (msg)") } } ! var hello = Hello() hello.say("Henry", msg:"Hi,Swift") 메소드
  • 153. 메소드와 레이블 • 함수의 경우 매개변수마다 레이블 을 달 필요는 없다. • 메소드의 경우 두번째 이상의 레이 블에 대해서는 반드시 레이블을 달 아야 한다 • Objective-C의 영향을 받은 것 • 매개변수의 역할을 정확하게 알 수 있어, 에러를 줄일 수 있다. import Foundation ! class Hello { var name: String = "" func say(name:String, msg:String){ println("(name) say (msg)") } } ! var hello = Hello() hello.say("Henry", msg:"Hi,Swift") Henry say Hi,Swift 메소드
  • 154. 레이블 class Counter { var count: Int = 0 func incrementBy(amount: Int, numberOfTimes: Int) { count += amount * numberOfTimes } } let counter = Counter() Counter.incrementBy(5, numberOfTimes: 3) // Counter value is now 15
  • 155. 레이블 class Counter { var count: Int = 0 func incrementBy(amount: Int, numberOfTimes: Int) { count += amount * numberOfTimes } } let counter = Counter() Counter.incrementBy(5, numberOfTimes: 3) // Counter value is now 15 메소드의 매개변수인 numberOfTime 를 호출시의 레이블로 사용할 수 있다. 이 레이블을 통해 두번째 매개변수의 목적을 명확하게 알 수 있다
  • 156. 레이블 class Counter { var count: Int = 0 func incrementBy(amount: Int, #numberOfTimes: Int) { count += amount * numberOfTimes } } let counter = Counter() Counter.incrementBy(5, numberOfTimes: 3) // Counter value is now 15
  • 157. 레이블 class Counter { var count: Int = 0 func incrementBy(amount: Int, #numberOfTimes: Int) { count += amount * numberOfTimes } } let counter = Counter() Counter.incrementBy(5, numberOfTimes: 3) // Counter value is now 15 numberOfTime 와 같은 매개변수 이름을 레이블로 사용할 수 있다.(디폴트 매개변수명) 이때 매개변수의 이름에 #을 붙여서 이를 명시적으로 만들 수 있다-Objective-C의 문법과 동일함.
  • 158. 레이블 import Foundation ! class Hello { … func say(name:String, msg:String, ofCount n:Int){ for index in 1...n { println("(name) say (msg)") } } } … hello.say("Henry", msg:"Hi,Swift", ofCount:3)
  • 159. 레이블 import Foundation ! class Hello { … func say(name:String, msg:String, ofCount n:Int){ for index in 1...n { println("(name) say (msg)") } } } … hello.say("Henry", msg:"Hi,Swift", ofCount:3) 위 메소드의 ofCount는 레이블이며, n은 매개변수이다 레이블과 매개변수는 다를 수 있다
  • 160. 함수 레이블(익명 레이블) import Foundation ! class Hello { … func say(name:String, _ msg:String, _ n:Int){ for index in 1...n { println("(name) say (msg)") } } } … hello.say("Henry","Hi,Swift", 3)
  • 161. 함수 레이블(익명 레이블) 레이블 대신 _(언더바)를 사용하여 레이블을 생력할 수 있다. import Foundation ! class Hello { … func say(name:String, _ msg:String, _ n:Int){ for index in 1...n { println("(name) say (msg)") } } } … hello.say("Henry","Hi,Swift", 3)
  • 162. self 속성 • self는 내재적인 속성으로 모든 인스턴스가 가지고 있다. • self는 인스턴스 자체를 지시한다 func increment() { self.count++ }
  • 163. self 속성 • self는 내재적인 속성으로 모든 인스턴스가 가지고 있다. • self는 인스턴스 자체를 지시한다 func increment() { self.count++ } 위와같이 메소드에 self를 반드시 달 필요는 없으며 self. 가 없을 경우 Swift는 현재 인스턴스의 메소드나 속성을 가리키는 것으로 가정한다
  • 165. self 속성 struct Point { var x = 0.0, y = 0.0 func isToTheRightOfX(x: Double) -> Bool { return x > x } }
  • 166. self 속성 struct Point { var x = 0.0, y = 0.0 func isToTheRightOfX(x: Double) -> Bool { return x > x } } self.
  • 167. self 속성 struct Point { var x = 0.0, y = 0.0 func isToTheRightOfX(x: Double) -> Bool { return x > x } } self. 위와 같은 메소드에서 x > x라고 할 경우 x가 매개변수 x인지 Point의 멤버 변수 x인지를 알 수 없다 self.x > x 를 통해서 self.x 는 멤버변수 x임을 알 수 있다
  • 168. mutating 키워드 struct Point { var x = 0.0, y = 0.0 mutating func moveByX(deltaX: Double, y deltaY: Double) { x += deltaX y += deltaY } } var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveByX(2.0, y: 3.0) println("The point is now at ((somePoint.x), (somePoint.y))")
  • 169. mutating 키워드 struct Point { var x = 0.0, y = 0.0 mutating func moveByX(deltaX: Double, y deltaY: Double) { x += deltaX y += deltaY } } var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveByX(2.0, y: 3.0) println("The point is now at ((somePoint.x), (somePoint.y))") 메소드 moveByX를 통해 인스턴스의 속성을 변경시키고자 할 경우 mutating 키워드를 사용하여 인스턴스의 속성 x, y를 변경
  • 170. 용어정리 • C나 C++와 같은 프로그래밍 언어에서는 일반적으로 클 래스의 인스턴스를 객체(오브젝트)라고 한다. • Swift는 다른 언어와 달리 클래스와 구조체의 기능이 유 사하다. 클래스와 구조체의 인스턴스 모두 함수를 가질 수 있다. • 이 때문에 Swift에서는 객체라는 용어보다 일반적인 인스턴스라는 용어를 사용한다
  • 171. 클래스와 구조체 • 클래스(class)와 구조체(struct)는 모두 범용적이고 가 변적인 프로그램의 구성 성분이다 • 클래스와 구조체 모두 상수, 변수, 함수를 가지는데, 이때 문법은 동일하다.
  • 172. 클래스와 구조체의 유사성 • 값을 저장할 속성을 정의한다 • 기능을 위한 메소드를 정의한다 • subscript 구문을 사용하여 이들의 값에 접근할 수 있도록 subscript를 제공한다 • 초기상태를 설정하기 위한 초기설정자를 제공한다 • 기본 구현내용을 확장하기 위한 기능을 제공한다 • 어떤 표준적인 기능을 제공하는 프로토콜을 따른다
  • 173. 클래스와 구조체의 차이점 • 클래스는 구조체가 가지지 못한 다음 기능을 가진다 • 한 클래스의 특성을 상속받는 기능이 가능하다 • 클래스 인스턴스를 형을 검사하고 번영하여 런타임시 에 형변환을 할 수 있다 • 할당된 임의의 리소스를 해지하는 deinitializer를 가 진다 • 클래스 인스턴스는 참조 카운터를 하나이상 허용한다
  • 174. 정리 • Swift의 함수는 C언어의 함수보다 간결하면서도 쉽다 • 튜플타입을 사용하여 함수를 유연성있게 사용할 수 있다 • Objective-C의 레이블 기능을 메소드에서도 사용할 수 있도록 하여 호환성을 확보하였다 • 제너릭 타입은 자료형에 무관한 유연한 코딩이 가능하다 • Swift의 철학은 간결하면서도 안전한 프로그램을 추구한 다