1. 程式人生 > >Swift Standard Library

Swift Standard Library

Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift Programming Language”). The rest remain undocumented.

This article lists all built-in Swift functions – both documented and undocumented ones. The definition used for “built-in function” used in this article is a function available in Swift without

importing any modules (such as Foundation, etc.) or referencing any classes.

Let’s start with the seven documented built-in functions mentioned in the Swift book along with the page number on which the function was first mentioned:

[objc] view plaincopyprint?
  1. // assert mentioned on page 55
  2. assert(true)  
  3. // countElements mentioned on page 79
  4. countElements("foo") == 3
  5. // enumerate mentioned on page 94
  6. for (i, j) in enumerate(["A""B"]) {  
  7.     // "0:A", "1:B" will be printed
  8.     println("\(i):\(j)")  
  9. }  
  10. // min mentioned on page 246
  11. min(823) == 2
  12. // print mentioned on page 85
  13. print("Hello "
    )  
  14. // println mentioned on page 4
  15. println("World")  
  16. // sort mentioned on page 14
  17. for i in sort(["B""A"]) {  
  18.     // "A", "B" will be printed
  19.     println(i)  
  20. }  

Now on to the most useful undocumented functions …

abs(signedNumber): Returns the absolute value of a given signed number. Trivial but not documented.

[objc] view plaincopyprint?
  1. abs(-1) == 1
  2. abs(-42) == 42
  3. abs(42) == 42

