Introduction to Swift's 74 common built-in functions _swift

Source: Internet
Author: User
Tags abs assert closure min

Swift contains 74 built-in functions, but only 7 of them are presented in the Swift programming langage, none of which is reflected in the documentation.

This article lists all the Swift library functions. The so-called built-in function refers to a function that can be used directly without introducing any modules (such as fundation, etc.).

Let's take a look at 7 library functions mentioned in the document:

The following is a list of useful library functions that are not reflected in the document:

Copy Code code as follows:

Assertion, if the argument is ' true ', continue, or throw an exception
Assert mentioned on page 55
ASSERT (True)

Count the number of elements in a sequence
Countelements mentioned on page 79
Countelements ("foo") = = 3

Returns a new sequence in which each element is a tuple,
The first value is the location of the original element ' index ' and the second is the element in the original sequence
Enumerate mentioned on page 94
For (I, J) in Enumerate (["A", "B"]) {
"0:a", "1:b" would be printed
println ("\ (i): \ (j)")
}

Returns the minimum value in all parameters
Min mentioned on page 246
Min (8, 2, 3) = 2

Print
Print mentioned on page 85
Print ("Hello")

Print (with line wrap)
println mentioned on page 4
println ("World")

Sort
Sort mentioned on page 14
For I in sort (["B", "A"]) {
"A", "B" would be printed
println (i)
}


ABS (Signednumber): Returns the absolute value of a number

Copy Code code as follows:

ABS (-1) = = 1
ABS (-42) = = 42
ABS (42) = 42

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

Copy Code code as follows:

