======== @objc =============
Introduction to @objc use in swift3, the compiler automatically infers @objc, in other words, it automatically adds @objc
In Swift4, the compiler no longer automatically infers that you must explicitly add @objc
In Swift if a button adds a click method if defined as private or defined as fileprivate then the private method cannot be found in the Addtaget method
But do not want to expose the method, to avoid outside access, then you can add @objc decoration before the private method so that it can find the way.
@objc role
1 Fileprivate or private guarantee that the method private can access this method in the same class or in the same file (extension) if defined as private then access in only one class cannot be accessed in class extensions
2 allows this function to be invoked at runtime via the OC's message mechanism ============== extension=======================
The extension declaration uses the keyword extension:
Extension SomeType {
The new features added to SomeType are written here
}
An extension can extend an existing type so that it can fit one or more protocols, and the syntax format is as follows:
Extension Sometype:someprotocol,anotherproctocol {
Protocol implementation written here
}
========================================is and as type conversions
Swift language type conversions can determine the type of an instance. It can also be used to detect whether an instance type belongs to an instance of its parent class or subclass.
Type conversions in Swift are implemented using the IS and as operators, is used to detect the type of the value, as for the conversion type.
Type conversions can also be used to check whether a class implements a protocol.
Transition downward, using type conversion operators (as or as!)
When you are unsure of how a downward transition can succeed, use a type-converted conditional form (as?). The type conversion of a conditional form always returns an optional value (optional value), and if the next turn is not possible, the optional value will be nil.
Only you can be sure that the downward transition will succeed when the Force form (as!) is used. Coercion of type conversions triggers a run-time error when you attempt to turn downward into an incorrect type.
var chemcount = 0
var mathscount = 0
For item in SA {
Conditional form of type conversion
If let show = Itemas? Chemistry {
Print ("Chemical subject is: ' (show.physics) ', \ (show.equations)")
Mandatory form
else Iflet example = Itemas? Maths {
Print ("Math topic is: ' (example.physics) ', \ (example.formulae)")
}
}
Type conversions are used to detect whether an instance type belongs to a particular instance type.
You can use it on the hierarchy of classes and subclasses, examine the types of instances of a particular class, and convert the type of the class instance into other types in this hierarchy.
Type checking uses the IS keyword.
The operator is to check whether an instance belongs to a particular subtype. If the instance belongs to that subtype, the type check operator returns TRUE, otherwise it returns false.
var chemcount = 0
var mathscount = 0
For item in SA {
Returns true if it is an instance of a chemistry type, and returns false instead.
If Item Ischemistry {
++chemcount
else if itemismaths {
++mathscount
}
}
Swift provides two special types of aliases for uncertain types: Anyobject can represent instances of any class type. Any can represent any type, including the method type (function types). ======================== access Control modifier ===========================
Protocols can also be limited to a range of uses, including global constants, variables, and functions in the Protocol.
Access control is based on modules and source files.
A module refers to a Framework or application that is built and published as a stand-alone unit. A module in Swift can introduce another module using the Import keyword.
A source file is a single source file that typically belongs to a module, and the source file can contain definitions of multiple classes and functions.
Swift provides four different levels of access to entities in your code: public, internal, fileprivate, private.
access Level |
definition |
Public |
You can access any entity in the source file in your module, and others can access all the entities in the source file by introducing the module. |
Internal |
You can access any entity in the source file in your module, but others cannot access the entities in the source file in the module. |
Fileprivate |
The file is private and can only be used in the current source file. |
Private |
can only be accessed in a class, leaving the outside side of the class or structure body inaccessible. |
Public is the highest level of access and private is the lowest level of access. Entities use the default access level internal unless there is a special description.
function type access rights
The access level of a function needs to be based on the access level of the function's parameter type and return type.
The following example defines a global function called somefunction and does not explicitly declare its access level.
Func someFunction ()-> (Someinternalclass,someprivateclass) {
function implementation
}
One of the classes in the function Someinternalclass the access level is internal, and the other Someprivateclass access level is private. So according to the principle of the tuple access level, the access level of the tuple is private (the access level of the tuple is the same as the type with the lowest access level in the tuple).
Because the access level of the function return type is private, you must explicitly declare the function by using the private modifier:
Private func someFunction ()-> (Someinternalclass,someprivateclass) {
function implementation
}
Declaring the function public or internal, or using the default access level internal, is incorrect because you cannot access the return value at the private level.
Enumeration type access rights
The access level of the member in the enumeration inherits from the enumeration, and you cannot declare different access levels for the members of the enumeration individually.
For example, an enumeration Student is explicitly declared public, and its members Name,mark access levels are also public:
Instance
public enum Student {
Case Name (String)
Case Mark (Int,int,int)
}
var studdetails = student.name ("Swift")
var studmarks = Student.mark (98,97,95)
Switch Studmarks {
Case. Name (Let Studname):
Print ("Student name: \ (studname).")
Case. Mark (Let Mark1,let mark2,let Mark3):
Print ("Student score: \ (MARK1), \ (MARK2), \ (MARK3)")
}
The access level of a subclass must not be higher than the access level of the parent class. For example, the access level of the parent class is internal, and the access level of the subclass cannot be declared public.
Constants, variables, and properties cannot have a higher level of access than their type.
For example, you define a public-level attribute, but its type is private, which is not allowed by the compiler.
Similarly, the subscript cannot have a higher access level than the index type or return type.
If the defined types of constants, variables, properties, and subscript indexes are private, they must explicitly declare the access level to be private:
The access levels of constants, variables, properties, subscript indexes, getters, and setters are inherited from the access level of the members to which they belong.
The setter's access level can be lower than the corresponding getter's access level, so that you can control the read and write permissions of the variable, property, or subscript index.
================== can be selected ========