๋ฐ์ํ
๋ฉ์๋(Methods)
ํน์ ํ์ ์ ํด๋์ค, ๊ตฌ์กฐ์ฒด, ์ด๊ฑฐํ๊ณผ ๊ด๋ จ๋ ํจ์
์ธ์คํด์ค ๋ฉ์๋ (Instance Methods)
- ํน์ ํด๋์ค, ๊ตฌ์กฐ์ฒด, ์ด๊ฑฐํ์ ์ธ์คํด์ค์ ์ํ ๋ฉ์๋
- ์ธ์คํด์ค ๋ด์ ๊ฐ์ ์ ์ดํ๊ฑฐ๋ ๋ณ๊ฒฝ ๊ฐ๋ฅ
- ์ธ์คํด์ค๊ฐ ์ํ ํน์ ํ์ ์ ์ธ์คํด์ค์์๋ง ์คํ ๊ฐ๋ฅ
class Counter {
var count = 0
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
let counter = Counter()
// ์ด๊ธฐ count ๊ฐ์ 0์
๋๋ค.
counter.increment()
// count ๊ฐ์ด 1๋ก ๋ณ๊ฒฝ ๋์ต๋๋ค.
counter.increment(by: 5)
// count ๊ฐ์ ํ์ฌ 6์
๋๋ค.
counter.reset()
// count ๊ฐ์ 0์ด ๋ฉ๋๋ค.
self ํ๋กํผํฐ
- ๋ชจ๋ ํ๋กํผํฐ๋ ์์์ ์ผ๋ก ์ธ์คํด์ค ์์ฒด๋ฅผ ์๋ฏธํ๋ self ๋ผ๋ ํ๋กํผํฐ๋ฅผ ๊ฐ๊ณ ์์
- ์ธ์คํด์ค ๋ฉ์๋ ์์์ self ํ๋กํผํฐ๋ฅผ ์ด์ฉํด ์ธ์คํด์ค ์์ฒด๋ฅผ ์ฐธ์กฐํ๋๋ฐ ์ฌ์ฉ ๊ฐ๋ฅ
- ๋ณดํต self ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์๋ ์ธ์คํด์ค์ ๋ฑ๋ก๋ ๋ฉ์๋๋ ํ๋กํผํฐ๋ฅผ ํธ์ถํ๋ฉด ํ์ฌ ์ธ์คํด์ค์ ๋ฉ์๋๋ ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ์๋์ผ๋ก ๊ฐ์
- ์์ธ์ ์ผ๋ก ์ธ์ ์ด๋ฆ์ด ํ๋กํผํฐ ์ด๋ฆ๊ณผ ๊ฐ์ ๊ฒฝ์ฐ ํ๋กํผํฐ์ ์ ๊ทผํ๊ธฐ ์ํด self ํค์๋๋ฅผ ์ฌ์ฉํด์ผํจ
struct Point {
var x = 0.0, y = 0.0
func isToTheRightOf(x: Double) -> Bool {
return self.x > x // self.x๋ฅผ ์ด์ฉํด ํ๋กํผํฐ x์ ์ธ์ x๋ฅผ ๊ตฌ๋ถ
}
}
let somePoint = Point(x: 4.0, y: 5.0)
if somePoint.isToTheRightOf(x: 1.0) {
print("This point is to the right of the line where x == 1.0")
}
// "This point is to the right of the line where x == 1.0" ์ถ๋ ฅ
์ธ์คํด์ค ๋ฉ์๋ ๋ด์์ ๊ฐ ํ์ ๋ณ๊ฒฝ
- ๊ตฌ์กฐ์ฒด์ ์ด๊ฒจํ์ ๊ฐ ํ์ ์ด๊ธฐ ๋๋ฌธ์ ์ธ์คํด์ค ๋ฉ์๋ ๋ด์์ ๊ฐ ํ์ ์ ํ๋กํผํฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์์
- ํ์ง๋ง ๋ฉ์๋์ mutating ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ์๋์ ๊ณ์ฐ์ด ๋๋ ํ ์๋ณธ ๊ตฌ์กฐ์ฒด์ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฎ์ด ์จ์ ๊ฐ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
- ์์ let์ผ๋ก ์ธ์คํด๋ฅผ ์ ์ธํ๋ฉด mutating ์ ์ธ๊ณผ ์๊ด ์์ด ๋ฉ์๋๋ก ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
x += deltaX
y += deltaY
}
}
var somePoint = Point(x: 1.0, y: 1.0)
somePoint.moveBy(x: 2.0, y: 3.0)
print("The point is now at (\\(somePoint.x), \\(somePoint.y))")
// "The point is now at (3.0, 4.0)" ์ถ๋ ฅ
let fixedPoint = Point(x: 3.0, y: 3.0)
fixedPoint.moveBy(x: 2.0, y: 3.0)
// let์ผ๋ก ์ ์ธํด์ ๊ฐ ๋ณ๊ฒฝ ์๋์ ์๋ฌ ๋ฐ์!
Mutating ๋ฉ์๋ ๋ด์์ self ํ ๋น
- Mutating๋ฉ์๋์์ selfํ๋กํผํฐ๋ฅผ ์ด์ฉํด ์์ ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์์
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
self = Point(x: x + deltaX, y: y + deltaY)
}
}
enum TriStateSwitch {
case off, low, high
mutating func next() {
switch self {
case .off:
self = .low
case .low:
self = .high
case .high:
self = .off
}
}
}
var ovenLight = TriStateSwitch.low
ovenLight.next()
// ovenLight ๊ฐ์ .high
ovenLight.next()
// ovenLight ๊ฐ์ .off
ํ์ ๋ฉ์๋(Type Methods)
- ํน์ ํ์ ์์ฒด์์ ํธ์ถํด ์ฌ์ฉ
- ํ์
๋ฉ์๋๋ ๋ฉ์๋ ํค์๋ func ์์ static ํน์ class ํค์๋๋ฅผ ์ถ๊ฐํด ์ฌ์ฉ
- static ๋ฉ์๋: ์๋ธํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ ํ ์ ์๋ ํ์ ๋ฉ์๋
- class ๋ฉ์๋: ์๋ธํด๋์ค์์ ์ค๋ฒ๋ผ์ด๋ ํ ์ ์๋ ํ์ ๋ฉ์๋
- ํ์ ๋ฉ์๋๋ ์ธ์คํด์ค ๋ฉ์๋์ ๊ฐ์ด ์ (.) ๋ฌธ๋ฒ์ผ๋ก ํธ์ถ
- ํ์ ๋ฉ์๋ ์์์ self ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ ์์ ์ ์๋ฏธ
class SomeClass {
class func someTypeMethod() {
// ํ์
๋ฉ์๋ ๊ตฌํ
}
}
SomeClass.someTypeMethod() // ํ์
๋ฉ์๋ ํธ์ถ!
- ํ์ ๋ฉ์๋ ์์์ ๋ค๋ฅธ ํ์ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
struct LevelTracker {
static var highestUnlockedLevel = 1
var currentLevel = 1
static func unlock(_ level: Int) {
if level > highestUnlockedLevel { highestUnlockedLevel = level }
}
static func isUnlocked(_ level: Int) -> Bool {
return level <= highestUnlockedLevel
}
@discardableResult //"์ด ๋ฉ์๋์ ๋ฐํ๊ฐ์ ์ฌ์ฉํ์ง ์์๋ ๊ด์ฐฎ๋ค"
mutating func advance(to level: Int) -> Bool {
if LevelTracker.isUnlocked(level) {
currentLevel = level
return true
} else {
return false
}
}
}
- @discardableResult: ์ฝ๋์ ๊ฐ๋
์ฑ๊ณผ ์๋๋ฅผ ๋ช
ํํ๊ฒ ์ ๋ฌํ๊ธฐ ์ํด ์ฌ์ฉ
- ๋ฉ์๋๋ฅผ ํธ์ถํ ํ ๋ฐํ๊ฐ์ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ ๊ฒฝ๊ณ ๊ฐ ๋ฐ์ํ์ง ์์ผ๋ฏ๋ก, “์ด ๋ฉ์๋์ ๋ฐํ๊ฐ์ ์ฌ์ฉํ์ง ์์๋ ๊ด์ฐฎ๋ค”๋ ์๋๋ฅผ ๋ช ์์ ์ผ๋ก ํํ
- ์ฌ์ฉํ์ง ์์๋ ๋ฌธ๋ฒ์ ์ผ๋ก ๋ฌธ์ ๋์ง ์์, ๊ฐ๋ฐ์์ ํ๋จ์ ๋ฐ๋ผ ์ฌ์ฉ ํน์ ์๋ต ๊ฐ๋ฅ
class Player {
var tracker = LevelTracker()
let playerName: String
func complete(level: Int) {
LevelTracker.unlock(level + 1)
tracker.advance(to: level + 1)
}
init(name: String) {
playerName = name
}
}
var player = Player(name: "Argyrios")
player.complete(level: 1)
print("highest unlocked level is now \\(LevelTracker.highestUnlockedLevel)")
// "highest unlocked level is now 2" ์ถ๋ ฅ
// ๋ ๋ฒจ 1์ ์๋ฃํด์ ๋ ๋ฒจ 2๊ฐ ์ด๋ ธ์ต๋๋ค.
player = Player(name: "Beto")
if player.tracker.advance(to: 6) {
print("player is now on level 6")
} else {
print("level 6 has not yet been unlocked")
}
// "level 6 has not yet been unlocked" ์ถ๋ ฅ
๋ฐ์ํ
'โจ๏ธ Language > swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ์์ (0) | 2024.03.22 |
---|---|
[Swift] ์๋ธ์คํฌ๋ฆฝํธ (0) | 2024.03.22 |
[Swift] ํ๋กํผํฐ (0) | 2024.03.20 |
[Swift] ํด๋์ค์ ๊ตฌ์กฐ์ฒด (0) | 2024.03.19 |
[Swift] ํด๋ก์ (1) | 2024.03.18 |