var languages = ["Swift", "objective-c"]
Contains (languages, "Swift") = = True
Contains (languages, "Java") = = False
Contains ([I, O, I, m,] = = True

Dropfirst (Sequence): Returns a new sequence (such as a new array) that removes the first element.

Copy Code code as follows:

var languages = ["Swift", "objective-c"]
var oldlanguages = Dropfirst (Languages)
Equal (Oldlanguages, ["objective-c"]) = = True

Droplast (Sequence): Returns a new sequence (such as a new array) that removes the last element.
Copy Code code as follows:

var languages = ["Swift", "objective-c"]
var newlanguages = Droplast (Languages)
Equal (Newlanguages, ["Swift"]) = = True

Dump (object): Prints out all information about an object
Copy Code code as follows:

var languages = ["Swift", "objective-c"]
Dump (Languages)
Prints:
▿2 elements
-[0]: Swift
-[1]: Objective-c


Equal (Sequence1, SEQUENCE2): Determining whether two sequences are equal
Copy Code code as follows:

var languages = ["Swift", "objective-c"]
Equal (languages, ["Swift", "objective-c"]) = = True
var oldlanguages = Dropfirst (Languages)
Equal (Oldlanguages, ["objective-c"]) = = True

Filter (sequence, includeelementclosure): Performs a includeelementclosure closure on each element in the sequence sequence. A new sequence sequence is synthesized and returned for all elements with the closure result being true.
Copy Code code as follows:

For I in filter (1...100, {$ 10 = 0}) {
10, 20, 30, ...
println (i)
ASSERT ([A, M, contains, MB, II,], (i))
}

Find (sequence, Element): Returns the position index of an element in a sequence sequence. Returns nil If this element does not exist in the sequence.

Copy Code code as follows:

var languages = ["Swift", "objective-c"]
Find (Languages, "objective-c") = = 1
Find (Languages, "Java") = = Nil
Find ([29, 85, 42, 96, 75], 42) = = 2

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

Copy Code code as follows:

Equal (indices ([29, 85, 42]), [0, 1, 2])
For I in indices ([29, 85, 42]) {
0, 1, 2
println (i)
}

Join (separator, sequence): connects the sequence sequence through a separator separator into a string, and returns this string.
Copy Code code as follows:

Join (":", ["A", "B", "C"]) = = "A:b:c"
var languages = ["Swift", "objective-c"]
Join ("/", languages) = = "Swift/objective-c"

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.

Copy Code code as follows:

Equal (map (1...3, {$ * 5}), [5, 10, 15])
For I in map (1...10, {$ * 10}) {
10, 20, 30, ...
println (i)
ASSERT ([A, M, contains, MB, II,], (i))
}

Max (Comparable1, Comparable2, etc.) : Returns the maximum value in the parameter.
Copy Code code as follows:

Max (0, 1) = 1
Max (8, 2, 3) = 8

Maxelement (Sequence): Returns the maximum value in the sequence sequence.
Copy Code code as follows:

Maxelement (1...10) = 10
var languages = ["Swift", "objective-c"]
Maxelement (languages) = "Swift"

Minelements (Sequence): Returns the minimum value in a sequence sequence.
Copy Code code as follows:

Minelement (1...10) = 1
var languages = ["Swift", "objective-c"]
Minelement (languages) = "Objective-c"

Reduce (sequence, initial, combineclosure): Given a sequence of sequence, and an initial value of initial, The initial and the 1th element in the sequence are then used as parameters in the combineclosure, the resulting results are saved to the initial, and then the initial and 2nd elements are computed in the combineclosure. The results are saved to initial until the elements in all sequence are evaluated and the final initial value is returned.

Copy Code code as follows:

var languages = ["Swift", "objective-c"]
Reduce (Languages, "", {$ +}) = = "Swiftobjective-c"
Reduce ([10, 20, 5], 1, {$ * $}) = = 1000

Reverse (Sequence): Returns the sequence sequence of the reverse order.

Copy Code code as follows:

Equal (reverse ([1, 2, 3]), [3, 2, 1])
For i in reverse ([1, 2, 3]) {
3, 2, 1
println (i)
}

StartsWith (Sequence1, Sequence2): Returns True if the element at the beginning of the sequence Sequence1 is equal to all elements in the sequence SEQUENCE2, otherwise it returns false.

Copy Code code as follows:

StartsWith ("Foobar", "foo") = = True
StartsWith (10..100, 10..15) = = True
var languages = ["Swift", "objective-c"]
StartsWith (Languages, ["Swift"]) = = True

The functions mentioned above are functions that I think will be used frequently in swift programming.

Full 74 built-in functions:

Copy Code code as follows:

ABS (...)
Advance (...)
Alignof (...)
Alignofvalue (...)
ASSERT (...)
Bridgefromobjectivec (...)
Bridgefromobjectivecunconditional (...)
Bridgetoobjectivec (...)
Bridgetoobjectivecunconditional (...)
C_malloc_size (...)
c_memcpy (...)
C_putchar (...)
Contains (...)
Count (...)
Countelements (...)
Countleadingzeros (...)
DebugPrint (...)
Debugprintln (...)
Distance (...)
Dropfirst (...)
Droplast (...)
Dump (...)
Encodebitsaswords (...)
Enumerate (...)
Equal (...)
Filter (...)
Find (...)
Getbridgedobjectivectype (...)
Getvalist (...)
Indices (...)
Insertionsort (...)
Isbridgedtoobjectivec (...)
Isbridgedverbatimtoobjectivec (...)
Isuniquelyreferenced (...)
Join (...)
Lexicographicalcompare (...)
Map (...)
Max (...)
Maxelement (...)
Min (...)
Minelement (...)
Numericcast (...)
Partition (...)
Posix_read (...)
Posix_write (...)
Print (...)
println (...)
QuickSort (...)
Reduce (...)
Reflect (...)
Reinterpretcast (...)
Reverse (...)
Rounduptoalignment (...)
sizeof (...)
Sizeofvalue (...)
Sort (...)
Split (...)
StartsWith (...)
Strideof (...)
Strideofvalue (...)
Swap (...)
Swift_magicmirrordata_summaryimpl (...)
Swift_bufferallocate (...)
Swift_keepalive (...)
ToString (...)
Transcode (...)
Underestimatecount (...)
Unsafereflect (...)
Withextendedlifetime (...)
Withobjectatpluszero (...)
Withunsafepointer (...)
Withunsafepointertoobject (...)
Withunsafepointers (...)
Withvalist (...)

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.