@@ -5,36 +5,79 @@ $(SPEC_S Interfacing to Objective-C,
55$(HEADERNAV_TOC)
66
77 $(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.
1111 )
1212
1313 $(P
1414 Fully working example is available at
1515 $(LINK2 #usage-example, the bottom).
1616 )
1717
18- $(SECTION2 $(LNAME2 external-class, Declaring an External Class))
18+ $(SECTION2 $(LNAME2 classes, Classes))
19+
20+ $(SECTION3 $(LNAME2 external-class, Declaring an External Class))
1921
2022 ---
2123 extern (Objective-C)
22- interface NSString
24+ class NSString
2325 {
2426 const(char)* UTF8String() @selector("UTF8String");
2527 }
2628 ---
2729
2830 $(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.
3235 )
3336
3437 $(P
3538 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.
3881 )
3982
4083 $(SECTION2 $(LNAME2 instance-method, Calling an Instance Method))
@@ -73,7 +116,7 @@ $(HEADERNAV_TOC)
73116
74117 ---
75118 extern (Objective-C)
76- interface NSString
119+ class NSString
77120 {
78121 NSString initWith(in char*) @selector("initWithUTF8String:");
79122 NSString initWith(NSString) @selector("initWithString:");
@@ -133,19 +176,19 @@ $(HEADERNAV_TOC)
133176
134177 $(P
135178 Objective-C linkage is achieved by attaching the `extern (Objective-C)`
136- attribute to an interface . Example:
179+ attribute to a class . Example:
137180 )
138181
139182 ---
140183 extern (Objective-C)
141- interface NSObject
184+ class NSObject
142185 {
143186 NSObject init() @selector("init");
144187 }
145188 ---
146189
147190 $(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
149192 get implicit Objective-C linkage.
150193 )
151194
@@ -229,7 +272,7 @@ $(HEADERNAV_TOC)
229272
230273 ---
231274 extern (Objective-C)
232- interface NSString
275+ class NSString
233276 {
234277 static NSString alloc() @selector("alloc");
235278 NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
@@ -295,7 +338,7 @@ $(HEADERNAV_TOC)
295338 module main;
296339
297340 extern (Objective-C)
298- interface NSString
341+ class NSString
299342 {
300343 static NSString alloc() @selector("alloc");
301344 NSString initWithUTF8String(in char* str) @selector("initWithUTF8String:");
0 commit comments