Created
March 14, 2016 18:43
-
-
Save alonecuzzo/e49eed42d113093b2072 to your computer and use it in GitHub Desktop.
NSCoder
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
/** | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
*/ | |
| |
| |
import UIKit | |
| |
// Dealing with having to override init(coder:) | |
| |
let frame = CGRect(x: 0, y: 0, width: 320, height: 460) | |
| |
/// 1. First, an example of NSCoder Implementation in Swift. | |
| |
class IAmCoder : UIView { | |
private var _title : String | |
init(frame: CGRect, title: String) { | |
_title = title | |
super.init(frame: frame) | |
} | |
| |
required init?(coder aDecoder: NSCoder) { | |
_title = aDecoder.decodeObjectForKey("banana") as! String | |
super.init(coder: aDecoder) | |
} | |
override func encodeWithCoder(aCoder: NSCoder) { | |
aCoder.encodeObject(self._title, forKey: "banana") | |
super.encodeWithCoder(aCoder) | |
} | |
} | |
| |
let iamcoderView = IAmCoder(frame: frame, title: "I am a coder") | |
iamcoderView.backgroundColor = UIColor.redColor() | |
| |
let iamcoderData = NSKeyedArchiver.archivedDataWithRootObject(iamcoderView) | |
let iamcoderNewInstance = NSKeyedUnarchiver.unarchiveObjectWithData(iamcoderData) | |
| |
// A better way to do this? With convenience initalizers | |
| |
class IAmConvenientCoder : UIView { | |
private var _title : String = String() | |
convenience init(frame: CGRect, title: String) { | |
self.init(frame: frame) | |
self._title = title | |
} | |
} | |
| |
| |
let conv = IAmConvenientCoder(frame: frame, title: "Convenient Coder") | |
conv.backgroundColor = UIColor.greenColor() | |
let iamconvData = NSKeyedArchiver.archivedDataWithRootObject(conv) | |
let unarchivedConv = NSKeyedUnarchiver.unarchiveObjectWithData(iamconvData) | |
| |
| |
// Dependency Injection with XIB / Storyboards | |
| |
protocol ViewControllerDelegate { | |
var title : String { get } | |
} | |
| |
class StoryboardViewController : UIViewController { | |
@IBInspectable var dataSource : ViewControllerDelegate? | |
convenience init(delegate : ViewControllerDelegate, color: UIColor) { | |
self.init(nibName: nil, bundle: nil) | |
self.dataSource = delegate | |
self.view.backgroundColor = color | |
} | |
override func viewDidLoad() { | |
self.view.backgroundColor = UIColor.blueColor() | |
print("ViewDidLoad") | |
self.title = self.dataSource?.title | |
} | |
} | |
| |
struct DataModel : ViewControllerDelegate { | |
var title : String { | |
return "Hi Jabari" | |
} | |
} | |
| |
let model = DataModel() | |
let controller = StoryboardViewController(delegate: model, color: UIColor.purpleColor()) | |
controller.dataSource = model | |
| |
let window = UIWindow(frame: frame) | |
window.rootViewController = controller | |
window.makeKeyAndVisible() | |
print(controller.title!) | |
//Being Testable | |
| |
class TestView : UIView { | |
var delegate : ViewControllerDelegate? | |
private var textLabel : UILabel = UILabel() | |
required convenience init(frame : CGRect, color: UIColor?) { | |
self.init(frame: frame) | |
self.backgroundColor = color | |
} | |
} | |
| |
extension TestView { | |
convenience init(test: ViewControllerDelegate) { | |
self.init(frame: CGRect(x: 0, y: 0, width: 320, height: 360), color: UIColor.redColor()) | |
self.delegate = test | |
testTheTitle() | |
} | |
func testTheTitle() -> Void { | |
self.textLabel.text = self.delegate?.title | |
print(self.textLabel.text) | |
print(self.delegate!.title) | |
print(assert(self.textLabel.text == self.delegate!.title)) | |
} | |
} | |
| |
let test = TestView(test: model) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment