티스토리 뷰
출처
타입 변환 (Type Casting)
타입 변환에 대한 클래스 계층 정의
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")
]
타입 확인하기
var movieCount = 0
var songCount = 0
var mediaCount = 0
for item in library {
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
} else if item is MediaItem {
mediaCount += 1
}
}
print("movieCount : \(movieCount) / songCount : \(songCount) / mediaCount : \(mediaCount)")
//movieCount : 2 / songCount : 3 / mediaCount : 0
다운캐스팅 (Downcasting)
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)")
}
}
Any 와 AnyObject 에 대한 타입 캐스팅
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)"
})
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")
}
}
let optionalNumber: Int? = 3
things.append(optionalNumber) // Warning
things.append(optionalNumber as Any) // No warning
'iOS 개발 > Swift' 카테고리의 다른 글
[Swift 3] 확장 (Extensions) (0) | 2017.04.22 |
---|---|
[Swift 3] 중첩 타입 (Nested Types) (0) | 2017.04.21 |
[Swift 3] 오류 처리 (Error Handling) (0) | 2017.04.19 |
[Swift 3] 옵셔널 체이닝 (Optional Chaining) (0) | 2017.04.18 |
[Swift 3] ARC (Automatic Reference Counting) (0) | 2017.04.18 |
- Total
- Today
- Yesterday
- Block
- HTTP
- 읽기 좋은 코드가 좋은 코드다
- applicationWillResignActive
- coredata
- AWS
- 꺼내먹어요
- EffectiveObjectiveC
- UIView
- CGImage
- Swift3
- docker
- NSManagedObjectModel
- NSManagedObjectContext
- optional
- Swift 3
- Swift 3.0
- thread
- workerThread
- Swfit
- string
- Arc
- CIImage
- Swift
- set
- NSManagedObject
- RunLoop
- ios
- dictionary
- delegate
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |