ν°μ€ν 리 λ·°
Swiftμμ νμ μ λ§€μ° μ€μν κ°λ μ λλ€. νμ μ λ°μ΄ν°λ₯Ό ν΄μνλ λ°©λ²μ μ μνκ³ , λμΌν λ°μ΄ν°λΌλ νμ μ λ°λΌ μ ν λ€λ₯Έ μλ―Έμ κΈ°λ₯μ κ°μ§ μ μμ΅λλ€. μ΄λ¬ν νμ μ λ€λ£° μ μκ² ν΄μ£Όλ κ²μ΄ λ°λ‘ νμ μΊμ€ν μ λλ€.
νμ μΊμ€ν μ΄λ?
νμ μΊμ€ν μ νμ μ λ³κ²½νμ¬ λ°μ΄ν°λ₯Ό λ€λ₯Έ λ°©μμΌλ‘ ν΄μνλ λ°©λ²μΌλ‘, μΈμ€ν΄μ€μ νμ μ νμΈνκ±°λ ν΄λμ€ κ³μΈ΅ ꡬ쑰 λ΄μμ νμ μ μ νν μ μμ΅λλ€.
Swiftμμλ μ£Όλ‘ λ κ°μ§ μ£Όμ μμ μ μνν©λλ€:
1. μΈμ€ν΄μ€μ νμ μ νμΈ
2. ν΄λμ€ κ³μΈ΅ ꡬ쑰 λ΄μμ μΈμ€ν΄μ€λ₯Ό λ€λ₯Έ μμ ν΄λμ€λ νμ ν΄λμ€λ‘ λ³ν
λν, νμ μ΄ νΉμ νλ‘ν μ½μ μ€μνλμ§ μ¬λΆλ νμ μΊμ€ν μ ν΅ν΄ νμΈν μ μμ΅λλ€.
νμ μΊμ€ν μ ꡬν
Swiftμμλ is μ°μ°μμ as μ°μ°μλ₯Ό μ¬μ©νμ¬ νμ μΊμ€ν μ ꡬνν©λλ€.
μμ λ₯Ό ν΅ν΄ μ΄ν΄λ³΄κ² μ΅λλ€.
class MediaItem {
var name: String
init(name: String) {
self.name = name
}
}
class Movie: MediaItem {
var director: String
init(name: String, director: String) {
self.director = director
super.init(name: name)
}
}
class Song: MediaItem {
var artist: String
init(name: String, artist: String) {
self.artist = artist
super.init(name: name)
}
}
let library = [
Movie(name: "Casablanca", director: "Michael Curtiz"),
Song(name: "Blue Suede Shoes", artist: "Elvis Presley"),
Movie(name: "Citizen Kane", director: "Orson Welles"),
Song(name: "The One And Only", artist: "Chesney Hawkes"),
Song(name: "Never Gonna Give You Up", artist: "Rick Astley")
] // libraryμ νμ
μ [MediaItem]μΌλ‘ μΆλ‘ λλ€.
// Moviewμ Song νμ
μ μΈμ€ν΄μ€κ° MediaItemμΌλ‘ μ
μΊμ€ν
λ κ²
1. is μ°μ°μ
is μ°μ°μλ μΈμ€ν΄μ€κ° νΉμ νμ μΌλ‘ μΊμ€ν μ΄ κ°λ₯νμ§ μ¬λΆλ₯Ό κ²μ¬ν©λλ€.
λ°νμ μμ μ μ€μ 체ν¬κ° μ΄λ£¨μ΄μ§λ©°, μΊμ€ν μ΄ κ°λ₯νκ±°λ νμ μ΄ λμΌν κ²½μ° trueλ₯Ό, κ·Έλ μ§ μμΌλ©΄ falseλ₯Ό λ°νν©λλ€.
μΊμ€ν κ°λ₯ μ¬λΆλ§μ 체ν¬νκΈ° λλ¬Έμ μ€μ νμ μ΄ ν΄λΉ νμ μ΄ μλ κ°λ₯μ±λ μμ΅λλ€.
var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
}
}
print("Media library contains \(movieCount) movies and \(songCount) songs")
// Prints "Media library contains 2 movies and 3 songs"
2. as μ°μ°μ
as μ°μ°μλ νμ μΊμ€ν μ μννλ λ° μ¬μ©λ©λλ€.
κ³μΈ΅ κ΄κ³κ° μλ νμ κ°μλ§ μ¬μ© κ°λ₯νλ©°, κ³μΈ΅ κ΄κ³κ° μλ νμ κ°μλ μΊμ€ν μ΄ μ€ν¨ν©λλ€.
• μ μΊμ€ν (Upcasting)
μ μΊμ€ν μ μλΈ ν΄λμ€ μΈμ€ν΄μ€λ₯Ό μνΌ ν΄λμ€ νμ μΌλ‘ μΊμ€ν νλ κ²μ λλ€.
μ μΊμ€ν μ νμ μ±κ³΅νλ©°, μ»΄νμΌ μμ μ μΊμ€ν κ°λ₯ μ¬λΆκ° κ²°μ λ©λλ€.
μμ μμ μμ libraryμ νμ μ [MediaItem]μΌλ‘ μΆλ‘ λ©λλ€. μ΄λ Movieμ Song νμ μ μΈμ€ν΄μ€κ° MediaItemμΌλ‘ μ μΊμ€ν λ κ²μ λλ€.
μ μΊμ€ν μ νλ λ°©μμ μμμ λ³Έ libraryμ²λΌ νμ μΆλ‘ μ μ΄μ©ν΄λ λκ³ μ§μ νμ μ λͺ μν΄λ λ©λλ€.
λν as μ°μ°μλ₯Ό μ¬μ©νμ¬ μ§μ μ μΊμ€ν μ ν΄μ£Όλ©΄ λ©λλ€.
// μ§μ μ
μΊμ€ν
let mediaItem1 = Movie(name: "Casablanca", director: "Michael Curtiz") as MediaItem
// νμ
λͺ
μ
let mediaItem2: MediaItem = Movie(name: "Casablanca", director: "Michael Curtiz")
μΆκ°λ‘, Swiftμ κΈ°λ³Ένμ λ€μ κ·Έμ λμνλ Foundation ν΄λμ€ νμ μΌλ‘ λ³ννλ λΈλ¦Ώμ§(Bridging)μ μλ―ΈνκΈ°λ ν©λλ€.
μλ₯Ό λ€μ΄, String νμ μ NSString νμ μΌλ‘ μ μΊμ€ν νμ¬ μ¬μ©ν μ μμ΅λλ€.
• λ€μ΄μΊμ€ν (DownCasting)
λ€μ΄μΊμ€ν μ μνΌ ν΄λμ€ μΈμ€ν΄μ€λ₯Ό μλΈ ν΄λμ€ νμ μΌλ‘ μΊμ€ν νλ κ²μ λλ€.
μ»΄νμΌ μμ μ μ±κ³΅ μ¬λΆλ₯Ό μ μ μμΌλ©°, λ°νμμ 체ν¬κ° μ΄λ£¨μ΄μ§λλ€.
λ€μ΄μΊμ€ν μ μ€ν¨ν μ μκΈ° λλ¬Έμ, as?λ₯Ό μ¬μ©νμ¬ μ΅μ λμ 리ν΄νκ±°λ as!λ₯Ό μ¬μ©νμ¬ κ°μ μΊμ€ν μ ν μ μμ΅λλ€.
νμ§λ§, μ ν 무κ΄ν νμ μΌλ‘μ μΊμ€ν μ μ»΄νμΌλ¬κ° κ²½κ³ λ₯Ό μ£ΌκΈ°λ ν©λλ€.
for item in library {
if let movie = item as? Movie {
print("Movie: \(movie.name), dir. \(movie.director)")
} else if let song = item as? Song {
print("Song: \(song.name), by \(song.artist)")
}
}
// Movie: Casablanca, dir. Michael Curtiz
// Song: Blue Suede Shoes, by Elvis Presley
// Movie: Citizen Kane, dir. Orson Welles
// Song: The One And Only, by Chesney Hawkes
// Song: Never Gonna Give You Up, by Rick Astley
νμ μΊμ€ν μ μ€μ λ‘ μΈμ€ν΄μ€λ₯Ό μμ νκ±°λ κ°μ λ³κ²½νμ§ μμ΅λλ€.
κΈ°λ³Έ μΈμ€ν΄μ€λ λμΌνκ² μ μ§λλ©°, λ¨μν μΊμ€νΈλ νμ μ μΈμ€ν΄μ€λ‘ μ²λ¦¬λκ³ μ κ·Όλ©λλ€.
μλ μ΄λ―Έμ§μ κ°μ΄ library λ°°μ΄μ 첫 λ²μ§Έ μμλ μ΄κΈ°μ Movie νμ μΌλ‘ μ΄κΈ°νκ° λμ§λ§,
MediaItem νμ μΌλ‘ μ μΊμ€ν λμ΄ director νλ‘νΌν°μλ μ κ·Όν μ μμ΅λλ€.
print(library[0].director)
λ²μ© νμ (Any, AnyObject)
νμ μΊμ€ν μ μνν λ μΌλ°μ μΌλ‘ μμ κ΄κ³μ μλ ν΄λμ€λΌλ¦¬λ§ μΊμ€ν μ΄ κ°λ₯νμ§λ§, Anyμ AnyObject νμ μ μ¬μ©ν κ²½μ° μμ κ΄κ³μ μμ§ μμλ νμ μΊμ€ν μ ν μ μμ΅λλ€.
• Any: λͺ¨λ νμ μ λνλ λλ€. (κ° νμ , μ°Έμ‘° νμ , ν¨μ νμ λ±)
var things: [Any] = []
things.append(0)
things.append(0.0)
things.append(42)
things.append(3.14159)
things.append("hello")
things.append((3.0, 5.0))
things.append(Movie(name: "Ghostbusters", director: "Ivan Reitman"))
things.append({ (name: String) -> String in "Hello, \(name)" })
• AnyObject: λͺ¨λ μ°Έμ‘° νμ (ν΄λμ€)μ λνλ λλ€. μ°Έμ‘° νμ μ΄ μλ κ²μ μ¬μ©νλ©΄ μλμ κ°μ΄ μλ¬κ° λ°μν©λλ€.
var things: [AnyObject] = []
things.append(1)
things.append(1.0)
things.append("Horang")
things.append(false)
things.append(print("Horang is smart"))
Anyλ‘ μ μΈλ μμλ€μ μ¬μ©ν λλ Switchλ¬Έ (ν¨ν΄ 맀μΉ)κ³Ό is, as ꡬ문μ μ‘°ν©νμ¬ μ¬μ©ν΄μΌ ν©λλ€.
for thing in things {
switch thing {
case 0 as Int:
print("zero as an Int")
case 0 as Double:
print("zero as a Double")
case let someInt as Int:
print("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
print("a positive double value of \(someDouble)")
case is Double:
print("some other double value that I don't want to print")
case let someString as String:
print("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
print("an (x, y) point at \(x), \(y)")
case let movie as Movie:
print("a movie called \(movie.name), dir. \(movie.director)")
case let stringConverter as (String) -> String:
print(stringConverter("Michael"))
default:
print("something else")
}
}
// zero as an Int
// zero as a Double
// an integer value of 42
// a positive double value of 3.14159
// a string value of "hello"
// an (x, y) point at 3.0, 5.0
// a movie called Ghostbusters, dir. Ivan Reitman
// Hello, Michael
Any νμ μ μ΅μ λ νμ μ ν¬ν¨νμ¬ λͺ¨λ νμ μ κ°μ λνλΌ μ μμ΅λλ€.
Any νμ μ΄ κΈ°λλλ κ³³μ μ΅μ λ κ°μ μ¬μ©νλ©΄ κ²½κ³ κ° λ°μν©λλ€. μ΄ κ²½μ°, as μ°μ°μλ₯Ό μ¬μ©νμ¬ Any νμ μΌλ‘ μΊμ€ν ν΄μ£Όμ΄μΌ ν©λλ€.
let optionalNumber: Int? = 3
things.append(optionalNumber) // Warning
things.append(optionalNumber as Any) // No warning
Anyμ AnyObjectλ λͺ¨λ νμ μ λ΄μ μ μμ§λ§, λ°νμ μμ μ νμ μ΄ κ²°μ λκΈ° λλ¬Έμ μ€λ₯κ° λ°μνλ©΄ λ°νμ μ€λ₯κ° λ°μν©λλ€.
λν, μ¬μ©ν λλ μ€μ νμ μΌλ‘ λ€μ΄μΊμ€ν μ΄ κ±°μ νμμ μ΄κΈ° λλ¬Έμ λ°νμ μ±λ₯μ μ’μ§ λͺ»ν μν₯μ μ£Όκ³ , μ€κ³μμΌλ‘λ λ°λμ§νμ§ μμ΅λλ€. λ°λΌμ, κ°λ₯νλ©΄ ꡬ체μ μΈ νμ μ μ¬μ©νλ κ²μ΄ μ’μ΅λλ€.
μ°Έκ³ π―
'Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Swift] ' % ' μ°μ°μλ λͺ¨λλ‘κ° μλλΌ λλ¨Έμ§μ λλ€. (Remainder Operator) (2) | 2024.02.04 |
---|---|
[Swift] Docs_(1) The Basics (0) | 2024.01.23 |
- λ¨μΌ μ°κ²° 리μ€νΈ
- 4949
- BOJ
- containerView
- κ· νμ‘ν μΈμ
- C++
- λ²μ©κ³ μ μλ³
- κ³΅κ° λ³΅μ‘λ
- 2024λ λͺ©ν
- root view controller
- μμ΄ κ³΅λΆ
- 2023λ νκ³
- ν
- κ΄νΈμ κ°
- μλ£ κ΅¬μ‘°
- μμ΄ λ΄μ€
- μλ°©ν₯ μ°κ²° 리μ€νΈ
- κ·λλΌλ―Έ μμ΄
- κ·λλΌλ―Έ μμ΄
- Anyobject
- tipkit
- remainder
- μ λ§λκΈ°
- Container View Controller
- 5397
- pageViewController
- 10808
- μ€ν
- Swift
- gitkraken
- Total
- Today
- Yesterday