iOS Development swif Language basic parameters, functions, methods definition and invocation

Source: Internet
Author: User

The teacher told the knowledge and everyone to share the next, I hope you ask questions, this is my comprehensive understanding and the teacher said to knock out!

var str = "Hello, playground"

Defining variables

Let A0:int = 5;

Defining functions

Func Test () {

Print ("ASDFG")

}

Test ()

To define parameters with parameters and return values

Func test1 (a:int)->int {

return 100;

}

Test1 (100)

Swift language code can be added later, or it can be finished without adding

Define the order of the parameters, preferably the same as the order in which the variables are declared

By default, its arguments are constants

To external name: Caller column for When: add

Add:to:

Func Add (A:int, to B:int)->int {

return a + B;

}

Add (ten, To:10)

Underline _ can not display parameter name definition column such as: ADD2

Func Add2 (A:int, _ B:int)->int {

return a + B;

}

ADD2 (10, 10)

Defining a variable is a default value that can be added to it, but you can modify its default value column when calling this method, such as: ADD3

Func add3 (A:int = ten, B:int =)->int {

return b/a;

}

ADD3 ();

ADD3 (5, b:100);

Func add4 (Initnum:int,numbers:int ...), Int {

var totals = Initnum;

For num in numbers {

totals + = num;

}

return totals;

}

ADD4 (5, numbers:1,2,3)

In addition to this definition of add4, we can also add5 this definition, but we do not recommend this definition

Func add5 (numbers:int...,initnum:int)->int {

var totals = Initnum;

For num in numbers {

totals + = num;

}

return totals;

}

ADD4 (5, numbers:1);

Func Vtest (var a:int)->int {

return (++A);

}

var m = 6;

Vtest (M)

Let result = Vtest (M);

Func vtest2 (inout a:int)->int {

a++;

return A;

}

Vtest2 (&M);

String

var str = "Hello";

Let i = 6;

var str2 = "hello\ (i)"//interpolation method

Let a = "ASDFG";

A.characters.count; Number of strings

a.startindex;//the starting subscript of a string 0

a[a.startindex];//the corresponding character of subscript 0

A.endindex;

Total number of A.endindex//subscripts

A[a.startindex.successor (). Successor (). Successor ()];//a third corresponding character after subscript 0

A[a.startindex.successor (). predecessor ()]//the next subscript of the small mark 0 the corresponding character of the previous subscript

A.startindex.successor (). Advancedby (3);//Subscript 0 after the next 3 small mark after the underlying

A.substringfromindex (A.startindex.advancedby (2));//starting from subscript 2 to the last string

A.substringfromindex (A.endindex.advancedby (-3))//from Back to back, intercept the next three strings

Let ran = range (Start:a.startindex, End:a.startindex.advancedby (4));//Range

A.substringwithrange (RAN);

var str3 = "Hello work";

Str3.hasprefix ("H");//Determine if the opening argument is preceded by H

Str3.hassuffix ("K")//Determine if the opening parameter is terminated by K

iOS Development swif Language basic parameters, functions, methods definition and invocation

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.