module
UI::ObjC
Overview
Factory methods for creating NativeHandle instances that wrap ObjC objects.
Each factory encodes the correct ReleaseStrategy for the ownership model:
-
.owned: Crystal owns the pointer (+1 retain count). Will callobjc_releaseon cleanup. Use for objects returned byalloc/init,copy,mutableCopy, or any method where Crystal has explicitly retained the result. -
.borrowed: Crystal does NOT own the pointer. No release will occur. Use for autoreleased return values, pointers borrowed from containers, or objects whose lifetime is managed by a parent view hierarchy. -
.retain: Immediately callsobjc_retainon the pointer to take ownership, then wraps it withObjCReleasestrategy. Use when you receive an autoreleased object that you need to store beyond the current autorelease scope. -
.class_ref: Wraps an ObjC class pointer (e.g., fromobjc_getClass). Class objects are never released; usesUnownedstrategy.
All factories are gated behind flag?(:darwin) to ensure zero ObjC code
compiles on non-Apple platforms.
Defined in:
ui/native/objc_handle.crClass Method Summary
-
.borrowed(ptr : Pointer(Void), label : String | Nil = nil) : NativeHandle
Wrap a borrowed ObjC pointer that Crystal does NOT own.
-
.class_ref(ptr : Pointer(Void), label : String | Nil = nil) : NativeHandle
Wrap an ObjC class object pointer.
-
.owned(ptr : Pointer(Void), label : String | Nil = nil) : NativeHandle
Wrap an ObjC pointer that Crystal already owns (+1 retain count).
-
.retain(ptr : Pointer(Void), label : String | Nil = nil) : NativeHandle
Retain an ObjC pointer (+1) and wrap it as owned.
Class Method Detail
Wrap a borrowed ObjC pointer that Crystal does NOT own. No release action will be taken.
Wrap an ObjC class object pointer.
Class objects are singletons managed by the ObjC runtime and are
never released. Uses Unowned strategy.
Wrap an ObjC pointer that Crystal already owns (+1 retain count).
The handle will call objc_release when released.
Retain an ObjC pointer (+1) and wrap it as owned. Use when you need to extend the lifetime of an autoreleased object.