Extensions
The following extensions are available globally.
-
Conceptually_,
Array
is an efficient, tail-growable random-access collection of arbitrary elements.Common Properties of Array Types ================================The information in this section applies to all three of Swift’s array types,Array<T>
,ContiguousArray<T>
, andSlice<T>
. When you read the wordarray
here in a normal typeface, it applies to all three of them.Value Semantics —————Each array variable,let
binding, or stored property has an independent value that includes the values of all of its elements. Therefore, mutations to the array are not observable through its copies::var a = [1, 2, 3] var b = a b[0] = 4 println(
(Of course, if the array storesa=\(a), b=\(b)
) // a=[1, 2, 3], b=[4, 2, 3]class
references, the objects are shared; only the values of the references are independent)Arrays use Copy-on-Write so that their storage and elements are only copied lazily, upon mutation, when more than one array instance is using the same buffer. Therefore, the first in any sequence of mutating operations may costO(N)
time and space, whereN
is the length of the array.Growth and Capacity ——————-When an array’s contiguous storage fills up, new storage must be allocated and elements must be moved to the new storage.Array
,ContiguousArray
, andSlice
share an exponential growth strategy that makesappend
a constant time operation when amortized over many invocations. In addition to acount
property, these array types have acapacity
that reflects their potential to store elements without reallocation, and when you know how many elements you’ll store, you can callreserveCapacity
to pre-emptively reallocate and prevent intermediate reallocations… _Conceptually:Objective-C Bridge ==================The main distinction betweenArray
and the other array types is that it interoperates seamlessly and efficiently with Objective-C.Array<T>
is considered bridged to Objective-C iffT
is bridged to Objective-C.WhenT
is aclass
or@objc
protocol type,Array
may store its elements in anNSArray
. Since any arbitrary subclass ofNSArray
can become anArray
, there are no guarantees about representation or efficiency in this case (see alsoContiguousArray
). SinceNSArray
is immutable, it is just as though the storage was shared by some copy: the first in any sequence of mutating operations causes elements to be copied into unique, contiguous storage which may costO(N)
time and space, whereN
is the length of the array (or more, if the underlyingNSArray
is has unusual performance characteristics).Bridging to Objective-C ———————–Any bridgedArray
can be implicitly converted to anNSArray
. WhenT
is aclass
or@objc
protocol, bridging takes O(1) time and O(1) space. OtherArray
\ s must be bridged element-by-element, allocating a new object for each element, at a cost of at least O(count
) time and space.Bridging from Objective-C ————————-AnNSArray
can be implicitly or explicitly converted to any bridgedArray<T>
. This conversion callscopyWithZone
on theNSArray
, to ensure it won’t be modified, and stores the result in theArray
. Type-checking, to ensure theNSArray
\ ’s elements match or can be bridged toT
, is deferred until the first element access.Declaration
Swift
struct Array<T> : MutableCollectionType, Sliceable
-
A hash-based mapping from
Key
toValue
instances. Also a collection of key-value pairs with no defined ordering.Declaration
Swift
struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConvertible
-
An arbitrary Unicode string value.Unicode-Correct ===============Swift strings are designed to be Unicode-correct. In particular, the APIs make it easy to write code that works correctly, and does not surprise end-users, regardless of where you venture in the Unicode character space. For example,
- The
==
operator checks for Unicode canonical equivalence, so two different representations of the same string will always compare equal. - String elements are
Characters
(Unicode extended grapheme clusters), a unit of text that is meaningful to most humans.
Dictionary<String, T>
in a running program depends on a given string comparison having a single, stable result. Therefore, Swift always uses the default, un-tailored Unicode algorithms for basic string operations.ImportingFoundation
endows swift strings with the full power of theNSString
API, which allows you to choose more complex locale-sensitive operations explicitly.Value Semantics ===============Each string variable,let
binding, or stored property has an independent value, so mutations to the string are not observable through its copies::var a =
Strings use Copy-on-Write so that their data is only copied lazily, upon mutation, when more than one string instance is using the same buffer. Therefore, the first in any sequence of mutating operations may costfoo
var b = a b[b.endIndex.predecessor()] =x
println(a=\(a), b=\(b)
) // a=foo, b=foxO(N)
time and space, whereN
is the length of the string’s (unspecified) underlying representation,.Growth and Capacity ===================When a string’s contiguous storage fills up, new storage must be allocated and characters must be moved to the new storage.String
uses an exponential growth strategy that makesappend
a constant time operation when amortized over many invocations.Objective-C Bridge ==================String
is bridged to Objective-C asNSString
, and aString
that originated in Objective-C may store its characters in anNSString
. Since any arbitrary subclass ofNSSString
can become aString
, there are no guarantees about representation or efficiency in this case. SinceNSString
is immutable, it is just as though the storage was shared by some copy: the first in any sequence of mutating operations causes elements to be copied into unique, contiguous storage which may costO(N)
time and space, whereN
is the length of the string representation (or more, if the underlyingNSString
is has unusual performance characteristics).Declaration
Swift
struct String
- The
-
************** Immutable Data ***************
Declaration
Swift
@interface NSData : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @end
-
A 64-bit unsigned integer value type.
Declaration
Swift
struct UInt64 : UnsignedIntegerType
-
A 64-bit signed integer value type.
Declaration
Swift
struct Int64 : SignedIntegerType
-
A value type whose instances are either
true
orfalse
.Declaration
Swift
struct Bool