Last active
August 29, 2015 14:17
-
-
Save delba/450938e8fd1ca46b717a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum Provider: String { | |
case Vimeo = "Vimeo" | |
case SoundCloud = "SoundCloud" | |
} | |
class Item { | |
let title: String | |
let provider: Provider | |
var width: Int! | |
init(data: [String: AnyObject]) { | |
provider = Provider(rawValue: data["provider"] as String)! | |
title = data["title"] as String | |
if let width = data["width"] as? Int { | |
self.width = width | |
} | |
} | |
} | |
class Vimeo: Item { | |
let video: String! | |
override init(data: [String: AnyObject]) { | |
super.init(data: data) | |
video = data["video"] as String | |
} | |
} | |
class SoundCloud: Item { | |
let sound: String! | |
override init(data: [String : AnyObject]) { | |
super.init(data: data) | |
width = data["_width"] as Int | |
sound = data["sound"] as String | |
} | |
} | |
let dataSet: [[String: AnyObject]] = [ | |
["title": "Vimeo title", "provider": "Vimeo", "video": "blah!", "width": 42], | |
["title": "SoundCloud title", "provider": "SoundCloud", "sound": "boom", "_width": 38] | |
] | |
func allItems() -> [Item] { | |
return dataSet.map { (data: [String: AnyObject]) -> Item in | |
let provider = Provider(rawValue: data["provider"] as String)! | |
switch provider { | |
case .Vimeo: | |
return Vimeo(data: data) | |
case .SoundCloud: | |
return SoundCloud(data: data) | |
} | |
} | |
} | |
for item in allItems() { | |
// Accessing item's attribute | |
println(item.title) | |
println(item.width) | |
// Accessing downcasted item | |
switch item.provider { | |
case .Vimeo: | |
println((item as Vimeo).video) | |
case .SoundCloud: | |
println((item as SoundCloud).sound) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item { | |
var title: String! | |
} | |
class SoundCloud: Item { | |
} | |
class Vimeo: Item { | |
override var title: String! { | |
get { | |
return super.title | |
} | |
set { | |
super.title = "Vimeo setter \(newValue)" | |
} | |
} | |
} | |
let item = Vimeo() as Item | |
item.title = "blah!" | |
if let item = item as? SoundCloud { | |
println("Yep") | |
} else { | |
println("Nope") | |
} | |
item.title |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment