Last active
August 29, 2017 02:44
-
-
Save el-hoshino/5fe8e073fac83733ca32 to your computer and use it in GitHub Desktop.
ガチで Swift でプログラム組んで1年経っての心得 ref: http://qiita.com/lovee/items/555a5b76097347aa2367
This file contains hidden or 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
let object = NSUserDefaults.standardUserDefaults().objectForKey("user data") | |
var data = object as? [String: Int] | |
var hp = data?["hp"] | |
//////いろいろ処理////// | |
if hp != nil { | |
hp = hp! + 2 | |
data!["hp"] = hp! | |
} else { | |
hp = 2 | |
data = ["hp": 2] | |
} | |
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "user data") |
This file contains hidden or 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
let object = NSUserDefaults.standardUserDefaults().objectForKey("user data") | |
var data: [String: Int]! = object as? [String: Int] | |
if data == nil { | |
data = ["hp": 0] | |
} | |
var hp: Int! = data["hp"] | |
//////いろいろ処理////// | |
hp = hp + 2 | |
data["hp"] = hp | |
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "user data") |
This file contains hidden or 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
enum MyButtonType: Int { | |
case Button1 = 1, Button2, Button3, Button4 | |
case Dummy | |
static let allValues = {() -> [MyButtonType] in | |
return [Int](Button1.rawValue ..< Dummy.rawValue).flatMap({ (value) -> MyButtonType? in | |
return MyButtonType(rawValue: value) | |
}) | |
}() | |
} | |
extension UIButton { | |
var type: MyButtonType { | |
return MyButtonType(rawValue: self.tag) ?? .Dummy | |
} | |
} | |
class MyViewController: UIViewController { | |
override func viewDidLoad() { | |
for buttonType in MyButtonType.allValues { | |
let frame = CGRect(x: CGFloat(buttonType.rawValue) * 50, y: 0, width: 50, height: 50) | |
let button = UIButton(frame: frame) | |
button.addTarget(self, action: "onButtonTapped:", forControlEvents: .TouchUpInside) | |
button.tag = buttonType.rawValue | |
self.view.addSubview(button) | |
} | |
} | |
func onButtonTapped(sender: UIButton) { | |
switch sender.type { | |
case .Button1: | |
//Do Something | |
break | |
case .Button2: | |
//Do Something | |
break | |
case .Button3: | |
//Do Something | |
break | |
case .Button4: | |
//Do Something | |
break | |
case .Dummy: | |
break | |
} | |
} | |
} |
This file contains hidden or 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
enum MyButtonType: Int { | |
case Button1 = 1, Button2, Button3, Button4 | |
case Dummy | |
static let allValues = {() -> [MyButtonType] in | |
return [Int](Button1.rawValue ..< Dummy.rawValue).flatMap({ (value) -> MyButtonType? in | |
return MyButtonType(rawValue: value) | |
}) | |
}() | |
func buttonAction() { | |
switch self { | |
case .Button1: | |
//Do something | |
break | |
case .Button2: | |
//Do Something | |
break | |
case .Button3: | |
//Do something | |
break | |
case .Button4: | |
//Do something | |
break | |
case .Dummy: | |
break | |
} | |
} | |
} | |
extension UIButton { | |
var type: MyButtonType { | |
return MyButtonType(rawValue: self.tag) ?? .Dummy | |
} | |
} | |
class MyViewController: UIViewController { | |
override func viewDidLoad() { | |
for buttonType in MyButtonType.allValues { | |
let frame = CGRect(x: CGFloat(buttonType.rawValue) * 50, y: 0, width: 50, height: 50) | |
let button = UIButton(frame: frame) | |
button.addTarget(self, action: "onButtonTapped:", forControlEvents: .TouchUpInside) | |
button.tag = buttonType.rawValue | |
self.view.addSubview(button) | |
} | |
} | |
func onButtonTapped(sender: UIButton) { | |
sender.type.buttonAction() | |
} | |
} |
This file contains hidden or 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 SomeClass { | |
var a = 1 | |
} | |
struct SomeStruct { | |
var a = 1 | |
} | |
let someClass = SomeClass() | |
let anotherClass = someClass | |
someClass.a | |
anotherClass.a = 3 | |
someClass.a | |
let someStruct = SomeStruct() | |
let anotherStruct = someStruct | |
someStruct.a | |
anotherStruct.a = 3 | |
someStruct.a |
This file contains hidden or 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
var someStruct = SomeStruct() | |
var anotherStruct = someStruct | |
someStruct.a | |
anotherStruct.a = 3 | |
someStruct.a |
This file contains hidden or 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 MyViewController: UIViewController { | |
override func viewDidLoad() { | |
let aChildView = UIView() | |
aChildView.tag = 100 | |
self.view.addSubview(aChildView) | |
} | |
func setChildViewBackgroundTo(color: UIColor) { | |
if let aChildView = self.view.viewWithTag(100) { | |
aChildView.backgroundColor = color | |
} | |
} | |
} |
This file contains hidden or 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
var a = 1 | |
var b: Int? | |
b = a | |
a = b |
This file contains hidden or 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 A { | |
var b: B? | |
} | |
class B { | |
var a: A? | |
} | |
let a = A() | |
let b = B() | |
a.b = b | |
b.a = a |
This file contains hidden or 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 Person { | |
var car: Car? | |
func goForADrive() { | |
self.car?.drive() | |
} | |
func buyGasoline() { | |
print("Go buy some gasoline") | |
} | |
} | |
class Car { | |
var owner: Person | |
init(owner: Person) { | |
self.owner = owner | |
} | |
func drive() { | |
print("Drive!") | |
} | |
func outOfGasoline() { | |
self.owner.buyGasoline() | |
} | |
} | |
let maki = Person() | |
let ferrari458Italia = Car(owner: maki) | |
maki.car = ferrari458Italia | |
maki.goForADrive() | |
ferrari458Italia.outOfGasoline() |
This file contains hidden or 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 Person: CarDelegate { | |
var car: Car? | |
func goForADrive() { | |
self.car?.drive() | |
} | |
func buyGasoline() { | |
print("Go buy some gasoline") | |
} | |
func solveGasolineShortageProblem() { | |
self.buyGasoline() | |
} | |
} | |
class Car { | |
var delegate: CarDelegate? | |
func drive() { | |
print("Drive!") | |
} | |
func outOfGasoline() { | |
self.delegate?.solveGasolineShortageProblem() | |
} | |
} | |
protocol CarDelegate { | |
func solveGasolineShortageProblem() | |
} | |
let nishikinoMaki = Person() | |
let ferrari458Italia = Car() | |
nishikinoMaki.car = ferrari458Italia | |
ferrari458Italia.delegate = nishikinoMaki | |
nishikinoMaki.goForADrive() | |
ferrari458Italia.outOfGasoline() |
This file contains hidden or 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 Person: CarDelegate { | |
//略 | |
func sellMyCar() { | |
self.car = nil | |
print("458 イタリア売却しました") | |
} | |
func buyANewCar() { | |
let laFerrari = Car() | |
laFerrari.delegate = self | |
self.car = laFerrari | |
print("ラ フェラーリを購入しました") | |
} | |
func solveGasolineShortageProblem() { | |
self.sellMyCar() | |
self.buyANewCar() | |
} | |
} |
This file contains hidden or 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
let object = NSUserDefaults.standardUserDefaults().objectForKey("user data") | |
var data = object as? [String: Int] ?? [String: Int]() | |
var hp = data["hp"] ?? 0 | |
//////いろいろ処理////// | |
hp = hp + 2 | |
data["hp"] = hp | |
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "user data") |
This file contains hidden or 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
protocol CarDelegate: class { | |
// 略… | |
} |
This file contains hidden or 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 Car { | |
weak var delegate: CarDelegate? | |
// 略… | |
} |
This file contains hidden or 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 Car { | |
weak var delegate: CarDelegate? | |
func drive() { | |
self.startEngine() | |
self.changeShift() | |
self.releaseSideBrake() | |
self.stepOnGasPedal() | |
} | |
func startEngine() { | |
print("Start Engine") | |
} | |
func changeShift() { | |
print("Change Shift") | |
} | |
func releaseSideBrake() { | |
print("Release Side Brake") | |
} | |
func stepOnGasPedal() { | |
print("GO! GO! GO!") | |
} | |
func outOfGasoline() { | |
self.delegate?.solveGasolineShortageProblem() | |
} | |
} |
This file contains hidden or 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
func goForADrive() { | |
self.car?.releaseSideBrake() | |
self.car?.startEngine() | |
self.car?.changeShift() | |
self.car?.stepOnGasPedal() | |
} |
This file contains hidden or 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 Car { | |
weak var delegate: CarDelegate? | |
func drive() { | |
self.startEngine() | |
self.changeShift() | |
self.releaseSideBrake() | |
self.stepOnGasPedal() | |
} | |
private func startEngine() { | |
print("Start Engine") | |
} | |
private func changeShift() { | |
print("Change Shift") | |
} | |
private func releaseSideBrake() { | |
print("Release Side Brake") | |
} | |
private func stepOnGasPedal() { | |
print("GO! GO! GO!") | |
} | |
func outOfGasoline() { | |
self.delegate?.solveGasolineShortageProblem() | |
} | |
} |
This file contains hidden or 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 SomeClass { | |
static var defaultObject: SomeClass = SomeClass() | |
private init() { | |
} | |
} |
This file contains hidden or 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
func appendString(string: String) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in | |
self.displayedString += string | |
switch self.displayedString.characters.count { | |
case 0 ..< 20: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
case 20: | |
if let image = UIImage(named: "TestImage") { | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: image) | |
} | |
case 21: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: true) | |
default: | |
self.displayedString = "Hello, World!" | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
} |
This file contains hidden or 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
let object = NSUserDefaults.standardUserDefaults().objectForKey("user data") | |
var data = object as? [String: Int] | |
var hp = data?["hp"] | |
//////いろいろ処理////// | |
if var data = data, hp = hp { | |
hp += 2 | |
data["hp"] = hp | |
} else { | |
let defaultHP = 2 | |
hp = defaultHP | |
data = ["hp": defaultHP] | |
} | |
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "user data") |
This file contains hidden or 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
func appendString(string: String) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in | |
self.displayedString += string | |
switch self.displayedString.characters.count { | |
case 0 ..< 20: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
case 20: | |
if let image = UIImage(named: "TestImage") { | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: image) | |
} | |
case 21: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: true) | |
default: | |
self.displayedString = "Hello, World!" | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
} |
This file contains hidden or 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
func needsUpdate(sender: NSNotification) { | |
switch sender.object { | |
case let newString as String: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
self.label.text = newString | |
}) | |
case let newImage as UIImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
let imageView = UIImageView(frame: self.label.frame) | |
imageView.contentMode = .ScaleAspectFill | |
imageView.image = newImage | |
self.addSubview(imageView) | |
}) | |
case let removeImage as Bool where removeImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
for subview in self.subviews { | |
if subview is UIImageView { | |
subview.removeFromSuperview() | |
} | |
} | |
}) | |
default: | |
break | |
} | |
} |
This file contains hidden or 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
func appendString(string: String) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in | |
self.displayedString += string | |
switch self.displayedString.characters.count { | |
case 0 ..< 20: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
case 20: | |
if let image = UIImage(named: "TestImage") { | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: image) | |
} | |
case 21: | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: true) | |
default: | |
self.displayedString = "Hello, World!" | |
NSNotificationCenter.defaultCenter().postNotificationName("NewString", object: self.displayedString) | |
} |
This file contains hidden or 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
func needsUpdate(sender: NSNotification) { | |
switch sender.object { | |
case let newString as String: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
self.label.text = newString | |
}) | |
case let newImage as UIImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
let imageView = UIImageView(frame: self.label.frame) | |
imageView.contentMode = .ScaleAspectFill | |
imageView.image = newImage | |
self.addSubview(imageView) | |
}) | |
case let removeImage as Bool where removeImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
for subview in self.subviews { | |
if subview is UIImageView { | |
subview.removeFromSuperview() | |
} | |
} | |
}) | |
default: | |
break | |
} | |
} |
This file contains hidden or 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
// TestModelDelegate | |
func display(something: NSObject) { | |
self.testView.display(something) | |
} |
This file contains hidden or 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
// TestModelDelegate | |
func display(something: NSObject) { | |
self.testView.display(something) | |
} |
This file contains hidden or 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
// TestModelDelegate | |
func display(something: NSObject) { | |
self.testView.display(something) | |
} |
This file contains hidden or 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
var result = 0.0 | |
var a = 0.0 | |
var b = 0.0 | |
func calculate(function: () -> Double) { | |
print(result) | |
result = function() | |
print(result) | |
} | |
a = 1 | |
b = 2 | |
calculate({ a + b }) | |
result | |
a = 3 | |
b = 4 | |
calculate({ a * b }) | |
result | |
a = 5 | |
b = 0 | |
calculate({ () -> Double in | |
if b == 0 { | |
return 0 | |
} else { | |
return a / b | |
} | |
}) | |
result |
This file contains hidden or 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 MyViewController: UIViewController { | |
var viewA: UIView! | |
var viewB: UIView! | |
override func viewDidLoad() { | |
self.viewA = UIView() | |
self.viewA.backgroundColor = .whiteColor() | |
self.viewA.hidden = true | |
self.view.addSubview(self.viewA) | |
self.viewB = UIView() | |
self.viewB.backgroundColor = .blackColor() | |
self.viewB.hidden = false | |
self.view.addSubview(self.viewA) | |
} | |
} |
This file contains hidden or 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 MyViewController: UIViewController { | |
var viewA: UIView! | |
var viewB: UIView! | |
override func viewDidLoad() { | |
self.viewA = UIView() | |
self.viewA.backgroundColor = .whiteColor() | |
self.viewA.hidden = true | |
self.view.addSubview(self.viewA) | |
self.viewB = UIView() | |
self.viewA.backgroundColor = .redColor() | |
self.viewB.hidden = false | |
self.view.addSubview(self.viewB) | |
} | |
} |
This file contains hidden or 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 MyViewController: UIViewController { | |
var viewA: UIView! | |
var viewB: UIView! | |
override func viewDidLoad() { | |
self.viewA = {() -> UIView in | |
let view = UIView() | |
view.backgroundColor = .whiteColor() | |
view.hidden = true | |
self.view.addSubview(view) | |
return view | |
}() | |
self.viewB = {() -> UIView in | |
let view = UIView() | |
view.backgroundColor = .redColor() | |
view.hidden = false | |
self.view.addSubview(view) | |
return view | |
}() | |
} | |
} |
This file contains hidden or 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 MyViewController: UIViewController { | |
var viewA: UIView! | |
var viewB: UIView! | |
override func viewDidLoad() { | |
// Swift 2.X 系では do を使えば簡単に局部スコープ作れます | |
do { | |
let view = UIView() | |
view.backgroundColor = .whiteColor() | |
view.hidden = true | |
self.view.addSubview(view) | |
self.viewA = view | |
} | |
do { | |
let view = UIView() | |
view.backgroundColor = .redColor() | |
view.hidden = false | |
self.view.addSubview(view) | |
self.viewB = view | |
} | |
} | |
} |
This file contains hidden or 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 MyViewController: UIViewController { | |
override func viewDidLoad() { | |
for i in 1 ... 4 { | |
let frame = CGRect(x: CGFloat(i) * 50, y: 0, width: 50, height: 50) | |
let button = UIButton(frame: frame) | |
button.addTarget(self, action: "onButtonTapped:", forControlEvents: .TouchUpInside) | |
button.tag = i | |
self.view.addSubview(button) | |
} | |
} | |
func onButtonTapped(sender: UIButton) { | |
switch sender.tag { | |
case 1: | |
//Do Something | |
break | |
case 2: | |
//Do Something | |
break | |
case 3: | |
//Do Something | |
break | |
case 4: | |
//Do Something | |
break | |
default: | |
break | |
} | |
} | |
} |
This file contains hidden or 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 UIKit | |
protocol TestModelDelegate { | |
func display(something: NSObject) | |
} | |
class TestModel: NSObject { | |
var displayedString: String | |
var delegate: TestModelDelegate? | |
override init() { | |
self.displayedString = "Hello, World!" | |
super.init() | |
} | |
func appendString(string: String) { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in | |
self.displayedString += string | |
switch self.displayedString.characters.count { | |
case 0 ..< 20: | |
self.delegate?.display(self.displayedString) | |
case 20: | |
if let image = UIImage(named: "TestImage.png") { | |
self.delegate?.display(image) | |
} | |
case 21: | |
self.delegate?.display(true) | |
default: | |
self.displayedString = "Hello, World!" | |
self.delegate?.display(self.displayedString) | |
} | |
} | |
} | |
} |
This file contains hidden or 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 UIKit | |
class TestView: UIView { | |
let label: UILabel | |
override init(frame: CGRect) { | |
self.label = UILabel(frame: CGRect(origin: .zero, size: frame.size)) | |
super.init(frame: frame) | |
self.addSubview(self.label) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func display(something: NSObject) { | |
switch something { | |
case let newString as String: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
self.label.text = newString | |
}) | |
case let newImage as UIImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
let imageView = UIImageView(frame: self.label.frame) | |
imageView.contentMode = .ScaleAspectFill | |
imageView.image = newImage | |
self.addSubview(imageView) | |
}) | |
case let removeImage as Bool where removeImage: | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
for subview in self.subviews { | |
if subview is UIImageView { | |
subview.removeFromSuperview() | |
} | |
} | |
}) | |
default: | |
break | |
} | |
} | |
} |
This file contains hidden or 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 UIKit | |
class ViewController: UIViewController, TestModelDelegate { | |
var testModel: TestModel! | |
var testView: TestView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.testModel = TestModel() | |
self.testView = TestView(frame: CGRect(origin: .zero, size: self.view.frame.size)) | |
self.testModel.delegate = self | |
self.testView.label.text = self.testModel.displayedString | |
self.view.addSubview(self.testView) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
self.testModel.appendString("!") | |
} | |
// TestModelDelegate | |
func display(something: NSObject) { | |
self.testView.display(something) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment