preface

After using Swift for so long, WHEN I was reconstructing the project, I found that I still did not have a deep understanding of many basic things, and I still needed to refer to them from time to time. I hereby study hard to record this, and I am sorry for any improper understanding

1. Permission modifier keywords

private :

  1. privateThe modified property or method can only be used in the current class; When the call is not in the current class, even the current class-related object cannot be used
  2. privateModification of theClassorStructTo use in addition to oneself; Others are not allowed (including class inheritance and instantiation), so they are not used in general developmentprivatemodifiedClassStruct
  3. privateModification of theprotocolThis is only allowed in the current fileprotocol; Everything else is disallowed (including type declarations, parameter declarations in functions or methods)

filePrivate:

  1. filePrivateModifies a property or method that can only be used in the current file; Within the current file, can be called across classes
  2. fileprivatemodifiedClassorStruct.ClassIt can be instantiated and inherited within the current file, and is not allowed to be used anywhere outside the current file
  3. fileprivateModification of theprotocolAnd the sameprivatemodifiedprotocolThe effect is the same, it’s only allowed to follow in the current file, it’s not allowed to use function and method parameter declarations

internal:

Swift default permission modifier keyword, generally not declared, only allowed in the current module, inherit, overwrite

public:

  1. publicModified properties,StructandprotocolTo allow acrossmoduleuse
  2. publicModification of theClassTo allow acrossmoduleUse, butpublicModification of theClassIs not allowed to crossmoduleinheritance
  3. publicDecorated methods that allow crossmoduleUse; But not allowed to crossmoduleOverride even if the method is adopted by the classopenmodified

open:

  1. openModified properties, andpublicConsistent because stored properties are not allowed to be overridden
  2. openNo retouching allowedprotocol.Struct.StructMethod, because of the open modifier, means that it can be overloaded
  3. openModifier method that allows crossmoduleCall and override
  4. openmodifiedclassTo allow acrossmoduleUsage and inheritance

summary

Permissions are sorted in ascending order: private < Fileprivate < internal < public < open

2.1 Compiled keywords

static:

  1. No retouching allowedclass.struct.protocolThe function
  2. staticThe modifier method is not overridden and therefore cannot be used simultaneouslyopenmodified
  3. static, the modification method is distributed directly

dynamic:

  1. Dynamic, also does not allow decorationclass.struct.protocol
  2. usingdynamicModified functions and methods that are dispatched using the runtime messaging mechanism

@objc :

  1. allowObjective-CThe call,@objcThe modified class, property, or method that is injected into the current modified property or methodObjective-CThe runtime allowsObjective-CIs distributed at runtime
  2. @objcNo retouching allowedstructThe function
  3. Inheritance toNSObjectClass, properties, and methods are adopted by default@objcModified, no need to add@objcmodified

@nonobjc:

  1. Modified classes, properties, and methods are not allowed to be injected intoObjective-CAt runtime, this modified property is disabled, and the method is message-distributive, that is, not allowedObjective-CIn the call
  2. @nonobjcIt is not allowed to modify classes, structures, protocols, and functions

@inline(options):

  1. The compiler will make relevant inline decisions based on the optimization Settings of the project, which of course also has an impact on the size of the binary file, as well as the speed of compilation
  2. option: __always, indicating that if possible, you want the compiler to inline the currently decorated method. If possible, there will be cases where inlining is not generated, such as when dynamic distribution is forced
  3. option: neverIs not inlined
  4. Only functions or methods can be decorated

@inlinable:

  1. If a function or method is declared as an inline function, the compiler will replace the function call with a concrete implementation at compile time. This can save the time of the function call, thus achieving the purpose of performance optimization. The trade-off is that you will increase the size of the binary file slightly
  2. Inlining can be implemented across modules
  3. Only functions or methods can be decorated

final:

  1. No modification of the structure is allowed,procotolThe function
  2. Direct distribution
  3. A modified class or method cannot be inherited or overridden

mutating:

In the instance method used to modify a structure, when modifying a structure property value

@escaping:

Modifies a closure to prevent it from being destroyed when a method or function returns, and is called allowing the closure to escape

summary

Reasonable use of keywords can effectively improve application performance; But remember not to use the modifier @inline(__always) when you don’t know how to use it, because the inline of this modifier is variable, so it’s best not to use it unless you know what you’re doing (my existing knowledge tells me to do so).

2.2 Function modifier keyword corresponding to the result of compilation

There are some missing modifiers in the compilation results corresponding to each compilable keyword

3. Summary

There are too many keywords in Swift. We will continue to sort them out later. If you have any improper understanding, please kindly correct them

As for the distribution mechanism mentioned in the appeal, here is a brief explanation: @nonobjc is a mechanism for swift method calls. For more details, see Understanding swift Dispatchmechanism and also to explain the difference between @nonobjc and final. If @nonobjc does not use final or static, Is, it is possible to use either function table distribution or direct distribution, depending on the specific method implementation; The final modification method only uses direct distribution