Swift Common library functions

Source: Internet
Author: User

//countelements entry is <t: _collectiontype> (x:t) return value t.index.distance This demo returns

println (countelements ("Hello Swift"))//11

//0:a 1:b 2:c returns a new sequence where each element is a tuple, the first value is the position where the original element is ' index ', and the second is the element in the original sequence

for (I,J) in Enumerate (["A", "B", "C"])

{

println ("\ (i): \ (j)")

}

//Returns the absolute value of the number

println (ABS ( -100))//100

println (ABS (565))//565

//contains (sequence, Element): Returns true if a sequence sequence (for example, an array) contains the specified element elements, otherwise false

println (Contains (["Swift", "Objective-c"], "Swift")//true

println (Contains (["Swift", "objective-c"], "Jave"))//false

//dropfirst (Sequence): Returns a new sequence (such as a new array) with the first element removed.

println (Dropfirst (["Swift", "Objective-c", "Java", "C #"]))//[objective-c, Java, C #]

//droplast (Sequence): Returns a new sequence (such as a new array) with the last element removed.

println (Droplast (["Swift", "Objective-c", "Java", "C #"]))//[swift, Objective-c, Java]

//dump (object): Prints out all the information about an object, including the number of elements,

4 Elements

-[0]: Swift

-[1]: Objective-c

-[2]: Java

-[3]: C #

[Swift, Objective-c, Java, C #]

println (Dump (["Swift", "Objective-c", "Java", "C #"]))

//equal (Sequence1, Sequence2): Determine if two sequences are equal

println (Equal (["Swift", "Objective-c", "Java", "C #"], ["Swift", "Objective-c", "Java", "C #"]))//true

println (Equal (["Swift", "Objective-c", "Java", "C #"], ["Swift", "Objective-c", "Java", "C #", "Javascript"]))// False

//filter (Sequence, includeelementclosure): includeelementclosure closures are executed for each element in the sequence sequence, And all the elements of the closure result as true are synthesized into a new sequence sequence and returned

For I in filter (1...100, {$0%10 = = 1}) {

println (i)//1,11,21,31,41,51,61,71,81,91

}

println (Filter (1...100, {$0%10 = = 1}))//[1, 11, 21, 31, 41, 51, 61, 71, 81, 91] different from the map function

//find (sequence, Element): Returns the position of an element in the sequence sequence index. If this element does not exist in the sequence, nil is returned

println (Find (["Swift", "Objective-c", "Java", "C #", "Javascript"], "Java"))//optional (2)

println (Find (["Swift", "Objective-c", "Java", "C #", "Javascript"], "C + +"))//nil

//indices (Sequence): Returns the position of all elements in the sequence sequence (indices is the plural of index)

println (Indices (["Swift", "Objective-c", "Java", "C #", "Javascript"]))//0..<5

//join (separator, sequence): Converts a sequence sequence through a delimiter separator into a string and returns this string

var lang = join (":", ["Swift", "Objective-c", "Java", "C #", "Javascript"])

println (lang)//swift:objective-c:java:c#:javascript

//map (Sequence, transformclosure): Performs a includeelementclosure closure on each element in the sequence sequence and synthesizes the results of all closures into a new sequence sequence and returns The difference from filter returns is the sequence of elements that have a true result

println (Map (1...10, {$0%3 = = 1}))//[true, False, False, True, False, False, True, False, False, true]

//maxelement (Sequence): Returns the maximum value in the sequence sequence.

println (Maxelement (1...100))//100

println (Maxelement (["Swift", "Objective-c", "Java", "C #", "Javascript"]))//swift Here's a question about why an array with numbers is an error.

//minelements (Sequence): Returns the minimum value in the sequence sequence

println (Minelement (["Swift", "Objective-c", "Java", "C #", "Javascript"]))//c#

//reduce (sequence, initial, combineclosure): Given a sequence sequence, and an initial value of initial, The initial and the 1th element in the sequence are then passed into the combineclosure as a parameter, and the resulting results are saved to initial, and then the initial and 2nd elements are passed into the combineclosure for calculation. The result is saved to initial, repeated until all elements in the sequence have been computed, and the final initial value is returned

println (Reduce (["Swift", "Objective-c", "Java", "C #", "Javascript"], "/", {$ +}))///swiftobjective-cjavac# Javascript

println (Reduce ([10,20,5,3], 1, {$ +}))//39

//reverse (Sequence): Returns the sequence in reverse order sequence

println (Reverse (["Swift", "Objective-c", "Java", "C #", "Javascript"]))//[javascript, C #, Java, objective-c, Swift]

//startswith (Sequence1, Sequence2): Returns True if the element beginning in the sequence Sequence1 is equal to all elements in the sequence Sequence2, otherwise returns false That is, the element in sequence two is a subset of the beginning of the element in sequence one .

println (StartsWith ("Helloswift", "Hello"))//true

println (StartsWith ("Helloswift", "HELLP"))//false

println (StartsWith (["Swift", "Objective-c", "Java", "C #", "Javascript"],["Swift", "Objective-c", "Java"]))//true

println (StartsWith (["Swift", "Objective-c", "Java", "C #", "Javascript"],["Swift", "Objective-c", "Java", "C + +"])) False

//Exchange two value two after the parameter is inout type solid exchange, the two values change

var oneint = 3

var anotherint = 903

Swap (&oneint,&anotherint)

println ("Oneint:\ (Oneint), anotherint:\ (Anotherint)")//oneint:903,anotherint:3

Swift Common library functions

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.