forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc_cast.swift
612 lines (522 loc) · 18.6 KB
/
objc_cast.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
@objc protocol SwiftObjCProto {}
class SwiftSuperPort : Port { }
class SwiftSubPort : SwiftSuperPort { }
class SwiftSuper { }
class SwiftSub : SwiftSuper { }
extension Port : SwiftObjCProto {}
var obj : AnyObject
func genericCast<T>(_ x: AnyObject, _: T.Type) -> T? {
return x as? T
}
func genericCastObjCBound<T: NSObject>(_ x: AnyObject, _: T.Type) -> T? {
return x as? T
}
// Test instance of Swift subclass of Objective-C class
obj = SwiftSubPort()
_ = obj as! SwiftSubPort
_ = obj as! SwiftSuperPort
_ = (obj as? Port)
_ = (obj as? NSObject)!
if (obj as? SwiftSubPort) == nil { abort() }
if (obj as? SwiftSuperPort) == nil { abort() }
if (obj as? Port) == nil { abort() }
if (obj as? NSObject) == nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) != nil { abort() }
obj = SwiftSuperPort()
_ = obj as! SwiftSuperPort
_ = obj as! Port
_ = obj as! NSObject
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) == nil { abort() }
if (obj as? Port) == nil { abort() }
if (obj as? NSObject) == nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) != nil { abort() }
// Test instance of Objective-C class that has Swift subclass
obj = Port()
_ = obj as! Port
_ = obj as! NSObject
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) != nil { abort() }
if (obj as? Port) == nil { abort() }
if (obj as? NSObject) == nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) != nil { abort() }
if (obj as? SwiftObjCProto) == nil { abort() }
obj = Port()
_ = genericCast(obj, Port.self)!
_ = genericCast(obj, NSObject.self)!
if genericCast(obj, SwiftSubPort.self) != nil { abort() }
if genericCast(obj, SwiftSuperPort.self) != nil { abort() }
if genericCast(obj, Port.self) == nil { abort() }
if genericCast(obj, NSObject.self) == nil { abort() }
if genericCast(obj, NSArray.self) != nil { abort() }
if genericCast(obj, SwiftSub.self) != nil { abort() }
if genericCast(obj, SwiftSuper.self) != nil { abort() }
_ = genericCastObjCBound(obj, Port.self)!
_ = genericCastObjCBound(obj, NSObject.self)!
if genericCastObjCBound(obj, SwiftSubPort.self) != nil { abort() }
if genericCastObjCBound(obj, SwiftSuperPort.self) != nil { abort() }
if genericCastObjCBound(obj, Port.self) == nil { abort() }
if genericCastObjCBound(obj, NSObject.self) == nil { abort() }
if genericCastObjCBound(obj, NSArray.self) != nil { abort() }
obj = NSObject()
_ = obj as! NSObject
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) != nil { abort() }
if (obj as? Port) != nil { abort() }
if (obj as? NSObject) == nil { abort() }
if (obj as? NSCopying) != nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) != nil { abort() }
if (obj as? SwiftObjCProto) != nil { abort() }
// Test instance of a tagged pointer type
obj = NSNumber(value: 1234567)
_ = obj as! NSNumber
_ = obj as! NSValue
_ = obj as! NSObject
_ = obj as! NSCopying
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) != nil { abort() }
if (obj as? NSNumber) == nil { abort() }
if (obj as? NSValue) == nil { abort() }
if (obj as? NSObject) == nil { abort() }
if (obj as? NSCopying) == nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) != nil { abort() }
// Test instance of a Swift class with no Objective-C inheritance
obj = SwiftSub()
_ = obj as! SwiftSub
_ = obj as! SwiftSuper
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) != nil { abort() }
if (obj as? NSObject) != nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) == nil { abort() }
if (obj as? SwiftSuper) == nil { abort() }
obj = SwiftSuper()
_ = obj as! SwiftSuper
if (obj as? SwiftSubPort) != nil { abort() }
if (obj as? SwiftSuperPort) != nil { abort() }
if (obj as? NSObject) != nil { abort() }
if (obj as? NSArray) != nil { abort() }
if (obj as? SwiftSub) != nil { abort() }
if (obj as? SwiftSuper) == nil { abort() }
// Test optional and non-optional bridged conversions
var ao: AnyObject = "s" as NSObject
ao as! String
ao is String
var auo: AnyObject! = "s" as NSObject
var s: String = auo as! String
var auoo: AnyObject? = "s" as NSObject
auoo! as? String
// Test bridged casts.
// CHECK: Downcast to hello
obj = NSString(string: "hello")
if let str = obj as? String {
print("Downcast to \(str)")
} else {
print("Not a string?")
}
// Forced cast using context
// CHECK-NEXT: Forced to string hello
let forcedStr: String = obj as! String
print("Forced to string \(forcedStr)")
// CHECK-NEXT: Downcast to Swift
var objOpt: AnyObject? = NSString(string: "Swift")
if let str = objOpt as? String {
print("Downcast to \(str)")
} else {
print("Not a string?")
}
// Forced cast using context
// CHECK-NEXT: Forced to string Swift
let forcedStrOpt: String = objOpt as! String
print("Forced to string \(forcedStrOpt)")
// CHECK-NEXT: Downcast to world
var objImplicitOpt: AnyObject! = NSString(string: "world")
if let str = objImplicitOpt as? String {
print("Downcast to \(str)")
} else {
print("Not a string?")
}
// Forced cast using context
// CHECK-NEXT: Forced to string world
let forcedStrImplicitOpt: String = objImplicitOpt as! String
print("Forced to string \(forcedStrImplicitOpt)")
// CHECK-NEXT: Downcast correctly failed due to nil
objOpt = nil
if let str = objOpt as? String {
print("Downcast should not succeed for nil")
} else {
print("Downcast correctly failed due to nil")
}
// CHECK-NEXT: Downcast correctly failed due to nil
objImplicitOpt = nil
if let str = objImplicitOpt as? String {
print("Downcast should not succeed for nil")
} else {
print("Downcast correctly failed due to nil")
}
// Test bridged "isa" checks.
// CHECK: It's a string!
obj = NSString(string: "hello")
if obj is String {
print("It's a string!")
} else {
print("Not a string?")
}
// CHECK-NEXT: It's a string!
objOpt = NSString(string: "Swift")
if objOpt is String {
print("It's a string!")
} else {
print("Not a string?")
}
// CHECK-NEXT: It's a string!
objImplicitOpt = NSString(string: "world")
if objImplicitOpt is String {
print("It's a string!")
} else {
print("Not a string?")
}
// CHECK-NEXT: Isa correctly failed due to nil
objOpt = nil
if objOpt is String {
print("Isa should not succeed for nil")
} else {
print("Isa correctly failed due to nil")
}
// CHECK-NEXT: Isa correctly failed due to nil
objImplicitOpt = nil
if objImplicitOpt is String {
print("Isa should not succeed for nil")
} else {
print("Isa correctly failed due to nil")
}
let words = ["Hello", "Swift", "World"]
// CHECK-NEXT: Object-to-bridged-array cast produced ["Hello", "Swift", "World"]
obj = words as AnyObject
if let strArr = obj as? [String] {
print("Object-to-bridged-array cast produced \(strArr)")
} else {
print("Object-to-bridged-array cast failed")
}
// Check downcast from the bridged type itself.
// CHECK-NEXT: NSArray-to-bridged-array cast produced ["Hello", "Swift", "World"]
var nsarr = words as NSArray
if let strArr = nsarr as? [String] {
print("NSArray-to-bridged-array cast produced \(strArr)")
} else {
print("NSArray-to-bridged-array cast failed")
}
// CHECK-NEXT: NSArray?-to-bridged-array cast produced ["Hello", "Swift", "World"]
var nsarrOpt = words as NSArray?
if let strArr = nsarrOpt as? [String] {
print("NSArray?-to-bridged-array cast produced \(strArr)")
} else {
print("NSArray?-to-bridged-array cast failed")
}
// CHECK-NEXT: NSArray!-to-bridged-array cast produced ["Hello", "Swift", "World"]
var nsarrImplicitOpt = words as NSArray!
if let strArr = nsarrImplicitOpt as? [String] {
print("NSArray!-to-bridged-array cast produced \(strArr)")
} else {
print("NSArray!-to-bridged-array cast failed")
}
// Check downcast from a superclass of the bridged type.
// CHECK-NEXT: NSObject-to-bridged-array cast produced ["Hello", "Swift", "World"]
var nsobj: NSObject = nsarr
if let strArr = nsobj as? [String] {
print("NSObject-to-bridged-array cast produced \(strArr)")
} else {
print("NSObject-to-bridged-array cast failed")
}
// CHECK-NEXT: NSArray is [String]
if nsarr is [String] {
print("NSArray is [String]")
} else {
print("NSArray is not a [String]")
}
// CHECK-NEXT: NSArray? is [String]
if nsarrOpt is [String] {
print("NSArray? is [String]")
} else {
print("NSArray? is not a [String]")
}
// CHECK-NEXT: NSArray! is [String]
if nsarrImplicitOpt is [String] {
print("NSArray! is [String]")
} else {
print("NSArray! is not a [String]")
}
// CHECK-NEXT: NSObject is [String]
if nsobj is [String] {
print("NSObject is [String]")
} else {
print("NSObject is not a [String]")
}
// Forced downcast based on context.
// CHECK-NEXT: Forced to string array ["Hello", "Swift", "World"]
var forcedStrArray: [String] = obj as! [String]
print("Forced to string array \(forcedStrArray)")
// CHECK-NEXT: Forced NSArray to string array ["Hello", "Swift", "World"]
forcedStrArray = nsarr as! [String]
print("Forced NSArray to string array \(forcedStrArray)")
// CHECK-NEXT: Forced NSArray? to string array ["Hello", "Swift", "World"]
forcedStrArray = nsarrOpt as! [String]
print("Forced NSArray? to string array \(forcedStrArray)")
// CHECK-NEXT: Forced NSArray! to string array ["Hello", "Swift", "World"]
forcedStrArray = nsarrImplicitOpt as! [String]
print("Forced NSArray! to string array \(forcedStrArray)")
// CHECK-NEXT: Object-to-array cast produced [Hello, Swift, World]
if let strArr = obj as? [NSString] {
print("Object-to-array cast produced \(strArr)")
} else {
print("Object-to-array cast failed")
}
// CHECK-NEXT: Object-to-bridged-array cast failed due to bridge mismatch
if let strArr = obj as? [Int] {
print("Object-to-bridged-array cast should not have succeeded")
} else {
print("Object-to-bridged-array cast failed due to bridge mismatch")
}
// CHECK-NEXT: Array of strings is not an array of ints
if obj is [Int] {
print("Array of strings should not be an array of ints!")
} else {
print("Array of strings is not an array of ints")
}
// Implicitly unwrapped optional of object to array casts.
// CHECK-NEXT: Object-to-bridged-array cast produced ["Hello", "Swift", "World"]
objOpt = words as AnyObject?
if let strArr = objOpt as? [String] {
print("Object-to-bridged-array cast produced \(strArr)")
} else {
print("Object-to-bridged-array cast failed")
}
// Forced downcast based on context.
// CHECK-NEXT: Forced to string array ["Hello", "Swift", "World"]
let forcedStrArrayOpt: [String] = objOpt as! [String]
print("Forced to string array \(forcedStrArrayOpt)")
// CHECK-NEXT: Object-to-array cast produced [Hello, Swift, World]
if let strArr = objOpt as? [NSString] {
print("Object-to-array cast produced \(strArr)")
} else {
print("Object-to-array cast failed")
}
// CHECK: Object-to-bridged-array cast failed due to bridge mismatch
if let intArr = objOpt as? [Int] {
print("Object-to-bridged-array cast should not have succeeded")
} else {
print("Object-to-bridged-array cast failed due to bridge mismatch")
}
// CHECK: Object-to-bridged-array cast failed due to nil
objOpt = nil
if let strArr = objOpt as? [String] {
print("Cast from nil succeeded?")
} else {
print("Object-to-bridged-array cast failed due to nil")
}
// Optional of object to array casts.
// CHECK-NEXT: Object-to-bridged-array cast produced ["Hello", "Swift", "World"]
objImplicitOpt = words as AnyObject!
if let strArr = objImplicitOpt as? [String] {
print("Object-to-bridged-array cast produced \(strArr)")
} else {
print("Object-to-bridged-array cast failed")
}
// Forced downcast based on context.
// CHECK-NEXT: Forced to string array ["Hello", "Swift", "World"]
let forcedStrArrayImplicitOpt: [String] = objImplicitOpt as! [String]
print("Forced to string array \(forcedStrArrayImplicitOpt)")
// CHECK-NEXT: Object-to-array cast produced [Hello, Swift, World]
if let strArr = objImplicitOpt as? [NSString] {
print("Object-to-array cast produced \(strArr)")
} else {
print("Object-to-array cast failed")
}
// CHECK: Object-to-bridged-array cast failed due to bridge mismatch
if let intArr = objImplicitOpt as? [Int] {
print("Object-to-bridged-array cast should not have succeeded")
} else {
print("Object-to-bridged-array cast failed due to bridge mismatch")
}
// CHECK: Object-to-bridged-array cast failed due to nil
objImplicitOpt = nil
if let strArr = objImplicitOpt as? [String] {
print("Cast from nil succeeded?")
} else {
print("Object-to-bridged-array cast failed due to nil")
}
// Casting an array of numbers to different numbers.
// CHECK: Numbers-as-doubles cast produces [3.9375, 2.71828, 0.0]
obj = ([3.9375, 2.71828, 0] as [Double]) as AnyObject
if let doubleArr = obj as? [Double] {
print(MemoryLayout<Double>.size)
print("Numbers-as-doubles cast produces \(doubleArr)")
} else {
print("Numbers-as-doubles failed")
}
// CHECK-FAIL: Numbers-as-floats cast produces [3.9375, 2.71828{{.*}}, 0.0]
// TODO: check if this is intention: rdar://33021520
if let floatArr = obj as? [Float] {
print(MemoryLayout<Float>.size)
print("Numbers-as-floats cast produces \(floatArr)")
} else {
print("Numbers-as-floats failed")
}
// CHECK-FAIL: Numbers-as-ints cast produces [3, 2, 0]
// TODO: check if this is intention: rdar://33021520
if let intArr = obj as? [Int] {
print("Numbers-as-ints cast produces \(intArr)")
} else {
print("Numbers-as-ints failed")
}
// CHECK-FAIL: Numbers-as-bools cast produces [true, true, false]
// TODO: check if this is intention: rdar://33021520
if let boolArr = obj as? [Bool] {
print("Numbers-as-bools cast produces \(boolArr)")
} else {
print("Numbers-as-bools failed")
}
class Base : NSObject {
override var description: String {
return "Base"
}
}
class Derived : Base {
override var description: String {
return "Derived"
}
}
// CHECK: Array-of-base cast produces [Derived, Derived, Base]
obj = [Derived(), Derived(), Base()] as NSObject
if let baseArr = obj as? [Base] {
print("Array-of-base cast produces \(baseArr)")
} else {
print("Not an array of base")
}
// CHECK: Not an array of derived
if let derivedArr = obj as? [Derived] {
print("Array-of-derived cast produces \(derivedArr)")
} else {
print("Not an array of derived")
}
// CHECK: Dictionary-of-base-base cast produces
obj = [Derived() : Derived(), Derived() : Base(), Derived() : Derived() ] as AnyObject
if let baseDict = obj as? Dictionary<Base, Base> {
print("Dictionary-of-base-base cast produces \(baseDict)")
} else {
print("Not a dictionary of base/base")
}
// CHECK: Dictionary-of-derived-base cast produces
if let baseDict = obj as? Dictionary<Derived, Base> {
print("Dictionary-of-derived-base cast produces \(baseDict)")
} else {
print("Not a dictionary of derived/base")
}
// CHECK: Not a dictionary of derived/derived
if let dict = obj as? Dictionary<Derived, Derived> {
print("Dictionary-of-derived-derived cast produces \(dict)")
} else {
print("Not a dictionary of derived/derived")
}
let strArray: AnyObject = ["hello", "world"] as NSObject
let intArray: AnyObject = [1, 2, 3] as NSObject
let dictArray: AnyObject = [["hello" : 1, "world" : 2],
["swift" : 1, "speedy" : 2]] as NSObject
// CHECK: Dictionary<String, AnyObject> is
obj = ["a" : strArray, "b" : intArray, "c": dictArray] as NSObject
if let dict = obj as? Dictionary<String, [AnyObject]> {
print("Dictionary<String, AnyObject> is \(dict)")
} else {
print("Not a Dictionary<String, AnyObject>")
}
// CHECK: Not a Dictionary<String, String>
if let dict = obj as? Dictionary<String, [String]> {
print("Dictionary<String, String> is \(dict)")
} else {
print("Not a Dictionary<String, String>")
}
// CHECK: Not a Dictionary<String, Int>
if let dict = obj as? Dictionary<String, [Int]> {
print("Dictionary<String, Int> is \(dict)")
} else {
print("Not a Dictionary<String, Int>")
}
// CHECK: [Dictionary<String, Int>] is
obj = dictArray
if let array = obj as? [Dictionary<String, Int>] {
print("[Dictionary<String, Int>] is \(array)")
} else {
print("Not a [Dictionary<String, Int>]")
}
// CHECK: Not a [Dictionary<String, String>]
if let array = obj as? [Dictionary<String, String>] {
print("[Dictionary<String, String>] is \(array)")
} else {
print("Not a [Dictionary<String, String>]")
}
// CHECK: Dictionary<String, [Dictionary<String, Int>]> is ["a": [
obj = ["a" : dictArray] as NSObject
if let dict = obj as? Dictionary<String, [Dictionary<String, Int>]> {
print("Dictionary<String, [Dictionary<String, Int>]> is \(dict)")
} else {
print("Not a Dictionary<String, [Dictionary<String, Int>]>")
}
// CHECK: Not a Dictionary<String, [Dictionary<String, String>]>
if let dict = obj as? Dictionary<String, [Dictionary<String, String>]> {
print("Dictionary<String, [Dictionary<String, String>]> is \(dict)")
} else {
print("Not a Dictionary<String, [Dictionary<String, String>]>")
}
// CHECK: [Dictionary<String, [Dictionary<String, Int>]>] is
obj = [obj, obj, obj] as NSObject
if let array = obj as? [Dictionary<String, [Dictionary<String, Int>]>] {
print("[Dictionary<String, [Dictionary<String, Int>]>] is \(array)")
} else {
print("Not a [Dictionary<String, [Dictionary<String, Int>]>]")
}
// CHECK: Not a Dictionary<String, [Dictionary<String, String>]>[]
if let array = obj as? Dictionary<String, [Dictionary<String, String>]> {
print("Dictionary<String, [Dictionary<String, String>]>[] is \(array)")
} else {
print("Not a Dictionary<String, [Dictionary<String, String>]>[]")
}
// Helper function that downcasts
func downcastToStringArrayOptOpt(_ obj: AnyObject???!) {
if let strArrOptOpt = obj as? [String]?? {
if let strArrOpt = strArrOptOpt {
if let strArr = strArrOpt {
print("some(some(some(\(strArr))))")
} else {
print("some(some(none))")
}
} else {
print("some(none)")
}
} else {
print("none")
}
}
// CHECK: {{^}}some(some(some(["a", "b", "c"]))){{$}}
var objOptOpt: AnyObject?? = .some(.some(["a", "b", "c"] as NSObject))
downcastToStringArrayOptOpt(objOptOpt)
// CHECK: {{^}}none{{$}}
objOptOpt = .some(.some([1 : "hello", 2 : "swift", 3 : "world"] as NSObject))
downcastToStringArrayOptOpt(objOptOpt)
// CHECK: {{^}}none{{$}}
objOptOpt = .some(.some([1, 2, 3] as NSObject))
downcastToStringArrayOptOpt(objOptOpt)
print("ok") // CHECK: ok