contains(sequence, element): Returns true if a given sequence (such as an array) contains the specified element. [objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. contains(languages, "Swift") == true
  3. contains(languages, "Java") == false
  4. contains([29854296, 75], 42) == true

dropFirst(sequence): Returns a new sequence (such as an array) without the first element of the sequence. [objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. var oldLanguages = dropFirst(languages)  
  3. equal(oldLanguages, ["Objective-C"]) == true

dropLast(sequence): Returns a new sequence (such as an array) without the last element of the sequence passed as argument to the function. [objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. var newLanguages = dropLast(languages)  
  3. equal(newLanguages, ["Swift"]) == true

dump(object): Dumps the contents of an object to standard output.

[objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. dump(languages)  
  3. // Prints:
  4. // ▿ 2 elements
  5. //   - [0]: Swift
  6. //   - [1]: Objective-C

equal(sequence1, sequence2): Returns true if sequence1 and sequence2 contain the same elements.

[objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. equal(languages, ["Swift""Objective-C"]) == true
  3. var oldLanguages = dropFirst(languages)  
  4. equal(oldLanguages, ["Objective-C"]) == true
filter(sequence, includeElementClosure): Returns a the elements from sequence that evaluate to true by includeElementClosure. [objc] view plaincopyprint?
  1. for i in filter(1...100, { $0 % 10 == 0 }) {  
  2.     // 10, 20, 30, ...
  3.     println(i)  
  4.     assert(contains([102030405060708090, 100], i))  
  5. }  

find(sequence, element): Return the index of a specified element in the given sequence. Or nil if the element is not found in the sequence. [objc] view plaincopyprint?
  1. var languages = ["Swift""Objective-C"]  
  2. find(languages, "Objective-C") == 1
  3. find(languages, "Java") == nil
  4. find([29854296, 75], 42) == 2

indices(sequence): Returns the indices (zero indexed) of the elements in the given sequence. [objc] view plaincopy

相關推薦

Swift Standard Library

Swift has 74 built-in functions but only seven of them are documented in the Swift book (“The Swift Programming Language”). The rest r

Python Standard Library

pos 介紹 win .net let keyword xpath imp 9.4 出自:http://blog.csdn.net/qiqll/article/details/39339319 0.1. 關於本書 0.2. 代碼約定 0.3. 關於例

筆記-python-standard library-8.1 data types-datetime

none struct supported 方法 all min lag ons ros 筆記-python-standard library-8.1 data types-datetime 1. Datatimes 本章節內容為日期和時間處理類和方法。

筆記-python-standard library-9.6 random

conf next step += spi red exp option server 筆記-python-standard library-9.6 random 1. random source code:Lib/random.py 1.1. func

筆記-python-standard library-11.2 os.path

exist time split case linu basename 當前 輸出 字符轉換 筆記-python-standard library-11.2 os.path 1. os.path Source code: Lib/posixpath.py (f

筆記-python-standard library-17.7 queue

 筆記-python-standard library-17.7 queue   1.  queue source code:Lib/queue.py 該模組實現了多生產者,多消費者佇列。 此模組實現了所有的required locking semantics. 模組

筆記-python-standard library-16.3 time

輸出 bsp sleep 定義 最適 元組 enter local int 筆記-python-standard library-16.3 time 1. time 1.1. 開始 time模塊中時間表現的格式主要有三種: timestamp時間戳,

The Python 2 Standard Library by Example

Distributed with every copy of Python, the Standard Library contains hundreds of modules that provide tools for interacting with the operating system, i

Go: Testing Standard Library Changes

Go: Testing Standard Library ChangesI had a wonderful time at GopherCon 2017. One of the highlights was being a mentor at the Go Contributor Workshop. This

Python3.5.2 document學習系列之02、The Python Standard Library(python 標準庫)——介紹

1.介紹Python庫包含幾種不同種類的組成部分。它包含通常被認為是一種語言“核心”部分的資料型別,如數字和列表。對這些型別來說,Python語言核心定義了書寫上的格式和位置以及一些語義上的約束,但沒有完全定義語法(另一方面,語言核心定義了語法特性像拼寫和操作優先順序)這個庫

Python手冊(Standard Library)--datetime+time+calendar

datetime datetime模組定義了6個類 datetime.date 表示日期的類 datetime.datetime 表示日期時間的類 datetime.time 表示時間的類 datetime.timedelta

標準標簽庫JSTL(JSP Standard Tag Library)

when 標準 表達 XML 只有一個 默認 with 本地 輸出變量 1, 核心標簽(最常用, 最重要的) 表達式控制標簽 out 輸出常量 value---直接賦值 輸出變量 default---默認值 escapeXml--

STL(Standard Template Library)

float hms stl containe 排序 Go iterator 操作 eight 容器(Containers) list、deque、vector、map等 算法(Algorithms) 算法作用於容器,它們提供了執行各種操作的方式。包括了對容器的

<Standard Template Library>標準模板庫專項複習總結

看了看部落格園的申請時間也一年多了...想想自己一年多以來一直處於各種划水狀態,現在又要面臨ACM的衝擊... 還是要抓緊時間趕緊複習一下了- -畢竟校園新生賽還是有獎金的..   1.棧   先進後出(LIFO)表 標頭檔案:#include<stack> 變數

<Standard Template Library>標準模板庫專項複習總結(二)

4.佇列 先進先出(FIFO)表 標頭檔案:#include<queue> 變數的定義:queue<TYPE>queueName 成員函式: bool empty() 空佇列返回true,否則返回false void pop

Swift工程如何整合Static Library

我們的iOS專案中使用了Swift+Objective-C的混合開發,很長一段時間我們在Podfile中使用的是use_frameworks!,也就是把所有Pods做成Dynamic framework。 微信等第三方SDK以靜態庫.a的形式提供給開發者,導致我們沒有辦法直接依賴,會報類似xxx has tr

Cpp Chapter 16: The string Class and the Standard Template Library

(這已經是第二次部落格園吞我東西了,有點心態爆炸) 16.1 The string Class ) Constructing a string Here is a table which shows the seven form of constructors that the string class

JSTL(JavaServer Pages Standard Tag Library)

JSTL(JavaServer Pages Standard Tag Library,JSP標準標籤庫)是一個不斷完善的開放原始碼的JSP標籤庫。 之前有學過c++的STL,剛開始還以為JSTL是java的STL。。。 大致簡介: JSTL 1.0 釋出於 2002

The Standard Template Library

Containers: 容器是一大堆元素的集合,主要分為三類容器: (1)序列式容器(sequence containers) 序列式容器經常被實現為array或者linked list。這是一種有序(ordered)的集合,每個元素都有確鑿的位置,主要取決於插入時機和地點,與元素的數值無關。STL中包含的s

【JavaEE】JSTL(JSP Standard Tag Library,JSP標準標籤庫)

怎麼用? 1.引入jar包。jstl.jar和standard.jar 2.在頁面中引入,使用JSPtaglib指令。注意,如果想和EL表示式一起使用,需要加入高版本的JSTL 基本用法: 假定頁面引入是這樣的指令 <%@ taglib prefix="c" uri="