하루를살자

Swift Basics - Playground(Xcode), Data type, Function 본문

Swift

Swift Basics - Playground(Xcode), Data type, Function

Kai1996 2021. 12. 17. 00:00

Basic Flow of Application

reference : https://www.youtube.com/watch?v=F2ojC6TNwws

Xcode - Playground 

It provides a simple tool called "playground" which enables to practice Swift. It also has advantages on as following; 

  • REPL - Read, Eval, Print, Loop 가능
  • 프로젝트 생성 불필요 
  • 코드 작성/변경 - 바로 결과 확인 가능 

Swift

Data-type 

  • String : "Hello"
  • Int : 1,2, 
  • Double : 0.2, 1.2
  • Bool : ture, false 
  • var : mutable variable 
  • let : immutable variable 
  • uses Camel case 
  •  

Function

- Basic call 

 

1. func functionName () {}

 --> type 'func' in front of the name of the function 

 

2. Parameters 

--> Swift has various ways to assign parameters of a function, and depending on the way it is stated, it is considered as a different function even if the name of the function is duplicated with the others.

Example) 

func myFunc(){

	let a = 10 
	let b = 10 
    print(a+b)

}

func myFunc(a:Int , b:Int){

    print(a-b)

}


func myFunc(firstNum a:Int, secondNum b:Int){

    print(a*b)

}

myFunc()
myFunc(a: 10, b: 20)
myFunc(firstNum: 5, secondNum: 4)

3. return 

Declare the type of return followed by '->' after parameter declaration.

func myFunc(firstNum a:Int, secondNum b:Int) -> Int{

    return a*b

}

 

 

 

'Swift' 카테고리의 다른 글

Swift 고차함수  (0) 2022.02.05
Swift - Collection Types  (0) 2022.01.01
Swift - Enum  (0) 2021.12.31
Swift - Optionals  (0) 2021.12.21
SwiftUI Architecture - MVVM (Model, View, and ViewModel)  (0) 2021.12.20
Comments