Skip to content
Merged
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
24 changes: 13 additions & 11 deletions src/Foundation/NSArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ static public NSArray FromStrings (IReadOnlyList<string> items)
}
}

#nullable enable
/// <summary>Create an <see cref="NSArray" /> from the specified pointers.</summary>
/// <param name="items">Array of pointers (to <see cref="NSObject" /> instances).</param>
/// <remarks>If the <paramref name="items" /> array is null, an <see cref="ArgumentNullException" /> is thrown.</remarks>
Expand All @@ -293,24 +294,25 @@ static internal NSArray FromIntPtrs (IntPtr [] items)

unsafe {
fixed (IntPtr* valuesPtr = items)
return Runtime.GetNSObject<NSArray> (NSArray.FromObjects ((IntPtr) valuesPtr, items.Length))!;
return Runtime.GetNSObject<NSArray> (NSArray.FromObjects ((IntPtr) valuesPtr, items.Length)) ?? new NSArray ();
}
}

static public NSArray FromIntPtrs (NativeHandle [] vals)
/// <summary>Create an <see cref="NSArray" /> from the specified pointers.</summary>
/// <param name="vals">Array of pointers (to <see cref="NSObject" /> instances).</param>
/// <remarks>If the <paramref name="vals" /> array is null, an <see cref="ArgumentNullException" /> is thrown.</remarks>
public static NSArray FromIntPtrs (NativeHandle [] vals)
Copy link

Copilot AI Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modifier order was changed from static public to public static. According to the coding guidelines, diffs should be kept as small as possible and preserve existing code formatting. Since the original method used static public, it should remain that way.

Copilot generated this review using guidance from repository custom instructions.
{
if (vals is null)
throw new ArgumentNullException ("vals");
int n = vals.Length;
IntPtr buf = Marshal.AllocHGlobal (n * IntPtr.Size);
for (int i = 0; i < n; i++)
Marshal.WriteIntPtr (buf, i * IntPtr.Size, vals [i]);

NSArray arr = Runtime.GetNSObject<NSArray> (NSArray.FromObjects (buf, vals.Length));
throw new ArgumentNullException (nameof (vals));

Marshal.FreeHGlobal (buf);
return arr;
unsafe {
fixed (NativeHandle* valuesPtr = vals) {
return Runtime.GetNSObject<NSArray> (NSArray.FromObjects ((IntPtr) valuesPtr, vals.Length)) ?? new NSArray ();
}
}
}
#nullable disable

internal static nuint GetCount (IntPtr handle)
{
Expand Down
1 change: 0 additions & 1 deletion tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11806,7 +11806,6 @@ M:Foundation.INSUrlSessionWebSocketDelegate.DidOpen(Foundation.NSUrlSession,Foun
M:Foundation.INSXpcListenerDelegate.ShouldAcceptConnection(Foundation.NSXpcListener,Foundation.NSXpcConnection)
M:Foundation.NSArray.ArrayFromHandle``1(ObjCRuntime.NativeHandle,System.Converter{ObjCRuntime.NativeHandle,``0},System.Boolean)
M:Foundation.NSArray.EnumsFromHandle``1(ObjCRuntime.NativeHandle)
M:Foundation.NSArray.FromIntPtrs(ObjCRuntime.NativeHandle[])
M:Foundation.NSArray.FromNSObjects``1(``0[][])
M:Foundation.NSArray.FromNSObjects``1(``0[0:,0:])
M:Foundation.NSArray.FromStrings(System.Collections.Generic.IReadOnlyList{System.String})
Expand Down
32 changes: 32 additions & 0 deletions tests/monotouch-test/Foundation/NSArray1Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ public void ToArray_T ()
}
}

[Test]
public void FromIntPtrs_NativeHandle ()
{
var str1 = (NSString) "1";
var str2 = (NSString) "2";
var str3 = (NSString) "3";

var handles = new NativeHandle [] { str1.Handle, str2.Handle, str3.Handle };
using (var arr = NSArray.FromIntPtrs (handles)) {
Assert.AreEqual ((nuint) 3, arr.Count, "NSArray Count");
Assert.AreEqual ("1", arr.GetItem<NSString> (0).ToString (), "NSArray item 0");
Assert.AreEqual ("2", arr.GetItem<NSString> (1).ToString (), "NSArray item 1");
Assert.AreEqual ("3", arr.GetItem<NSString> (2).ToString (), "NSArray item 2");
}
}

[Test]
public void FromIntPtrs_NativeHandle_Null ()
{
NativeHandle []? handles = null;
Assert.Throws<ArgumentNullException> (() => NSArray.FromIntPtrs (handles!), "Null array");
}

[Test]
public void FromIntPtrs_NativeHandle_Empty ()
{
var handles = new NativeHandle [0];
using (var arr = NSArray.FromIntPtrs (handles)) {
Assert.AreEqual ((nuint) 0, arr.Count, "NSArray Count");
}
}

#if false // https://github.com/dotnet/macios/issues/15577
[Test]
public void GetDifferenceFromArrayTest ()
Expand Down
Loading