“This is the 26th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

This section describes the keywords of access control permission in SWIFT

private

1. Private: Access level is only valid within the currently defined scope (used in singletons)

//8-1, private: Access level 'is only valid in the currently defined scope
class CJLTeacher{
    static let shareInstance = CJLTeacher(a)private init(a){}}var t = CJLTeacher.shareInstance
Copy the code

filePrivate

FilePrivate: Access restrictions are limited only to the currently defined source file

<! --1,Defined in the access.swift fileCJLPartTimeTeacher-->
fileprivate class CJLPartTimeTeacher: CJLTeacher{
    var partTime: Double?
    init(_ partTime: Double) {
        super.init(a)self.partTime = partTime
    }
}

<! --2,Not accessible in main.swiftCJLPartTimeTeacher-->
Copy the code

Swift (CJLPartTimeTeacher) {CJLPartTimeTeacher (CJLPartTimeTeacher); The permission of PT is greater than that of CJLPartTimeTeacher, the system does not allow this, so there is an error

  • You need to useprivate / fileprivateModified pt
private let pt = CJLPartTimeTeacher(10.0)
/ / or
fileprivate let pt = CJLPartTimeTeacher(10.0)
Copy the code

If you define it directly in a method, you can dispense with the access modifier

func test(a){
    let pt = CJLPartTimeTeacher(10.0)}Copy the code

Internal

Internal: The default access level that allows access to any source file in the defined module, but not to any source file outside the module (for example, import Foundation, where Foundation is a module)

<! --1,main.swift-->
import Foundation
class CJLTeacher{
    init(a){}}let t = CJLTeacher(a)<! --2,custom-->
import AppKit
Swift (t) : Expressions are not allowed at the top level
print(t)
Copy the code

The main reason for the error is that the default permission of T in main.swift file is Internal, which can only be used in the same module, which belongs to Foundation module, while custom. Swift file cannot call T, because it belongs to AppKit module. It is not the same module as Foundation, so it cannot be accessed

public

Public: Open access, allowing us to use code in any source file in which the module is defined, and to access the source file from another source file. But can only be inherited and subclassed in defined modules

open

Open: The most unrestricted level of access that can be inherited, defined, and overridden anywhere and between modules

The difference between public and Open:

  • Public not inheritable
  • The open inheritable

Conclusion:

  • Without the write access control permission keyword, the default access permission is internal

  • Access control permissions in descending order: Open > Public > Internal > filePrivate > Private

    • 1.private: Access levelValid only in the currently defined scope
    • 2,filePrivate: Access restrictionOnly in the currently defined source file
    • 3,Internal: The default access level that allows access to any source file in the defined module, but not to any source file outside the module
    • 4,public: Open access, allowing us to use code in any source file in which the module is defined, and to access the source file from another source file. butCan only be inherited and subclassed in defined modules
    • 5,open: The most unrestricted level of access that can be inherited, defined, and overridden anywhere and between modules