Make _convertNSArrayToArray work for non-verbatim bridged types.

The runtime diagnostics here are awful, but the monkey dances
<rdar://problem/16899681>.



Swift SVN r18121
This commit is contained in:
Doug Gregor
2014-05-15 19:25:58 +00:00
parent 87904b8228
commit 01f3f7777e
2 changed files with 13 additions and 11 deletions

View File

@@ -557,7 +557,10 @@ func asNSArray<T>(array: T[]) -> NSArray {
/// The entry point for bridging `NSArray` to `Array`. /// The entry point for bridging `NSArray` to `Array`.
func _convertNSArrayToArray<T>(nsarr: NSArray) -> T[] { func _convertNSArrayToArray<T>(nsarr: NSArray) -> T[] {
return T[](ArrayBuffer(reinterpretCast(nsarr) as CocoaArray)) if let arr = T[].bridgeFromObjectiveC(nsarr) {
return arr
}
fatal("NSArray does not bridge to array")
} }
/// The entry point for bridging 'Array' to 'NSArray'. /// The entry point for bridging 'Array' to 'NSArray'.
@@ -580,7 +583,8 @@ extension Array : _ConditionallyBridgedToObjectiveC {
} }
static func bridgeFromObjectiveC(x: NSArray) -> Array<T>? { static func bridgeFromObjectiveC(x: NSArray) -> Array<T>? {
fatal("implement") let anyArr = AnyObject[](ArrayBuffer(reinterpretCast(x) as CocoaArray))
return _arrayBridgedDownCast(anyArr)
} }
@conversion func __conversion() -> NSArray { @conversion func __conversion() -> NSArray {

View File

@@ -114,7 +114,6 @@ func testConvertArrayOfImplicitUnwrappedArray() {
testConvertArrayOfImplicitUnwrappedArray() testConvertArrayOfImplicitUnwrappedArray()
#if false
// Bridge an NSArray to an array of implicitly unwrapped class type. // Bridge an NSArray to an array of implicitly unwrapped class type.
func testConvertToArrayOfImplicitUnwrappedClass() { func testConvertToArrayOfImplicitUnwrappedClass() {
println("Converting an NSArray to an array of X!") println("Converting an NSArray to an array of X!")
@@ -124,9 +123,9 @@ func testConvertToArrayOfImplicitUnwrappedClass() {
var arr: (X!)[] = _convertNSArrayToArray(nsarr) var arr: (X!)[] = _convertNSArrayToArray(nsarr)
// CHECK-DISABLED: Class array count = 2 // CHECK: Class array count = 2
// CHECK-DISABLED: Element 0 has value X(1) // CHECK: Element 0 has value X(1)
// CHECK-DISABLED: Element 1 has value X(2) // CHECK: Element 1 has value X(2)
println("Class array count = \(arr.count)") println("Class array count = \(arr.count)")
for (index, opt) in enumerate(arr) { for (index, opt) in enumerate(arr) {
if let x = opt { if let x = opt {
@@ -148,13 +147,13 @@ func testConvertToArrayOfImplicitUnwrappedString() {
var arr: (String!)[] = _convertNSArrayToArray(nsarr) var arr: (String!)[] = _convertNSArrayToArray(nsarr)
// CHECK-DISABLED: String array count = 2 // CHECK: String array count = 2
// CHECK-DISABLED: Element 0 has value Hello // CHECK: Element 0 has value Hello
// CHECK-DISABLED: Element 1 has value World // CHECK: Element 1 has value World
println("String array count = \(arr.count)") println("String array count = \(arr.count)")
for (index, opt) in enumerate(arr) { for (index, opt) in enumerate(arr) {
if let str = opt { if let str = opt {
println("Element \(index) has value X(str)") println("Element \(index) has value \(str)")
} else { } else {
println("Element \(index) is empty") println("Element \(index) is empty")
} }
@@ -162,7 +161,6 @@ func testConvertToArrayOfImplicitUnwrappedString() {
} }
testConvertToArrayOfImplicitUnwrappedString() testConvertToArrayOfImplicitUnwrappedString()
#endif
// FIXME: Negative tests will need their own path. // FIXME: Negative tests will need their own path.