1,swift protocol is similar to the interface in other languages, the Protocol only makes the declaration of methods, including the method name, return value, parameters and other information, and no specific method implementation.
Protocol person { //read-write property var nprotocol Person {
// Read and write attributes
var name: String {get set}
// Read-only property
var age: Int {get}
// type method
static func method1 ()
// Instance method
func method2 ()-> Int
// mutation method
mutating func method3 ()
}
2, the protocol can inherit another protocol
protocol Animal {
func move()
}
protocol Bird:Animal{
func song()
}
class Chiken: Bird {
func song() {
print("gege")
}
func move() {
print("run")
}
}
3, if a class set inherits a parent class and a protocol is followed, then the colon should be preceded by the parent class, and then the protocol is written
Class CC: Inherited parent class, protocol 1, Protocol 2{}
Class extension (extension) 1, adding computed properties to an existing class and evaluating static properties 2, defining a new instance method and class method 3, providing a new constructor 4, defining subscript script 5, is an existing type that conforms to a protocol individual modifier differences
- Properties or methods that are modified by the private access level can only be accessed in the current class.
- The properties or methods modified by the Fileprivate access level are accessible in the current Swift source file.
- Internal (default access level, internal modifier can be written without writing)
The properties or methods that the internal access level modifies can be accessed throughout the module where the source code resides.
- If it is a frame or library code, it is accessible throughout the framework and is not accessible when the framework is referenced by external code.
- If it is an app code, it is also available throughout the app code and within the entire app.
- Public can be accessed by anyone. However, other module can not be override and inherit, and within module can be override and inherit.
- Open can be used by anyone, including override and inheritance
Swift-Protocol, Class extension (extension), access control (Fileprivate,private,internal,public,open)