Skip to content

Instantly share code, notes, and snippets.

@misbell
Last active September 8, 2019 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misbell/b1acb66d7c8209ca885ea08f03dd6f22 to your computer and use it in GitHub Desktop.
Save misbell/b1acb66d7c8209ca885ea08f03dd6f22 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
//1 create view with a title property
class TestView : UIView {
var title: String?
var rank: Int?
init(title: String, rank: Int) {
self.title = title
self.rank = rank
super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// 2 create a static func, set title with a function
static func setMyView (rank: Int, aFunc : () -> Void ) {
aFunc()
}
}
public class AutoCTest {
var view : TestView = TestView.init(title: "private", rank: 1)
// 4 instead, create an autoclosure function
func setMyView(rank: Int = 5, _ setting: @autoclosure @escaping () -> Void) {
TestView.setMyView(rank: rank, aFunc: setting)
}
func doIt () {
// 3 set the title with a closure
// do it the old way
TestView.setMyView(rank: 5) {
view.title = "admiral"
}
// 5 set the title with an autoclosure
// do it the new way, use my autoclosure function, no braces required
setMyView(self.view.title = "captain")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment