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)
}
}
// MediaType κ³Ό κ·Έκ±Έ μμν Movie, Song μΌλ‘ μ΄λ£¨μ΄μ Έ μκΈ° λλ¬Έμ
// [MediaType] μΌλ‘ μΆλ‘ λ¨.
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")
]
is
λ‘ ν΄λΉ μΈμ€ν΄μ€κ° νμ ν΄λμ€ νμ
μ΄λ©΄ true μλλ©΄ falsevar 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"
as?
as!
λ₯Ό μ΄μ©ν΄μ νμ ν΄λμ€ νμ
μΌλ‘ λ€μ΄μΊμ€νΈ ν μ μλ€.for item in library {
// νμ νμ
μ΄μ΄μΌ movie.director, song.artist μ μ κ·Όν μ μλ€.
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)")
}
}
Any
: ν¨μ νμ
μ ν¬ν¨ν λͺ¨λ νμ
μ μΈμ€ν΄μ€λ₯Ό λνλΌ μ μμ.AnyObject
: λͺ¨λ ν΄λμ€ νμ
μ μΈμ€ν΄μ€λ₯Ό λνλΌ μ μμ.as 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)" })
let optionalNumber: Int? = 3
things.append(optionalNumber as Any) // optional κ°μ λ£μΌλ €λ©΄ Any λ‘ μΊμ€νΈνμ.
// switch ꡬ문μ ν΅ν΄μ νμΈν μ μλ€.
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")
}
}
struct BlackjackCard > enum Suit
struct BlackjackCard > enum Rank > struct Values
struct BlackjackCard {
// nested Suit enumeration
enum Suit: Character {
case spades = "β ", hearts = "β‘", diamonds = "β’", clubs = "β£"
}
// nested Rank enumeration
enum Rank: Int {
case two = 2, three, four, five, six, seven, eight, nine, ten
case jack, queen, king, ace
struct Values {
let first: Int, second: Int?
}
var values: Values {
switch self {
case .ace:
return Values(first: 1, second: 11)
case .jack, .queen, .king:
return Values(first: 10, second: nil)
default:
return Values(first: self.rawValue, second: nil)
}
}
}
// BlackjackCard properties and methods
let rank: Rank, suit: Suit
var description: String {
var output = "suit is \(suit.rawValue),"
output += " value is \(rank.values.first)"
if let second = rank.values.second {
output += " or \(second)"
}
return output
}
}
μ§μ λ initμ΄ μκΈ° λλ¬Έμ λ©€λ²λ³ μ΄κΈ°μ μ¬μ© κ°λ₯.
let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
print("theAceOfSpades: \(theAceOfSpades.description)")
// Prints "theAceOfSpades: suit is β , value is 1 or 11"
μ€μ²©λ νμ μ μ΄λ¦μ chaining μ²λΌ . μ λΆμ¬μ μ°Έμ‘°ν μ μλ€.
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
// heartsSymbol is "β‘"