File: objc_subclass.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (145 lines) | stat: -rw-r--r-- 3,008 bytes parent folder | download
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
// RUN: %target-run-simple-swift foo | %FileCheck %s
// REQUIRES: executable_test

// REQUIRES: objc_interop

import Foundation

class SuperString : NSString {
  var len = Int()

  override init() { super.init() }

  init(_ len:Int) {
    super.init()
    self.len = len
  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }
  required init(itemProviderData data: Data, typeIdentifier: String) throws {
    fatalError("don't call this initializer")
  }

  override var length: Int {
    return len
  }

  override func character(at n: Int) -> unichar {
    return unichar(0x30 + n)
  }

  override func substring(with r: NSRange) -> String {
    if (r.location == 0) {
      return SuperString(r.length) as String
    }
    return super.substring(with: r)
  }
}

// CHECK: 0123456789
print(SuperString(10))
// CHECK: 0123456789
print(NSString(string: SuperString(10) as String))
// CHECK: 012
print(SuperString(10).substring(with: NSRange(location: 0, length: 3)))
// CHECK: 345
print(SuperString(10).substring(with: NSRange(location: 3, length: 3)))

class X {
  var label: String

  init(_ label: String) {
    self.label = label
    print("Initializing \(label)");
  }

  deinit {
    print("Destroying \(label)");
  }
}

@requires_stored_property_inits
class A : NSObject {
  var x1 = X("A.x1")
  var x2 = X("A.x2")

  override init() {
    print("Initializing A instance");
  }

  deinit {
    print("Destroying A instance");
  }
}

class B : A {
  var y1 = X("B.y1")
  var y2 = X("B.y2")

  override init() {
    super.init()
    print("Initializing B instance");
  }

  deinit {
    print("Destroying B instance");
  }
}

func testB() -> B {
  return B()
}

// CHECK: Initializing A.x1
// CHECK: Initializing A.x2
// CHECK: Initializing B.y1
// CHECK: Initializing B.y2
// CHECK: Initializing A instance
// CHECK: Initializing B instance
// CHECK: Destroying B instance
// CHECK: Destroying A instance
// CHECK: Destroying B.y1
// CHECK: Destroying B.y2
// CHECK: Destroying A.x1
// CHECK: Destroying A.x2
testB()

// Propagating nil init out of a superclass initialization.
class MyNSData : NSData {
  init?(base64EncodedString str: String) {
    super.init(base64Encoded:str, 
               options:[])
    print("MyNSData code should not be executed")
  }

  required init?(coder: NSCoder) {
    super.init(coder: coder)
  }
}

// CHECK-NOT: should not be executed
if let myNSData = MyNSData(base64EncodedString:"\n\n\n") {
  print("NSData came back non-nil?")
} else {
  // CHECK: nil MyNSData as expected
  print("nil MyNSData as expected")
}

// Propagating nil out of delegating initialization.
extension NSData {
  convenience init?(myString str: String) {
    self.init(base64Encoded:str, 
              options:[])
    print("NSData code should not be executed")
  }
}

// CHECK-NOT: NSData code should not be executed
var nsData : NSData! = NSData(myString:"\n\n\n")
if nsData == nil {
  // CHECK: nil NSData as expected
  print("nil NSData as expected")
}