Differences between OC and Swift 5. Differences between OCSwift 5.
13 Functions
Oc Function Definition:
Return Value Type Function Name (parameter type parameter name, parameter type parameter name ){
}
Swift Function Definition:
Func function name (Parameter Name: parameter type, parameter name: parameter type)-> return value type {
}
Functions without return values in swift can be written in three ways:
13.1
Func function name (Parameter Name: parameter type, parameter name: parameter type)-> Void {
}
13.2
Func function name (Parameter Name: parameter type, parameter name: parameter type)-> (){
}
13.3
Func function name (Parameter Name: parameter type, parameter name: parameter type ){
}
Parentheses after function names without row parameters in swift cannot be omitted.
Function Definition of the returned tuples:
Func function name (Parameter Name: parameter type, parameter name: parameter type)-> (element name: Element type, tuples name: Element type ){
}
External Parameter Name in swift
Func function name (Form of External Parameter Name: parameter type, form of External Parameter Name: parameter type)-> return value type {
}
// If # is added before the formal parameter name, the external parameter name is the same as the formal parameter name
Func function name (# form parameter name: parameter type, # form parameter name: parameter type)-> return value type {
}
When a function with an external parameter name is added to a call, the compiler automatically generates a call with an external parameter name, and the external parameter name must be added during the call, this increases the readability of the caller's parameters.
For example
Func addPerson (person_name: String, person_age: Int ){
}
AddPerson (person_name: "jack", person_age: 28)
Default parameter values in swift
Func function name (Form of External Parameter Name: parameter type = parameter default value, form of External Parameter Name: parameter type)-> return value type {
}
When you call a function with the default value, you do not need to pass a value for a parameter with the default value. If you do not specify an External Parameter with the default value, swift automatically generates an external parameter name with the same name as the row parameter. If _ is added before the number of row parameters with the default value, the external parameter name does not need to be written during the call.
Note: The parameter in oc can be modified in the function body, because it is similar to a local variable. However, in swift, parameters are constant parameters by default, and parameter values cannot be modified in the function body. If you need to modify the parameter value in the function body, you need to define the variable parameter, that is, add the var keyword before the parameter name.