Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public NSData(IntPtr pointer) : base(pointer)
public NSData(byte[] bytes) : base(InitWithBytes(bytes))
{
}

/// <summary>
/// Default constructor that initializes an empty NSData instance.
/// </summary>
public NSData() : base(CreateEmptyNSData())
{
}

private static IntPtr InitWithBytes(byte[] bytes)
{
Expand Down Expand Up @@ -66,12 +73,23 @@ public byte[] Bytes
/// Helper method to get the byte array from an interop IntPtr to NSData.
/// </summary>
public static byte[] GetBytes(IntPtr pointer) => (pointer != default) ? new NSData(pointer).Bytes : Array.Empty<byte>();

/// <summary>
/// Creates an empty NSData instance using a native method.
/// </summary>
/// <returns>Pointer to the newly created empty NSData object.</returns>
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);

}
}
}
11 changes: 11 additions & 0 deletions plug-ins/Apple.Core/Native/AppleCoreNative/NSData.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}