Differences between OC and Swift 5. Differences between OCSwift 5.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.