forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_lookup.swift
90 lines (70 loc) · 1.73 KB
/
dynamic_lookup.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
// RUN: %target-run-simple-swift | FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
class X {
init() {}
@objc func f() { print("X.f()") }
@objc var myValue : Int {
print("X.myValue getter\n")
return 17
}
}
class Y {
init() {}
@objc class func g() { print("Y.g()") }
}
class Z {
init() {}
}
extension Z {
@objc func f() { print("Z.f()") }
}
func test_dynamic_lookup_f(obj: AnyObject) {
var of = obj.f
if of != nil {
of!()
} else {
print("Object does not respond to the selector \"f\".\n", terminator: "")
}
}
func test_dynamic_lookup_g(obj: AnyObject) {
var og = obj.dynamicType.g
if og != nil {
og!()
} else {
print("Class does not respond to the selector \"g\".\n", terminator: "")
}
}
func test_dynamic_lookup_myValue(obj: AnyObject) {
var ov = obj.myValue
if ov != nil {
print("myValue = \(ov!)")
} else {
print("Object does not respond to the selector \"myValue\".")
}
}
// CHECK: X.f()
test_dynamic_lookup_f(X())
// CHECK: Object does not respond to the selector "f"
test_dynamic_lookup_f(Y())
// CHECK: Z.f()
test_dynamic_lookup_f(Z())
// CHECK: Class does not respond to the selector "g"
test_dynamic_lookup_g(X())
// CHECK: Y.g()
test_dynamic_lookup_g(Y())
// CHECK: X.myValue getter
// CHECK: myValue = 17
test_dynamic_lookup_myValue(X())
// CHECK: Object does not respond to the selector "myValue"
test_dynamic_lookup_myValue(Y())
// <rdar://problem/16554056> __FUNCTION__ in deinit for NSObject subclasses crashes the compiler
// Test __FUNCTION__
class FUNCTION_NAME_TEST : NSObject {
override init() { super.init() ; print(__FUNCTION__) }
deinit { print(__FUNCTION__) }
}
FUNCTION_NAME_TEST()
// CHECK: init()
// CHECK: deinit