Resources in Swift
1) Variables
var a = 5 // ๋ณ์
Swift
๋ณต์ฌ
ํน์ DataType์ผ๋ก ์ง์ ํ๊ณ ์ถ์ผ๋ฉด ์๋์ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
์ด๋ DataType์ ๋ชจ๋ ์๊ธ์๋ ๋๋ฌธ์์ด๋ค!
var data: Int = 1234
Swift
๋ณต์ฌ
โข
option + click ์ ํตํด์ DataType์ ์ ์ ์๋ค
2) Constants
let a = 3 // ์์
Swift
๋ณต์ฌ
3) Commenting
// single line commenting
/* multi
line
commenting */
Swift
๋ณต์ฌ
4) Range Convention
a...b -> Closed Range
a..<b -> Half Open Range
...b -> One Sided Range
Swift
๋ณต์ฌ
5) Optionals
Swift๋ฅผ ๊ณต๋ถํ๋ค๋ณด๋ฉด ํน์ ํ์
๋ค์ ? ํน์ !๊ฐ ๋ฐ๋ผ ๋์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ด ๋, ์ด ๊ธฐํธ๋ค์ด ๋ฐ๋ก Optional์ ๋ณด๋ฅผ ๋ํ๋ธ๋ค.
DataType <ใฑ
var hardness: String?
ใด> Optional
Swift
๋ณต์ฌ
Swift๋ DataType์ ๊ฐ์ง ๊ธฐ๋ณธ ๋ณ์์ nil(C๋ก ์น๋ฉด null)์ ๋ฃ์ ์ ์๋ค. ์๊ฐํด๋ณด๋ฉด ๋ง๋ ๋ง์ด๋ค. nil์ String์ด๋ผ๊ณ ํ ์ ์๊ธฐ ๋๋ฌธ... ์ฆ, Optional์ด ํด์ฃผ๋ ์ญํ ์ ์๋์ ๊ฐ๋ค
์ด ๋ณ์์๋ ๊ฐ์ด ์์ ์๋, ์์ ์๋ ์๋ค!
๊ทธ๋์ Swift์์๋ ํ์
๋ค์? ๋ฅผ ๋ถ์ด๋ฉด์ ๊ฐ์ด ์์ ์๋ ์๋ค๋ ์ํฉ์ ์๋ ค์ฃผ๋ ๊ฒ์ด๋ค.
Example
var player1Username: String = nil // error
var player1Username: String? = nil // OK
var player1Username: String? // OK (์์ ๋์ผ)
player1Username = "username"
Swift
๋ณต์ฌ
๋ง์ฝ ์ ๋ณ์๋ฅผ ๊ทธ๋ฅ ์ถ๋ ฅํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
print(player1Username)
Swift
๋ณต์ฌ
์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฒฐ๊ณผ๋ "username"์ธ๋ฐ Optional() ์ด๋ผ๋ ๋ธ๋ก ์์ ๊ฐ์ด ๋์ค๊ณ ์๋ค. ์ ์ด๋ฐ ๊ฒฐ๊ณผ๊ฐ ์๊ธฐ๋ ๊ฑธ๊น?
Wrapping
๊ทธ ์ด์ ๋ ๋ฐ๋ก Wrapping์ด๋ค. Optional ํ์
์ ๊ธฐ๋ณธ์ ์ผ๋ก wrap๋์ด ์๋ ์ด๋ค. ์ฆ, Optional๋ก ์ ์ธ๋ ๋ณ์๋ค์ ๊ฐ์ด ์๋ ๊ฒ์ธ์ง, nil์ธ ๊ฒ์ธ์ง wrap๋์ด ์์ด์ ๋ชจ๋ฅด๋ ์ํ์ด๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์(์ปดํ์ผ๋ฌ ์
์ฅ์์๋ ๋ณ์๊ฐ nil์ผ ์๋ ์๊ธฐ ๋๋ฌธ์) wrap๋ ์ํ์์๋ ์ค๋ นย ๋ณ์์ value๊ฐ์ด ์๋ค๊ณ ํ๋๋ผ๋ ๋ฐ๋ก value๊ฐ ์ถ๋ ฅ๋์ง ์๋๋ค.(Swift 3.0์์ ๋ฐ์๋ ์ฌํญ)ย
var optionalString: String? = โHelloโ
print(optionalString)
// ์ถ๋ ฅ ๊ฒฐ๊ณผ: Optional(โHelloโ)
Swift
๋ณต์ฌ
์ด ๊ฒฝ์ฐ, optionalString์ด nil์ผ ์๋ ์๊ธฐ ๋๋ฌธ์, ๊ฒฐ๊ณผ๊ฐ โHelloโ๊ฐ ์ถ๋ ฅ๋์ง ์๊ณ ,ย Optional(โHelloโ)ย ๊ฐ ์ฝ์์ฐฝ์ ์ถ๋ ฅ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ๊ทธ๋ฅ ์ํ๋ ๊ฐ๋ง ์ถ๋ ฅํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
๋ฐฉ๋ฒ์ ๊ฐ๋จํ๋ค ๋ฐ๋ก Forced Unwrapping์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
print(optionalString!)
ใด> Forced Unwrapping
Swift
๋ณต์ฌ
์ด ๋, optionalString์ด nil์ด๋ฉด error๊ฐ ๋ ์ ์๋ค.
๋ฐ๋ผ์ if๋ฌธ์ ํตํด ์์ ํ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
if optionalString != nil{
print(optionalString!)
}
Swift
๋ณต์ฌ
5 - 1) Optional Binding, Chaining, and the Nil Coalescing Operator
ํญ์ ์๋ก ์ ์ธํ ๋๋ง๋ค !๋ถ์ฌ์ผํจ.. ๋ถํธ
1.
optional binding
if let saefOptional = optional {
safeOptional
}
C
๋ณต์ฌ
2. nil coalescing operatior
optional ?? defalutValue
let text: String = myOptional ?? "i am default"
C
๋ณต์ฌ
optional class or struct?
3. optional chaining
nil์ด ์๋๋๋ง ์คํํด๋ผ!
optional?.property
optional?.method()
C
๋ณต์ฌ
6) Null? Nil? ๋ฌด์์ด ๋ค๋ฅผ๊น?
์ค์ํํธ๋ฅผ ํ๋ค๋ณด๋ฉด null์ด ์๋๋ผ nil์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ฐ๋ก ๋์น์ฑ ์ ์์ ๊ฒ์ด๋ค. ์ฒ์์๋ ๋๊ฐ์ ํํ์ ํ๊ธฐ๋ง ๋ค๋ฅด๊ฒ ํด์ ์ฐ๋๋ณด๋ค ํ๊ณ ์๊ฐํ๋ค๊ฐ ๊ฐ์๊ธฐ ๊ถ๊ธํด์ ธ์ ์ฐพ์๋ณด๊ฒ ๋์๋ค.
๊ฐ๋จํ๊ฒ ๋งํ๋ฉด, ์๋ Objective-C ์์ null๊ณผ nil์ ๋ฐ๋ก ์ฌ์ฉํ๋ฉด์ ๊ทธ ์์ฌ๋ก ์๊ธด ๊ฒ์ด๋ผ๊ณ ํ๋ค. swift์์๋ ํฌ์ธํฐ์ ๊ฐ๋
์ด ์๊ธฐ ๋๋ฌธ์ nil๋ง ๊ฐ์ ธ์จ ๊ฒ์ด๋ผ๊ณ ..(obc์์ null์ ํฌ์ธํฐ์ ๋ถ์ฌ๋ฅผ ๋ํ๋ธ๋ค)
์ฆ, Swift์์์ nil์ ํฌ์ธํฐ๊ฐ ์๋๋ผ ํน์ ํ์
์ ๋ํ ๊ฐ์ ๋ถ์ฌ๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด๋ค.
7) Immutability!!
โข
let : immutable
โข
var : mutable
โข
struct inside : immutable
// error!!!
let name: String
var citizens: [String]
var resources: [String: Int]
...
func harvestRice() {
resources["Rice"] = 100
}
}
Swift
๋ณต์ฌ
// OK!! can change state of struct
mutating func harvestRice() {
resources["Rice"] = 100
}
Swift
๋ณต์ฌ
โข
struct outside : mutable
Type in Swift
1) String
"This is text!!"
Swift
๋ณต์ฌ
string concatenation
let password = "a" + "b"
Swift
๋ณต์ฌ
2) Int
์ ์ ํ์
, 64๋นํธ ์ ์ํ
var intValue: Int = 100
var intValue: Int = 100.1 // error!
Swift
๋ณต์ฌ
2 - 1) UInt
์์ ์ ์ ํ์
, 64๋นํธ ์์ ์ ์ํ
var uintValue: UInt = 100
var uintValue: UInt = -100 // error!
Swift
๋ณต์ฌ
3) Double
์ค์ ํ์
, 64๋นํธ ๋ถ๋ ์์ํ
var doubleValue: Double = 3.14
Swift
๋ณต์ฌ
4) Float
์ค์ ํ์
, 32๋นํธ ๋ถ๋ ์์ํ
var floatValue: Float = 3.14
Swift
๋ณต์ฌ
5) Bool
true์ false๋ง์ ๊ฐ์ผ๋ก ๊ฐ์ง๋ ํ์
var boolValue: Bool = true
var boolValue: Bool = false
boolValue = 0 // error!
boolValue = 1 // error!
Swift
๋ณต์ฌ
Data Structure in Swift
1) Array
var name = [data, data, data ... ][num]
Swift
๋ณต์ฌ
var numbers = [45, 73, 195, 53]
var computedNumbers = numbers
for i in 0...3{
print(computedNumbers[i])
}
Swift
๋ณต์ฌ
2) Dictionary
var dict = ["key1" : val1, "key2" : val2]
var dict : [String : Int ] = ["String" : Int, "String" : Int]
Swift
๋ณต์ฌ
3) Struct
struct Town{
let name = "42land"
var citizens = ["4", "2"]
var resources = ["Test" : 100, "Subject" : 42, "Cadet" : 1000]
}
Swift
๋ณต์ฌ
init ํจ์ ์ฌ์ฉํด์ struct initialize!
import UIKit
struct Town{
let name: String
var citizens: [String]
var resources: [String: Int]
init(townName: String, people: [String], stats: [String: Int]) {
name = townName
citizens = people
resources = stats
}
func fortify() {
print("Defences increased!")
}
}
var anotherTown = Town(townName: "SU", people: ["1","2"], stats: ["coconuts": 100])
anotherTown.citizens.append("3")
print(anotherTown.citizens)
C
๋ณต์ฌ
self ํค์๋๋ก ๊ตฌ์กฐ์ฒด ๋ด๋ถ ๋ณ์ ๋ํ๋ด๊ธฐ
struct Town{
let name: String
var citizens: [String]
var resources: [String: Int]
init(name: String, citizens: [String], resources: [String: Int]) {
self.name = name
self.citizens = citizens
self.resources = resources
}
func fortify() {
print("Defences increased!")
}
}
C
๋ณต์ฌ
4) Classes
ํด๋์ค ์์ฑํ ๋์๋ ๋ฌด์กฐ๊ฑด ํ์ผ ์ด๋ฆ๊ณผ ๋์ผํ๊ฒ ์ง์ด์ผ ํ๋ค.
5) Struct vs Classes
struct : unable to inherit
ํด๋์ค๋ ๋ฌด์กฐ๊ฑด initํจ์๊ฐ ์์ด์ผํ๋ค.
class Enemy {
var health: Int
init(health: Int) {
self.health = health
}
}
Swift
๋ณต์ฌ
ํด๋์ค๋ ๋ํผ๋ฐ์ค๊ฐ ๋ณต์ฌ๋๋ค.
// ๋๊ฐ์ ๋ค๋ฅธ ๊ฐ์ฒด ์์ฑ
let ske1 = Enemy(health: 100, attackStregth: 10)
let ske2 = Enemy(health: 100, attackStregth: 10)
// ํ๋์ ๊ฐ์ฒด์ ๋ ๊ฐ์ ๋ณ์๊ฐ ์ฐธ์กฐ
let ske1 = Enemy(health: 100, attackStregth: 10)
let ske2 = ske2
Swift
๋ณต์ฌ
์์ฑ์๋ฅผ ๋ฐ๋ก ์จ์ผ ๋ค์ ๋ง๋ค์ด์ง!!!
๊ตฌ์กฐ์ฒด๋?
let -> ๋ณ๊ฒฝ๋ถ๊ฐ๋ฅ
but ๊ตฌ์กฐ์ฒด๋ ์์ ํ ๋ณต์ฌ๋จ
Swift
๋ณต์ฌ
struct : passed by value, immutable
class : passed by reference, mutable, inheritance
Structures vs Classes
Swift๋ฅผ ๊ณต๋ถํ๋ฉด์ struct๋ฅผ ์ฐ๋ ๊ฒ์ ๋ณด๊ณ ์กฐ๊ธ ์์ํ๋ค. class๊ฐ ์๋๋ฐ ๊ตณ์ด struct๋ฅผ ์ฌ์ฉํด์ผํ๋? ์ฒ์ ์ฌ์ฉ๋ฒ์ ๋ฐฐ์ฐ๋ฉด์๋ ๋๋ฌด๋๋ ๋น์ทํ ํ์ฉ๋ฒ์ ์๋ฌธ์ ํ์ด์ ๋์ฒด ์ struct์ class๋ฅผ ๋๋ ์ ์ฌ์ฉํ๋์ง ์ฐพ์๋ณด์๋ค.
์ด ํฌ์คํธ๋ Apple์ Swift Documents๋ฅผ ๋ณด๊ณ ์์ฑํ์์ต๋๋ค
Comparing Structures and Classes
Both can:
โข
Define properties to store values
โข
Define methods to provide functionality
โข
Define subscripts to provide access to their values using subscript syntax
โข
Define initializers to set up their initial state
โข
Be extended to expand their functionality beyond a default implementation
โข
Conform to protocols to provide standard functionality of a certain kind
๊ฐ๊ฐ์ ๊ธฐ๋ฅ๋ค์ ๋ํ ์ถ๊ฐ์ ์ธ ์ค๋ช
์ ์๋ ํ์ด์ง์์ ๋ณผ ์ ์๋ค. Classes can:
class๊ฐ struct๋ณด๋ค ๋ ๋ง์ ๊ธฐ๋ฅ์ ๋ด๊ณ ์๊ธฐ ๋๋ฌธ์, class๊ฐ ์ด๋ค ๊ฒ์ ๋ ํ ์ ์๋์ง ์์๋ณด์.
โข
Inheritance enables one class to inherit the characteristics of another.
โข
Type casting enables you to check and interpret the type of a class instance at runtime.
โข
Deinitializers enable an instance of a class to free up any resources it has assigned.
โข
Reference counting allows more than one reference to a class instance.
๊ฐ๊ฐ์ ๊ธฐ๋ฅ๋ค์ ๋ํ ์ถ๊ฐ์ ์ธ ์ค๋ช
์ ์๋ ํ์ด์ง์์ ๋ณผ ์ ์๋ค. Apple๊ณต์ ๋ฌธ์์์ ๋ ๊ตฌ์กฐ ์ค ํ๋๋ฅผ ๊ณ ๋ฅด๋ ๊ธฐ์ค์ ๊ฐ๋จํ๊ฒ ์ ์ํด ๋์๋ค.
The additional capabilities that classes support come at the cost of increased complexity. As a general guideline, prefer structures because theyโre easier to reason about, and use classes when theyโre appropriate or necessary.
์ผ๋ฐ์ ์ธ ์ํฉ์์๋(ํน๋ณํ ๋ณต์กํ ๊ธฐ๋ฅ์ด ํ์์๋) struct๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๊ณ , ๋ง์ฝ ํ์ํ๋ค๋ฉด class๋ฅผ ์ฌ์ฉํ๋ผ๊ณ ๋์์๋ค. ์๋ฌด๋๋ class๋ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ด ๋ง๊ธฐ ๋๋ฌธ์ ๋ง์ ์ฝ๋๋ค์ ๋ด๋ ํ๋ก์ ํธ์์ ๋ฌด๋ถ๋ณํ ์ฌ์ฉ๋ณด๋ค๋ ํ์ํ ์ํฉ์ ์ ์ ํ ์ฌ์ฉํ๋๊ฒ ์ข์ ๊ฒ ๊ฐ๋ค.
Definition Syntax
์ด์ ๊ฐ๊ฐ ์ด๋ป๊ฒ ์ ์ธํ๋์ง ์ดํด๋ณด์
// structures
struct SomeStructure {
// Do Something
}
// classes
class SomeClasses: SomeParentsClasses {
// Do Something
}
Swift
๋ณต์ฌ
Whenever you define a new structure or class, you define a new Swift type. Give types UpperCamelCase names (such as SomeStructure and SomeClass here) to match the capitalization of standard Swift types (such as String, Int, and Bool). Give properties and methods lowerCamelCase names (such as frameRate and incrementCount) to differentiate them from type names.
์ด ๋, type์ uppercamelcase๋ก, ์์ฑ์ด๋ ๋ฉ์๋๋ lowercamelcase๋ก ์ ๋ ๊ฒ์ ๊ท์น์ผ๋ก ํ๋ค. ๋ํ struct, class์ ์ด๋ฆ์ ๋๋๋ก์ด๋ฉด ํ์ผ์ ์ด๋ฆ๊ณผ ๋์ผํ๊ฒ ๋ง๋ ๋ค.
ex. Student.swift โ class Student
Structure and Class Instances
์ ์ธํ ๊ตฌ์กฐ๋ค์ ๊ฐ์ฒด๋ ์๋์ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
let someStruct = SomeStructure()
let someClass = SomeClasses()
Swift
๋ณต์ฌ
Structures and Enumerations Are Value Types
A value type is a type whose value is copied when itโs assigned to a variable or constant, or when itโs passed to a function.
C++์ ๊ณต๋ถํ๋ค๋ณด๋ฉด Call by Value, Call by Reference์ ๊ฐ์ ๊ฒ์ ์์ฃผ ๋ณผ ์ ์๋ค. ํด๋น ๊ฐ๋
์ ์ตํ๊ณ ์๋ค๋ฉด ์๋ ๋ถ๋ถ์ ์ด๋ ต์ง ์๊ฒ ์ดํดํ ์ ์์ ๊ฒ์ด๋ค.
Swift์ structures๋ Value Type์ด๋ค. ๊ฐ๋จํ๊ฒ ์์๋ฅผ ํตํด์ ์ดํด๋ณด์. ์ฐ์ BoxSize๋ผ๋ struct๋ฅผ ๋ง๋ค์๋ค. BoxSize๋ height์ width๋ฅผ ๊ฐ์ง๊ณ ์๋ค. Main์์ BoxSize์ instance๋ฅผ ์์ฑํด hd๋ผ๋ ๋ณ์์ ๋ด์๋ณด์.
struct BoxSize {
var height: Int = 1080
var width = 1920 // ํ์
์ ์๋ตํ๋ฉด ํ์
์ถ๋ก ์ ํด์ค๋ค
}
Swift
๋ณต์ฌ
// in Main
var hd = BoxSize()
print("height = \(hd.height)")
print("width = \(hd.width)")
Swift
๋ณต์ฌ
์ด ๋, hd๋ BoxSize๋ก ์์ฑํ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ height์ width๋ฅผ ๊ณ ์ ์ ์ผ๋ก ๊ฐ์ง๊ณ ํ์ด๋๋ค.
์ด์ ์ฐ๋ฆฌ๋ ์ฌ๊ธฐ์ cinema๋ผ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ hd๋ฅผ ๋ณต์ฌํด๋ณด์. ๊ทธ๋ฆฌ๊ณ cinema์ width property๋ฅผ 2048๋ก ์์ ํด๋ณด์
// in Main
...
var cinema = hd
cinema.width = 2048
Swift
๋ณต์ฌ
๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด cinema์ width์์ฑ๋ง ๋ฐ๋ ๊ฒ์ ์ ์ ์๋ค. cinema๋ hd์ ๋ชจ๋ value๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํด์ ๊ฐ์ ธ๊ฐ ์์ ํ ์๋ก์ด ํ๋์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๊ฒ์ ์ ์ ์๋ค.
๊ทธ๋ ๋ค๋ฉด Class๋ ์ด๋จ๊น?
Classes Are Reference Types
Unlike value types, reference types are not copied when theyโre assigned to a variable or constant, or when theyโre passed to a function. Rather than a copy, a reference to the same existing instance is used.
์์ ์ค๋ช
์์ ๋ณผ ์ ์๋ฏ์ด Classes๋ ํจ์์ ์ ๋ฌ๋๊ฑฐ๋ ๋ณ์์ ํ ๋น๋ ๋, ๋ณต์ฌ ๋์ ๊ธฐ์กด ๊ฐ์ฒด์ ๋ํ ์ฐธ์กฐ๊ฐ ์ฌ์ฉ๋๋ค. struct์์ ์ฌ์ฉํ ์์๋ฅผ ๋์ผํ๊ฒ class๋ก ๋ฐ๊พธ์ด ๋ณด์
class BoxSize {
var height = 1080
var width = 1920
}
Swift
๋ณต์ฌ
// in Main
var hd = BoxSize()
var cinema = hd
cinema.width = 2048
print("hd height = \(hd.height)")
print("hd width = \(hd.width)")
print("cinema height = \(cinema.height)")
print("cinema width = \(cinema.width)")
Swift
๋ณต์ฌ
hd๋ฅผ ๋ณต์ฌํด์ cinema์ ๋ฃ๊ณ cinema์ width๊ฐ์ ๋ฐ๊ฟจ์ ๋ฟ์ธ๋ฐ hd์ cinema์ ๊ฐ์ด ๋ชจ๋ ๋ฐ๋ ๊ฒ์ ์ ์ ์๋ค. class๋ ์์์ ๋งํ๋ฏ์ด ๊ฐ์ ๋ณต์ฌํด ๋ฃ๋๊ฒ ์๋๋ผ ๊ฐ์ฒด์ ๋ํ ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค. ์๋์ ๊ทธ๋ฆผ์ด ์ดํด๋ฅผ ๋์์ค ๊ฒ์ด๋ค.
Identity Operators
ํด๋์ค๋ reference type์ด๊ธฐ ๋๋ฌธ์ ํ๋์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ์ฌ๋ฌ๊ฐ์ ๋ณ์๊ฐ ์กด์ฌํ ์ ์๋ค. ์ด ๋, ๊ฐ ๋ณ์๋ค์ด ๋์ผํ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋์ง ํ์ธํ๋ ๋๊ตฌ๊ฐ ์์ผ๋ฉด ์ ์ฉํ ๊ฒ์ด๋ค! ์๋ ์ฐ์ฐ์๋ฅผ ๋ณด์
=== // Identical to
!== // Not Identical to
Swift
๋ณต์ฌ
Identical์ Equal๊ณผ ๋ค๋ฅด๋ค. Identical์ ๋ ๋ณ์๊ฐ ๋์ผํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํฌ๋ ๊ฒ์ ๋ํ๋ธ๋ค. Equal์ ๋ํ ์ ์๋ ๊ฐ๋ฐ์์ ์ญํ ์ด๋ค.
Functions in Swift
1) Parameters
ํจ์์ parameter๋ฅผ ๋ฃ์ผ๋ ค๋ฉด ์๋์ ๊ฐ์ ํํ๋ก ๋ฃ์ด์ค๋ค.
func printSomething(parameter: DataType) {
// Do something
}
Swift
๋ณต์ฌ
ํด๋น ํจ์๋ฅผ ํธ์ถํ ๋์๋ ๋์ผํ ํํ๊ฐ ๋์ด์ผํ๋ค
printSomething(parameter: Contetns)
Swift
๋ณต์ฌ
2) Functions with Outputs
func getSomething(money: Int) -> Int {
// Do something
return value
}
Swift
๋ณต์ฌ
3) IF / Else
if something == Value{
// do
} else {
// do
}
Swift
๋ณต์ฌ
Example
func loveCalculator() {
let loveScore = Int.random(in: 0...100)
if loveScore == 100{
print("You love each other")
} else {
print("You will alone...")
}
}
loveCalculator()
Swift
๋ณต์ฌ
4) Switch / Case
switch value{
case:
//code
case:
//code
default:
//code
}
Swift
๋ณต์ฌ
Example
func loveCalculator() {
let loveScore = Int.random(in: 0...100)
switch loveScore {
case 81...100:
print("You love each other")
case ...40
print("something")
default:
print("You love each other")
}
}
loveCalculator()
Swift
๋ณต์ฌ













