@@ -5,36 +5,79 @@ $(SPEC_S Interfacing to Objective-C,
5
5
$(HEADERNAV_TOC)
6
6
7
7
$(P
8
- D has limited support for interfacing with Objective-C. It supports
9
- external classes and calling instance and class methods. It is only
10
- available on macOS, compiling for 64bit.
8
+ D has some support for interfacing with Objective-C. It supports
9
+ classes, instance and class methods. It is only available on macOS,
10
+ compiling for 64bit.
11
11
)
12
12
13
13
$(P
14
14
Fully working example is available at
15
15
$(LINK2 #usage-example, the bottom).
16
16
)
17
17
18
- $(SECTION2 $(LNAME2 external-class, Declaring an External Class))
18
+ $(SECTION2 $(LNAME2 classes, Classes))
19
+
20
+ $(SECTION3 $(LNAME2 external-class, Declaring an External Class))
19
21
20
22
---
21
23
extern (Objective-C)
22
- interface NSString
24
+ class NSString
23
25
{
24
26
const(char)* UTF8String() @selector("UTF8String");
25
27
}
26
28
---
27
29
28
30
$(P
29
- Currently all Objective-C classes need to be declared as interfaces in
30
- D. All Objective-C classes that should be accessible from within D
31
- need to be declared with the $(LINK2 #objc-linkage, Objective-C linkage).
31
+ All Objective-C classes that should be accessible from within D need to
32
+ be declared with the $(LINK2 #objc-linkage, Objective-C linkage). If all
33
+ methods inside an Objective-C class are without a body the class is
34
+ expected to be defined externally.
32
35
)
33
36
34
37
$(P
35
38
The $(LINK2 #selector-attribute, `@selector`) attribute indicates which
36
- Objective-C selector should be used when calling this method from D.
37
- This attribute needs to be attached to all methods.
39
+ Objective-C selector should be used when calling this method.
40
+ This attribute needs to be attached to all methods with the
41
+ `Objective-C` linkage.
42
+ )
43
+
44
+ $(SECTION3 $(LNAME2 defining-class, Defining a Class))
45
+
46
+ ---
47
+ // externally defined
48
+ extern (Objective-C)
49
+ class NSObject
50
+ {
51
+ static NSObject alloc() @selector("alloc");
52
+ NSObject init() @selector("init");
53
+ }
54
+
55
+ extern (Objective-C)
56
+ class Foo : NSObject
57
+ {
58
+ override static Foo alloc() @selector("alloc");
59
+ override Foo init() @selector("init");
60
+
61
+ final int bar(int a) @selector("bar:")
62
+ {
63
+ return a;
64
+ }
65
+ }
66
+
67
+ void main()
68
+ {
69
+ assert(Foo.alloc.init.bar(3) == 3);
70
+ }
71
+ ---
72
+
73
+ $(P
74
+ Defining an Objective-C class is exactly the same as declaring an
75
+ external class but it needs to have at least one method with a body.
76
+ )
77
+
78
+ $(P
79
+ To match the Objective-C semantics, `static` and `final` methods are
80
+ virtual. `static` methods are overridable as well.
38
81
)
39
82
40
83
$(SECTION2 $(LNAME2 instance-method, Calling an Instance Method))
@@ -73,7 +116,7 @@ $(HEADERNAV_TOC)
73
116
74
117
---
75
118
extern (Objective-C)
76
- interface NSString
119
+ class NSString
77
120
{
78
121
NSString initWith(in char*) @selector("initWithUTF8String:");
79
122
NSString initWith(NSString) @selector("initWithString:");
@@ -133,19 +176,19 @@ $(HEADERNAV_TOC)
133
176
134
177
$(P
135
178
Objective-C linkage is achieved by attaching the `extern (Objective-C)`
136
- attribute to an interface . Example:
179
+ attribute to a class . Example:
137
180
)
138
181
139
182
---
140
183
extern (Objective-C)
141
- interface NSObject
184
+ class NSObject
142
185
{
143
186
NSObject init() @selector("init");
144
187
}
145
188
---
146
189
147
190
$(P
148
- All methods inside an interface declared as `extern (Objective-C)` will
191
+ All methods inside a class declared as `extern (Objective-C)` will
149
192
get implicit Objective-C linkage.
150
193
)
151
194
@@ -229,7 +272,7 @@ $(HEADERNAV_TOC)
229
272
230
273
---
231
274
extern (Objective-C)
232
- interface NSString
275
+ class NSString
233
276
{
234
277
static NSString alloc() @selector("alloc");
235
278
NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
@@ -295,7 +338,7 @@ $(HEADERNAV_TOC)
295
338
module main;
296
339
297
340
extern (Objective-C)
298
- interface NSString
341
+ class NSString
299
342
{
300
343
static NSString alloc() @selector("alloc");
301
344
NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
0 commit comments