加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

74个Swift标准库函数

发布时间:2020-12-14 01:45:24 所属栏目:百科 来源:网络整理
导读:本文译自Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions。我不是原作者,我只是勤劳的翻译工:)文中作者没有提及他是如何发现这么多未在文档中体现的

本文译自Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions。我不是原作者,我只是勤劳的翻译工:)文中作者没有提及他是如何发现这么多未在文档中体现的内置函数的,估计是反编译的结果。我测试了好多个都能用,而且Xcode还会给出语法提示:)

Swift包含了74个内置函数,但在The Swift Programming Langage一书中只介绍了其中的7个,其它的都没有在文档中体现。

这篇文章列举出了所有的Swift库函数。文中所谓的库函数是指无需引入任何模块(比如说Fundation等)即可以直接使用的函数。

下面先来看看7个在文档中提到的库函数:

//断言,参数如果为`true`则继续,否则抛出异常 // assert mentioned on page 55 assert(true)//计算序列的元素个数// countElements mentioned on page 79 countElements("foo" == 3//返回一个新的序列,其中每个元素是一个元组,第一个值为原来元素所在的位置`index`,第二个为原来序列中的元素// enumerate mentioned on page 94for(i, jin enumerate(["A" "B"]){// "0:A","1:B" will be printed println"(i):(j)"}//返回所有参数中的最小值// min mentioned on page 246 min(8232//打印// print mentioned on page 85print"Hello "//打印(带换行)// println mentioned on page 4"World"//排序// sort mentioned on page 14 i  sort// "A","B" will be printed}

下面列出一些很实用,但未在文档中体现的库函数:

abs(signedNumber):返回数字的绝对值

abs(-11 abs424242

contains(sequence,element):如果某个序列sequence(比如说一个数组)包含指定的元素element,则返回true,否则返回false

var languages =["Swift""Objective-C"] containslanguagestrue"Java"false([29859675],123)">true

dropFirst(sequence):返回一个去掉了首个元素的、新的序列(比如一个新数组)。

 oldLanguages  dropFirst equaloldLanguagesdropLast(sequence):返回一个去掉了最后一个元素的、新的序列(比如一个新数组)。

 newLanguages  dropLastnewLanguagesdump(object):打印出某个对象object的所有信息

dump// Prints:// ? 2 elements// - [0]: Swift// - [1]: Objective-C

equal(sequence1,sequence2):判断两个序列是否相等

filter(sequence,includeElementClosure):对序列sequence中每个元素都执行includeElementClosure闭包,并将所有闭包结果为true的元素合成一个新序列sequence并返回。

 filter1...100 $0 %100})// 10,20,30,...contains102030405060708090 i))}

find(sequence,element):返回序列sequence中某元素element的位置index。如果序列中不存在此元素,则返回nil

 findnil2

indices(sequence):返回序列sequence中所有元素的位置(indices是index的复数)

equalindices]),161)">[0 indices// 0,1,2join(separator,sequence):将序列sequence通过分隔符separator连成一个字符串,并返回此字符串。

join":""C""A:B:C" join"/" languages"Swift/Objective-C"

map(sequence,transformClosure):对序列includeElementClosure闭包,并将所有闭包的结果合成一个新序列map*5}),145)">515 mapmax(comparable1,comparable2,etc.):返回参数中的最大值。

max max8

maxElement(sequence):返回序列sequence中的最大值。

maxElement maxElement"Swift"

minElements(sequence):返回序列sequence中的最小值。

minElement minElement"Objective-C"

reduce(sequence,initial,combineClosure):给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值。

 reduce""+ $1 "SwiftObjective-C"1000

reverse(sequence):返回逆序的序列sequence

reverse reverse// 3,2,1startsWith(sequence1,sequence2):如果序列sequence1中开头的元素跟序列sequence2中的所有元素都相等,则返回startsWith"foobar" startsWith10.. 上面提到的函数是我认为在Swift编程中会经常用到的函数。下面将列出完整的74个函数列表。

完整74个内置函数:

(...) advancealignof alignofValue bridgeFromObjectiveC bridgeFromObjectiveCUnconditional bridgeToObjectiveC bridgeToObjectiveCUnconditional c_malloc_size c_memcpy c_putchar count countLeadingZeros debugPrint debugPrintln distance encodeBitsAsWords getBridgedObjectiveCType getVaList insertionSort isBridgedToObjectiveC isBridgedVerbatimToObjectiveC isUniquelyReferenced lexicographicalCompare numericCast partition posix_read posix_write quickSort reflect reinterpretCast roundUpToAlignmentsizeof sizeofValue split strideof strideofValue swap swift_MagicMirrorData_summaryImpl swift_bufferAllocate swift_keepAlive toString transcode underestimateCount unsafeReflect withExtendedLifetime withObjectAtPlusZero withUnsafePointer withUnsafePointerToObject withUnsafePointers withVaList(...)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读