forked from apple/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSArray.swift
167 lines (144 loc) · 5.19 KB
/
NSArray.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_exported import Foundation // Clang module
//===----------------------------------------------------------------------===//
// Arrays
//===----------------------------------------------------------------------===//
extension NSArray : ExpressibleByArrayLiteral {
/// Create an instance initialized with `elements`.
public required convenience init(arrayLiteral elements: Any...) {
// Let bridging take care of it.
self.init(array: elements)
}
}
extension Array : _ObjectiveCBridgeable {
/// Private initializer used for bridging.
///
/// The provided `NSArray` will be copied to ensure that the copy can
/// not be mutated by other code.
internal init(_cocoaArray: NSArray) {
_sanityCheck(_isBridgedVerbatimToObjectiveC(Element.self),
"Array can be backed by NSArray only when the element type can be bridged verbatim to Objective-C")
// FIXME: We would like to call CFArrayCreateCopy() to avoid doing an
// objc_msgSend() for instances of CoreFoundation types. We can't do that
// today because CFArrayCreateCopy() copies array contents unconditionally,
// resulting in O(n) copies even for immutable arrays.
//
// <rdar://problem/19773555> CFArrayCreateCopy() is >10x slower than
// -[NSArray copyWithZone:]
//
// The bug is fixed in: OS X 10.11.0, iOS 9.0, all versions of tvOS
// and watchOS.
self = Array(
_immutableCocoaArray:
unsafeBitCast(_cocoaArray.copy() as AnyObject, to: _NSArrayCore.self))
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSArray {
return unsafeBitCast(self._bridgeToObjectiveCImpl(), to: NSArray.self)
}
public static func _forceBridgeFromObjectiveC(
_ source: NSArray,
result: inout Array?
) {
// If we have the appropriate native storage already, just adopt it.
if let native =
Array._bridgeFromObjectiveCAdoptingNativeStorageOf(source) {
result = native
return
}
if _fastPath(_isBridgedVerbatimToObjectiveC(Element.self)) {
// Forced down-cast (possible deferred type-checking)
result = Array(_cocoaArray: source)
return
}
result = _arrayForceCast([AnyObject](_cocoaArray: source))
}
public static func _conditionallyBridgeFromObjectiveC(
_ source: NSArray,
result: inout Array?
) -> Bool {
// Construct the result array by conditionally bridging each element.
let anyObjectArr = [AnyObject](_cocoaArray: source)
result = _arrayConditionalCast(anyObjectArr)
return result != nil
}
public static func _unconditionallyBridgeFromObjectiveC(
_ source: NSArray?
) -> Array {
// `nil` has historically been used as a stand-in for an empty
// array; map it to an empty array instead of failing.
if _slowPath(source == nil) { return Array() }
// If we have the appropriate native storage already, just adopt it.
if let native =
Array._bridgeFromObjectiveCAdoptingNativeStorageOf(source!) {
return native
}
if _fastPath(_isBridgedVerbatimToObjectiveC(Element.self)) {
// Forced down-cast (possible deferred type-checking)
return Array(_cocoaArray: source!)
}
return _arrayForceCast([AnyObject](_cocoaArray: source!))
}
}
extension NSArray : _HasCustomAnyHashableRepresentation {
// Must be @nonobjc to avoid infinite recursion during bridging
@nonobjc
public func _toCustomAnyHashable() -> AnyHashable? {
return AnyHashable(self as! Array<AnyHashable>)
}
}
extension NSArray : Sequence {
/// Return an *iterator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
final public func makeIterator() -> NSFastEnumerationIterator {
return NSFastEnumerationIterator(self)
}
}
/* TODO: API review
extension NSArray : Swift.Collection {
final public var startIndex: Int {
return 0
}
final public var endIndex: Int {
return count
}
}
*/
extension NSArray {
// Overlay: - (instancetype)initWithObjects:(id)firstObj, ...
public convenience init(objects elements: Any...) {
self.init(array: elements)
}
}
extension NSArray {
/// Initializes a newly allocated array by placing in it the objects
/// contained in a given array.
///
/// - Returns: An array initialized to contain the objects in
/// `anArray``. The returned object might be different than the
/// original receiver.
///
/// Discussion: After an immutable array has been initialized in
/// this way, it cannot be modified.
@nonobjc
public convenience init(array anArray: NSArray) {
self.init(array: anArray as Array)
}
}
extension NSArray : CustomReflectable {
public var customMirror: Mirror {
return Mirror(reflecting: self as [AnyObject])
}
}
extension Array: CVarArg {}