1, NSClassFromString
guard let workName = Bundle.main.infoDictionary!["CFBundleExecutable"] else {
print("Namespace does not exist")
return
}
let cls: AnyClass? = NSClassFromString("\(workName as! String).\(vcs[indexPath.row])")
guard let vc = cls as? UIViewController.Type else {
print("Failed to convert to VC.")
return
}
navigationController?.pushViewController(vc.init(), animated: true)
Copy the code
When you use NSClassFromString, you need to concatenate the project name, otherwise you return nil because of the namespace
2, the Timer
Timer, listen for count, use didSet, and accordingly, willSet method
var count: Float = 0.0 {
didSet(oldValue) {
view1.text = String(format: "%.1f", count)
}
}
Copy the code
This is how to print only one decimal place.
Pause timer
@objc func end(a) {
if state = = .end {
return
}
state = .end
timer?.fireDate = Date.distantFuture / / * *
}
Copy the code
Continue timer
@objc func start(a) {
if state = = .start {
return
}
if timer = = nil {
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(change), userInfo: nil, repeats: true)
}
state = .start
timer?.fireDate = Date.distantPast
}
Copy the code
reset
@objc func reset(a) {
if state = = .reset {
return
}
state = .reset
count = 0.0
timer?.invalidate()
timer = nil
}
Copy the code
@objc indicates that this is an oc method, because Button adds methods when it initializes
view2.addTarget(self, action: #selector(start), for: .touchUpInside)
Copy the code
To be aware of the timer’s circular references, use the OC approach. To determine whether the page is free, print the judgment in the following method
deinit {
print("deinit")}Copy the code
3. Class naming
Oc and SWIFT can’t start with a number, so I accidentally created a new file called 3dtouchvc. swift and found that the internal generated class named _DTouchVC still works
4. Initialize
When I rewrite the init method of the ViewController, I keep asking you to
'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'
Copy the code
Required initializers, classes in the UIView family, classes in the UIViewController family, are added because those classes comply with the NSCoding Protocol.
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")}Copy the code
The construction method in Swift is strict, and the key words are Designated, Convenience and Required.
Designated drink Each class has at least one Designated drink and a subclass can inherit one of its parent class.
Convenience, must be used in the same class, internal self.init, subclasses cannot inherit
Required, subclasses must implement the constructor of their parent class
class Animal {
var type: String
var name: String
// Designated benches can be inherited by subclasses
init(_ type: String.name: String) {
self.type = type
self.name = name
}
// Subclasses must be inherited
required init(_ type: String) {
self.type = type
self.name = ""
}
// Subclasses cannot inherit
convenience init(name: String) {
self.init("", name: name)
}
}
class Dog: Animal {
override init(_ type: String.name: String) {
super.init(type, name: name)
}
required init(_ type: String) {
super.init(type)
}
}
Copy the code
To be continued…