On the changes of swift 4.0
- 2020-06-07 05:23:05
- OfStack
preface
When Swift first introduced access levels, there was some confusion and confusion. While developers are excited about adding access control to the Swift programming language, the private keyword behaves differently from other programming languages.
Previously, in swift 3.0, declared variables or methods with the private prefix were used only in the current class and not in extension. Changing to fileprivate, which can be instantiated in other classes, makes the scope of the attribute larger and can inadvertently lead to misuse of the attribute.
So in Swift 4, the attributes of private are extended to extension and are restricted to struct and extension so that there is no need to change to fileprivate.
What are the new es23EN 4.0?
String is a more user-friendly, multi-line character string, Range is supported, and is also a collection type Improved ACCESS to private, private access modifier Smarter and safer Key Value Coding key value encoding Dictionaries and Collections Archiving and serialization Single range General subscript (__) 3. Related type restriction Classes and protocols exist Limit @objc inferenceThe following is the main introduction of this paper:
In the code
class ViewController: UIViewController {
var test = ""
private var test1 = ""
fileprivate var test2 = ""
override func viewDidLoad() {
super.viewDidLoad()
testIt() // An error was reported before I need to change it to fileprivate
}
func testForNormal(){
}
private func testForPrivate(){
}
fileprivate func testForfileprivate(){
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController {
private func testIt(){
testForPrivate() /// swift 4.0 You can visit Not before
}
}
class other {
let a = ViewController()
func lalal(){
_ = a.test /// Normal
_ = a.test2 /// fileprivate
a.testForNormal() /// Normal
a.testForfileprivate() /// fileprivate
a.testForPrivate() /// An error 'testForPrivate' is inaccessible due to 'private' protection level
}
}
conclusion