Skip to content

Instantly share code, notes, and snippets.

func pureLayout() {
scrollView.autoPinEdgesToSuperviewEdges(with: .zero)
scrollView.autoPinEdge(.bottom, to: .bottom, of: longer, withOffset: 20)
box.autoPinEdge(toSuperviewEdge: .top, withInset: 150)
box.autoAlignAxis(toSuperviewAxis: .vertical)
box.autoSetDimensions(to: CGSize(width: 100, height: 100))
circle.autoPinEdge(.bottom, to: .bottom, of: self.view, withOffset: -20, relation: .lessThanOrEqual)
circle.autoPinEdge(.bottom, to: .top, of: longer, withOffset: -20, relation: .lessThanOrEqual)
func pureLayout() {
scrollView.autoPinEdgesToSuperviewEdges(with: .zero)
scrollView.autoPinEdge(.bottom, to: .bottom, of: longer, withOffset: 20)
box.autoPinEdge(toSuperviewEdge: .top, withInset: 50)
box.autoPinEdge(.left, to: .left, of: self.view, withOffset: 20)
box.autoSetDimensions(to: CGSize(width: 100, height: 100))
circle.autoPinEdge(.top, to: .top, of: box)
circle.autoPinEdge(.right, to: .right, of: self.view, withOffset: -20)
func pureLayout() {
box.autoPinEdge(toSuperviewEdge: .top, withInset: 50)
box.autoPinEdge(toSuperviewEdge: .left, withInset: 20)
box.autoSetDimensions(to: CGSize(width: 100, height: 100))
circle.autoPinEdge(.top, to: .top, of: box)
circle.autoPinEdge(toSuperviewEdge: .right, withInset: 20)
circle.autoSetDimensions(to: CGSize(width: 100, height: 100))
longer.autoPinEdge(toSuperviewEdge: .left, withInset: 20)
// weak reference, but we bet that self will not be nil
disposables += signal.observe {[unowned self] values in
self.workWithMeAllTheTime()
}
// weak reference, but self becomes optional
disposables += signal.observe {[weak self] values in
guard let strongSelf = self else { return }
strongSelf.workWithMeAllTheTime()
var disposables = CompositeDisposable()
disposables += viewModel.criticalInfo.observeValues {[unowned self] (value) in
// react to value
}
deinit {
disposables.dispose()
}
// public variable accessible from outside of class
var producer: SignalProducer<String, NoError>
init() {
producer = SignalProducer {[weak self] observer, compositeDisposable in
guard let strongSelf = self else { return }
compositeDisposable.add {
print("I've been disposed! I can clean my resources ;)")
}
@Eluss
Eluss / deinitdispose.swift
Last active October 24, 2016 09:09
deinitdisposeobserver
var disposables = CompositeDisposable()
disposables += viewModel.criticalInfo.observeValues {[unowned self] (value) in
// react to value
}
deinit {
disposables.dispose()
}
let contactTextField = UITextField()
contactTextField.delegate = self
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
displayContacts()
return false // don't start editing
}
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let contact = contactProperty.contact
if let phoneNumber = contact.phoneNumbers.first { // We display only phone numbers, so we will have it ;)
let phoneNumberValue = phoneNumber.value as! CNPhoneNumber
let result = phoneNumberValue.stringValue
self.contactTextField.text = result
} else {
self.contactTextField.text = ":("
}
}
func displayContacts() {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self // we want to take a contact after coming back
contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey] // We are interested in phone numbers
presentViewController(contactPicker, animated: true, completion: nil)
}