Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/CoreVideo/CVPixelBufferAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public CVPixelFormatType []? PixelFormatTypes {
} else if (obj is NSNumber number) {
return new CVPixelFormatType [] { (CVPixelFormatType) number.UInt32Value };
} else if (obj is NSArray array) {
return Array.ConvertAll (array.ToArray (), (v) => (CVPixelFormatType) ((NSNumber) v).UInt32Value);
return Array.ConvertAll<NSObject?, CVPixelFormatType> (array.ToArray (), (v) => (CVPixelFormatType) (((NSNumber?) v)?.UInt32Value ?? default));
Comment thread
rolfbjarne marked this conversation as resolved.
} else {
throw new InvalidOperationException ($"Unable to convert object of type {obj.GetType ()} into an array of CVPixelFormatType.");
}
Expand Down
42 changes: 32 additions & 10 deletions src/Foundation/NSArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

using CoreFoundation;

// Disable until we get around to enable + fix any issues.
#nullable disable
#nullable enable

namespace Foundation {

Expand All @@ -39,8 +38,6 @@ namespace Foundation {
#endif

public partial class NSArray : IEnumerable<NSObject> {

#nullable enable
/// <summary>Creates an NSArray from a C# array of NSObjects.</summary>
/// <param name="items">Strongly typed array of NSObjects. Null elements are stored as <see cref="NSNull.Null"/>. If the array itself is null, an empty <see cref="NSArray"/> is returned.</param>
/// <returns>A new <see cref="NSArray"/> containing the specified objects.</returns>
Expand Down Expand Up @@ -1008,17 +1005,42 @@ internal static T [] NonNullDictionaryArrayFromHandleDropNullElements<T> (Native
GC.KeepAlive (this);
return rv;
}
#nullable disable

public TKey [] ToArray<TKey> () where TKey : class, INativeObject
/// <summary>Converts this <see cref="NSArray" /> to a strongly-typed C# array, dropping null and incompatible elements.</summary>
/// <typeparam name="T">The element type for the returned array. Must be a class that implements <see cref="INativeObject" />.</typeparam>
/// <returns>A C# array of <typeparamref name="T" /> elements, excluding any null or incompatible elements.</returns>
internal T [] NonNullToArrayDropNullElements<T> () where T : class, INativeObject
{
var rv = NonNullArrayFromHandleDropNullElements<T> (Handle);
GC.KeepAlive (this);
return rv;
}

/// <summary>Converts this <see cref="NSArray" /> to a C# array by first resolving each element to <typeparamref name="V" />, then converting to <typeparamref name="T" />, dropping null elements.</summary>
/// <typeparam name="T">The target element type for the returned array.</typeparam>
/// <typeparam name="V">The intermediate native object type used to convert each element. Must be a class that implements <see cref="INativeObject" />.</typeparam>
/// <param name="createObject">A delegate to convert an instance of <typeparamref name="V" /> to <typeparamref name="T" />.</param>
/// <returns>A C# array of <typeparamref name="T" /> elements, excluding any null elements.</returns>
internal T []? NonNullToArrayDropNullElements<T, V> (Converter<V, T> createObject) where V : class, INativeObject
{
var rv = new TKey [GetCount (Handle)];
for (var i = 0; i < rv.Length; i++)
rv [i] = GetItem<TKey> ((nuint) i);
var rv = NonNullArrayFromHandleDropNullElements<T> (Handle, (handle) => createObject (Runtime.GetINativeObject<V> (handle, false)!));
GC.KeepAlive (this);
Comment thread
rolfbjarne marked this conversation as resolved.
return rv;
}

/// <summary>Converts this <see cref="NSArray" /> to a strongly-typed C# array, where <see cref="NSNull" /> elements are converted to <see langword="null" />.</summary>
/// <typeparam name="TKey">The element type for the returned array. Must be a class that implements <see cref="INativeObject" />.</typeparam>
/// <returns>A C# array of <typeparamref name="TKey" /> elements. Elements that are <see cref="NSNull" /> in the source array are represented as <see langword="null" />.</returns>
public TKey? [] ToArray<TKey> () where TKey : class, INativeObject
{
var rv = NonNullArrayFromHandle<TKey> (Handle, NSNullBehavior.ConvertToNull);
GC.KeepAlive (this);
return rv;
}

public NSObject [] ToArray ()
/// <summary>Converts this <see cref="NSArray" /> to a C# array of <see cref="NSObject" />, where <see cref="NSNull" /> elements are converted to <see langword="null" />.</summary>
/// <returns>A C# array of <see cref="NSObject" /> elements. Elements that are <see cref="NSNull" /> in the source array are represented as <see langword="null" />.</returns>
public NSObject? [] ToArray ()
{
return ToArray<NSObject> ();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Foundation/NSArray_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ public TKey? this [nint idx] {
}
}

public new TKey [] ToArray ()
/// <summary>Converts this <see cref="NSArray{TKey}" /> to a strongly-typed C# array, where <see cref="NSNull" /> elements are converted to <see langword="null" />.</summary>
/// <returns>A C# array of <typeparamref name="TKey" /> elements. Elements that are <see cref="NSNull" /> in the source array are represented as <see langword="null" />.</returns>
public new TKey? [] ToArray ()
{
return base.ToArray<TKey> ();
}
Expand Down
4 changes: 2 additions & 2 deletions src/appkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13711,7 +13711,7 @@ partial interface NSPasteboard // NSPasteboard does _not_ implement NSPasteboard
void DetectValues (HashSet<NSPasteboardDetectionPattern> patterns, NSPasteboardDetectValuesHandler completionHandler);

[Mac (15, 4)]
[Wrap ("DetectValues (NSSet<NSString>.Create (patterns, (v) => NSPasteboardDetectionPatternExtensions.GetConstant (v)!), new NSPasteboardDetectValuesHandler ((detectedValues, error) => completionHandler (detectedValues?.ToDictionary<NSPasteboardDetectionPattern, DataDetection.DDMatch[]> ((k, v) => (NSPasteboardDetectionPatternExtensions.GetValue (k), ((NSArray) v).ToArray<DataDetection.DDMatch> ())), error)))")]
[Wrap ("DetectValues (NSSet<NSString>.Create (patterns, (v) => NSPasteboardDetectionPatternExtensions.GetConstant (v)!), new NSPasteboardDetectValuesHandler ((detectedValues, error) => completionHandler (detectedValues?.ToDictionary<NSPasteboardDetectionPattern, DataDetection.DDMatch[]> ((k, v) => (NSPasteboardDetectionPatternExtensions.GetValue (k), ((NSArray) v).NonNullToArrayDropNullElements<DataDetection.DDMatch> ())), error)))")]
void DetectValues (HashSet<NSPasteboardDetectionPattern> patterns, NSPasteboardDetectValuesCompletionHandler completionHandler);

[Mac (15, 4)]
Expand Down Expand Up @@ -13943,7 +13943,7 @@ interface NSPasteboardItem : NSPasteboardWriting, NSPasteboardReading {
void DetectValues (HashSet<NSPasteboardDetectionPattern> patterns, NSPasteboardDetectValuesHandler completionHandler);

[Mac (15, 4)]
[Wrap ("DetectValues (NSSet<NSString>.Create (patterns, (v) => NSPasteboardDetectionPatternExtensions.GetConstant (v)!), new NSPasteboardDetectValuesHandler ((detectedValues, error) => completionHandler (detectedValues?.ToDictionary<NSPasteboardDetectionPattern, DataDetection.DDMatch[]> ((k, v) => (NSPasteboardDetectionPatternExtensions.GetValue (k), ((NSArray) v).ToArray<DataDetection.DDMatch> ())), error)))")]
[Wrap ("DetectValues (NSSet<NSString>.Create (patterns, (v) => NSPasteboardDetectionPatternExtensions.GetConstant (v)!), new NSPasteboardDetectValuesHandler ((detectedValues, error) => completionHandler (detectedValues?.ToDictionary<NSPasteboardDetectionPattern, DataDetection.DDMatch[]> ((k, v) => (NSPasteboardDetectionPatternExtensions.GetValue (k), ((NSArray) v).NonNullToArrayDropNullElements<DataDetection.DDMatch> ())), error)))")]
void DetectValues (HashSet<NSPasteboardDetectionPattern> patterns, NSPasteboardDetectValuesCompletionHandler completionHandler);

[Mac (15, 4)]
Expand Down
3 changes: 0 additions & 3 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11800,9 +11800,6 @@ M:Foundation.INSUrlSessionTaskDelegate.NeedNewBodyStream(Foundation.NSUrlSession
M:Foundation.INSUrlSessionWebSocketDelegate.DidClose(Foundation.NSUrlSession,Foundation.NSUrlSessionWebSocketTask,Foundation.NSUrlSessionWebSocketCloseCode,Foundation.NSData)
M:Foundation.INSUrlSessionWebSocketDelegate.DidOpen(Foundation.NSUrlSession,Foundation.NSUrlSessionWebSocketTask,System.String)
M:Foundation.INSXpcListenerDelegate.ShouldAcceptConnection(Foundation.NSXpcListener,Foundation.NSXpcConnection)
M:Foundation.NSArray.ToArray
M:Foundation.NSArray.ToArray``1
M:Foundation.NSArray`1.ToArray
M:Foundation.NSAttributedString.#ctor(System.String,AppKit.NSFont,AppKit.NSColor,AppKit.NSColor,AppKit.NSColor,AppKit.NSColor,AppKit.NSColor,Foundation.NSUnderlineStyle,Foundation.NSUnderlineStyle,AppKit.NSParagraphStyle,System.Single,AppKit.NSShadow,Foundation.NSUrl,System.Boolean,AppKit.NSTextAttachment,Foundation.NSLigatureType,System.Single,System.Single,System.Single,System.Single,AppKit.NSCursor,System.String,System.Int32,AppKit.NSGlyphInfo,Foundation.NSArray,System.Boolean,AppKit.NSTextLayoutOrientation,AppKit.NSTextAlternatives,AppKit.NSSpellingState)
M:Foundation.NSAttributedString.LoadDataAsync(System.String,Foundation.NSProgress@)
M:Foundation.NSAttributedString.LoadFromHtml(Foundation.NSData,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSAttributedStringCompletionHandler)
Expand Down
Loading