From 088cdf99705c46890f6aa020fa51bdc8ff0d35a0 Mon Sep 17 00:00:00 2001 From: Eliseo Diego Viola Date: Mon, 1 Sep 2025 13:53:31 +0200 Subject: [PATCH] added default constructor to NSData that will not be stripped --- .../Assets/Apple.Core/Runtime/NSData.cs | 18 ++++++++++++++++++ .../Apple.Core/Native/AppleCoreNative/NSData.m | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs b/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs index 05d8ae1a..72285dd1 100644 --- a/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs +++ b/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs @@ -23,6 +23,13 @@ public NSData(IntPtr pointer) : base(pointer) public NSData(byte[] bytes) : base(InitWithBytes(bytes)) { } + + /// + /// Default constructor that initializes an empty NSData instance. + /// + public NSData() : base(CreateEmptyNSData()) + { + } private static IntPtr InitWithBytes(byte[] bytes) { @@ -66,12 +73,23 @@ public byte[] Bytes /// Helper method to get the byte array from an interop IntPtr to NSData. /// public static byte[] GetBytes(IntPtr pointer) => (pointer != default) ? new NSData(pointer).Bytes : Array.Empty(); + + /// + /// Creates an empty NSData instance using a native method. + /// + /// Pointer to the newly created empty NSData object. + private static IntPtr CreateEmptyNSData() + { + return Interop.NSData_Empty(NSException.ThrowOnExceptionCallback); + } private static class Interop { [DllImport(InteropUtility.DLLName)] public static extern IntPtr NSData_InitWithBytes(IntPtr bytes, UInt64 length, NSExceptionCallback onException); [DllImport(InteropUtility.DLLName)] public static extern UInt64 NSData_GetLength(IntPtr nsDataPtr, NSExceptionCallback onException); [DllImport(InteropUtility.DLLName)] public static extern IntPtr NSData_GetBytes(IntPtr nsDataPtr, NSExceptionCallback onException); + [DllImport(InteropUtility.DLLName)] public static extern IntPtr NSData_Empty(NSExceptionCallback onException); + } } } diff --git a/plug-ins/Apple.Core/Native/AppleCoreNative/NSData.m b/plug-ins/Apple.Core/Native/AppleCoreNative/NSData.m index af31bafd..be1bdf44 100644 --- a/plug-ins/Apple.Core/Native/AppleCoreNative/NSData.m +++ b/plug-ins/Apple.Core/Native/AppleCoreNative/NSData.m @@ -40,3 +40,14 @@ NSUInteger NSData_GetLength(void * nsDataPtr, void (* exceptionCallback)(void * } return 0; } + +const void * NSData_Empty(void (*exceptionCallback)(void * nsExceptionPtr)) { + @try { + NSData *data = [NSData data]; + return (__bridge_retained const void *)data; + } + @catch (NSException * e) { + exceptionCallback((void *)CFBridgingRetain(e)); + } + return NULL; +}