Search
moon
sun

Swift Deep Dive

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
๋ณต์‚ฌ