summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-29 13:17:30 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-29 13:17:30 +0000
commitfc6ff10acb9d00ea1c8c5924869e0efbd38093c5 (patch)
treea4cb5d9d9d0ea2702de42df1e3c2fbe40185e293 /examples
parentda9e8758e9d89dc7362be67f8e7573309efe170c (diff)
Objective-C interop + a few improvements
Diffstat (limited to 'examples')
-rw-r--r--examples/objc_interop.zc42
1 files changed, 42 insertions, 0 deletions
diff --git a/examples/objc_interop.zc b/examples/objc_interop.zc
new file mode 100644
index 0000000..c33fd0d
--- /dev/null
+++ b/examples/objc_interop.zc
@@ -0,0 +1,42 @@
+
+//> macos: framework: Foundation
+//> linux: cflags: -fconstant-string-class=NSConstantString -D_NATIVE_OBJC_EXCEPTIONS -I/usr/include/x86_64-linux-gnu/GNUstep -std=gnu99
+//> linux: link: -lgnustep-base -lobjc
+
+include <Foundation/Foundation.h>
+
+raw {
+ id make_nsstring(const char* s) {
+ return [NSString stringWithUTF8String:s];
+ }
+
+ void print_description(id obj) {
+ NSLog(@"[ObjC Log] Description: %@", obj);
+ }
+}
+
+fn main() {
+
+ raw {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ }
+
+ "=> Zen C + Objective-C interop demo.";
+ "=> (Make sure to run with: zc run --objc examples/objc_interop.zc)";
+
+ // Call ObjC helper to create an NSString (returned as id)
+ let s = make_nsstring("Hello from Objective-C!");
+
+ // Pass it back to ObjC
+ print_description(s);
+
+ "Zen C interpolated string: {s}";
+
+ raw {
+ // You can also raw ObjC syntax anywhere.
+ NSArray *arr = [NSArray arrayWithObjects: @"Zen", @"Bit", @"C", nil];
+ NSLog(@"[ObjC Raw] Array: %@", arr);
+
+ [pool drain];
+ }
+}