From 71042b6f1b54e7352a9d31e3a6de0ea0a7d14e99 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 28 Oct 2022 09:36:56 -0700 Subject: [PATCH] pkg/objc: more methods --- pkg/objc/object.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/objc/object.zig b/pkg/objc/object.zig index f6ef6e427..cd0352496 100644 --- a/pkg/objc/object.zig +++ b/pkg/objc/object.zig @@ -7,6 +7,22 @@ pub const Object = struct { value: c.id, pub usingnamespace MsgSend(Object); + + pub fn fromId(id: anytype) Object { + return .{ .value = @ptrCast(c.id, @alignCast(@alignOf(c.id), id)) }; + } + + /// Returns the class of an object. + pub fn getClass(self: Object) ?objc.Class { + return objc.Class{ + .value = c.object_getClass(self.value) orelse return null, + }; + } + + /// Returns the class name of a given object. + pub fn getClassName(self: Object) [:0]const u8 { + return std.mem.sliceTo(c.object_getClassName(self.value), 0); + } }; test { @@ -16,5 +32,6 @@ test { // Should work with our wrappers const obj = NSObject.msgSend(objc.Object, objc.Sel.registerName("alloc"), .{}); try testing.expect(obj.value != null); + try testing.expectEqualStrings("NSObject", obj.getClassName()); obj.msgSend(void, objc.sel("dealloc"), .{}); }