Skip to content

Commit 5256562

Browse files
authored
[Foundation] Fix nullability in NSMutableOrderedSet<TKey>. (#24423)
This is file 33 of 47 files with nullability disabled in Foundation. Changes: * Enabled nullability by replacing '#nullable disable' with '#nullable enable' * Removed [SupportedOSPlatform] attributes without version numbers * Replaced 'To be added' comments with proper XML documentation * Added comprehensive XML comments for all public members * Added 'see cref' attributes for type references * Fixed whitespace in XML comments * Updated exception handling to use ArgumentNullException.ThrowIfNull * Added null-forgiving operators where necessary for Runtime.GetINativeObject calls * Made operator overload parameters and return types nullable where appropriate * Add more tests. Contributes towards #17285.
1 parent 8a64cf2 commit 5256562

File tree

3 files changed

+636
-114
lines changed

3 files changed

+636
-114
lines changed

src/Foundation/NSMutableOrderedSet_1.cs

Lines changed: 102 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,27 @@
99

1010
using System.Collections.Generic;
1111
using System.Collections;
12+
using System.Diagnostics.CodeAnalysis;
1213

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

1616
namespace Foundation {
17-
[SupportedOSPlatform ("ios")]
18-
[SupportedOSPlatform ("maccatalyst")]
19-
[SupportedOSPlatform ("macos")]
20-
[SupportedOSPlatform ("tvos")]
2117
[Register ("NSMutableOrderedSet", SkipRegistration = true)]
2218
public sealed partial class NSMutableOrderedSet<TKey> : NSMutableOrderedSet, IEnumerable<TKey>
2319
where TKey : class, INativeObject {
2420

25-
/// <summary>To be added.</summary>
26-
/// <remarks>To be added.</remarks>
21+
/// <summary>Initializes a new empty mutable ordered set.</summary>
2722
public NSMutableOrderedSet ()
2823
{
2924
}
3025

26+
/// <summary>A constructor that initializes the object from the data stored in the unarchiver object.</summary>
3127
/// <param name="coder">The unarchiver object.</param>
32-
/// <summary>A constructor that initializes the object from the data stored in the unarchiver object.</summary>
33-
/// <remarks>
34-
/// <para>This constructor is provided to allow the class to be initialized from an unarchiver (for example, during NIB deserialization). This is part of the <see cref="Foundation.NSCoding" /> protocol.</para>
35-
/// <para>If developers want to create a subclass of this object and continue to support deserialization from an archive, they should implement a constructor with an identical signature: taking a single parameter of type <see cref="Foundation.NSCoder" /> and decorate it with the [Export("initWithCoder:"] attribute declaration.</para>
36-
/// <para>The state of this object can also be serialized by using the companion method, EncodeTo.</para>
37-
/// </remarks>
28+
/// <remarks>
29+
/// <para>This constructor is provided to allow the class to be initialized from an unarchiver (for example, during NIB deserialization). This is part of the <see cref="Foundation.NSCoding" /> protocol.</para>
30+
/// <para>If developers want to create a subclass of this object and continue to support deserialization from an archive, they should implement a constructor with an identical signature: taking a single parameter of type <see cref="Foundation.NSCoder" /> and decorate it with the [Export("initWithCoder:")] attribute declaration.</para>
31+
/// <para>The state of this object can also be serialized by using the companion method, EncodeTo.</para>
32+
/// </remarks>
3833
public NSMutableOrderedSet (NSCoder coder) : base (coder)
3934
{
4035
}
@@ -43,192 +38,165 @@ internal NSMutableOrderedSet (NativeHandle handle) : base (handle)
4338
{
4439
}
4540

46-
/// <param name="capacity">To be added.</param>
47-
/// <summary>To be added.</summary>
48-
/// <remarks>To be added.</remarks>
41+
/// <summary>Initializes a new mutable ordered set with the specified initial capacity.</summary>
42+
/// <param name="capacity">The initial capacity of the set.</param>
4943
public NSMutableOrderedSet (nint capacity) : base (capacity)
5044
{
5145
}
5246

53-
/// <param name="start">To be added.</param>
54-
/// <summary>To be added.</summary>
55-
/// <remarks>To be added.</remarks>
47+
/// <summary>Initializes a new mutable ordered set containing the specified object.</summary>
48+
/// <param name="start">The object to add to the set.</param>
5649
public NSMutableOrderedSet (TKey start) : base (start)
5750
{
5851
}
5952

60-
/// <param name="objs">To be added.</param>
61-
/// <summary>To be added.</summary>
62-
/// <remarks>To be added.</remarks>
53+
/// <summary>Initializes a new mutable ordered set with the specified objects.</summary>
54+
/// <param name="objs">An array of objects to add to the set.</param>
6355
public NSMutableOrderedSet (params TKey [] objs) : base (objs)
6456
{
6557
}
6658

67-
/// <param name="source">To be added.</param>
68-
/// <summary>To be added.</summary>
69-
/// <remarks>To be added.</remarks>
59+
/// <summary>Initializes a new mutable ordered set with the contents of the specified set.</summary>
60+
/// <param name="source">The source set to copy from.</param>
7061
public NSMutableOrderedSet (NSSet<TKey> source) : base (source)
7162
{
7263
}
7364

74-
/// <param name="other">To be added.</param>
75-
/// <summary>To be added.</summary>
76-
/// <remarks>To be added.</remarks>
65+
/// <summary>Initializes a new mutable ordered set with the contents of the specified ordered set.</summary>
66+
/// <param name="other">The ordered set to copy from.</param>
7767
public NSMutableOrderedSet (NSOrderedSet<TKey> other) : base (other)
7868
{
7969
}
8070

81-
/// <param name="other">To be added.</param>
82-
/// <summary>To be added.</summary>
83-
/// <remarks>To be added.</remarks>
71+
/// <summary>Initializes a new mutable ordered set with the contents of the specified mutable ordered set.</summary>
72+
/// <param name="other">The mutable ordered set to copy from.</param>
8473
public NSMutableOrderedSet (NSMutableOrderedSet<TKey> other) : base (other)
8574
{
8675
}
8776

77+
/// <summary>Gets or sets the object at the specified index.</summary>
78+
/// <param name="idx">The index of the object.</param>
79+
/// <returns>The object at the specified index.</returns>
8880
public new TKey this [nint idx] {
8981
get {
9082
var ret = _GetObject (idx);
91-
return Runtime.GetINativeObject<TKey> (ret, false);
83+
return Runtime.GetINativeObject<TKey> (ret, false)!;
9284
}
9385

9486
set {
95-
if (value is null) // You can't pass nil here
96-
throw new ArgumentNullException (nameof (value));
97-
87+
ArgumentNullException.ThrowIfNull (value);
9888
_SetObject (value.Handle, idx);
9989
GC.KeepAlive (value);
10090
}
10191
}
10292

103-
/// <summary>To be added.</summary>
104-
/// <returns>To be added.</returns>
105-
/// <remarks>To be added.</remarks>
93+
/// <summary>Returns a set containing the objects from this ordered set.</summary>
94+
/// <returns>A new <see cref="NSSet{TKey}"/> containing the same objects as this ordered set.</returns>
10695
public NSSet<TKey> AsSet ()
10796
{
10897
var ret = _AsSet ();
109-
return Runtime.GetINativeObject<NSSet<TKey>> (ret, false);
98+
return Runtime.GetINativeObject<NSSet<TKey>> (ret, false)!;
11099
}
111100

112-
/// <param name="obj">To be added.</param>
113-
/// <param name="atIndex">To be added.</param>
114-
/// <summary>To be added.</summary>
115-
/// <remarks>To be added.</remarks>
101+
/// <summary>Inserts the specified object at the specified index in the ordered set.</summary>
102+
/// <param name="obj">The object to insert.</param>
103+
/// <param name="atIndex">The index at which to insert the object.</param>
116104
public void Insert (TKey obj, nint atIndex)
117105
{
118-
if (obj is null)
119-
throw new ArgumentNullException (nameof (obj));
120-
106+
ArgumentNullException.ThrowIfNull (obj);
121107
_Insert (obj.Handle, atIndex);
122108
GC.KeepAlive (obj);
123109
}
124110

125-
/// <param name="objectAtIndex">To be added.</param>
126-
/// <param name="newObject">To be added.</param>
127-
/// <summary>To be added.</summary>
128-
/// <remarks>To be added.</remarks>
111+
/// <summary>Replaces the object at the specified index with a new object.</summary>
112+
/// <param name="objectAtIndex">The index of the object to replace.</param>
113+
/// <param name="newObject">The new object to place at the specified index.</param>
129114
public void Replace (nint objectAtIndex, TKey newObject)
130115
{
131-
if (newObject is null)
132-
throw new ArgumentNullException (nameof (newObject));
133-
116+
ArgumentNullException.ThrowIfNull (newObject);
134117
_Replace (objectAtIndex, newObject.Handle);
135118
GC.KeepAlive (newObject);
136119
}
137120

138-
/// <param name="obj">To be added.</param>
139-
/// <summary>To be added.</summary>
140-
/// <remarks>To be added.</remarks>
121+
/// <summary>Adds the specified object to the ordered set.</summary>
122+
/// <param name="obj">The object to add to the set.</param>
141123
public void Add (TKey obj)
142124
{
143-
if (obj is null)
144-
throw new ArgumentNullException (nameof (obj));
145-
125+
ArgumentNullException.ThrowIfNull (obj);
146126
_Add (obj.Handle);
147127
GC.KeepAlive (obj);
148128
}
149129

150-
/// <param name="source">To be added.</param>
151-
/// <summary>To be added.</summary>
152-
/// <remarks>To be added.</remarks>
130+
/// <summary>Adds the objects in the specified array to the ordered set.</summary>
131+
/// <param name="source">An array of objects to add to the set.</param>
153132
public void AddObjects (params TKey [] source)
154133
{
155-
if (source is null)
156-
throw new ArgumentNullException (nameof (source));
157-
134+
ArgumentNullException.ThrowIfNull (source);
158135
_AddObjects (NSArray.FromNativeObjects (source));
159136
}
160137

161-
/// <param name="objects">To be added.</param>
162-
/// <param name="atIndexes">To be added.</param>
163-
/// <summary>To be added.</summary>
164-
/// <remarks>To be added.</remarks>
138+
/// <summary>Inserts the specified objects at the specified indexes in the ordered set.</summary>
139+
/// <param name="objects">An array of objects to insert.</param>
140+
/// <param name="atIndexes">The indexes at which to insert the objects.</param>
165141
public void InsertObjects (TKey [] objects, NSIndexSet atIndexes)
166142
{
167-
if (objects is null)
168-
throw new ArgumentNullException (nameof (objects));
169-
if (atIndexes is null)
170-
throw new ArgumentNullException (nameof (atIndexes));
171-
143+
ArgumentNullException.ThrowIfNull (objects);
144+
ArgumentNullException.ThrowIfNull (atIndexes);
172145
_InsertObjects (NSArray.FromNativeObjects (objects), atIndexes);
173146
}
174147

175-
/// <param name="indexSet">To be added.</param>
176-
/// <param name="replacementObjects">To be added.</param>
177-
/// <summary>To be added.</summary>
178-
/// <remarks>To be added.</remarks>
148+
/// <summary>Replaces the objects at the specified indexes with the specified replacement objects.</summary>
149+
/// <param name="indexSet">The indexes of the objects to replace.</param>
150+
/// <param name="replacementObjects">An array of objects to use as replacements.</param>
179151
public void ReplaceObjects (NSIndexSet indexSet, params TKey [] replacementObjects)
180152
{
181-
if (replacementObjects is null)
182-
throw new ArgumentNullException (nameof (replacementObjects));
183-
if (indexSet is null)
184-
throw new ArgumentNullException (nameof (indexSet));
185-
153+
ArgumentNullException.ThrowIfNull (replacementObjects);
154+
ArgumentNullException.ThrowIfNull (indexSet);
186155
_ReplaceObjects (indexSet, NSArray.FromNativeObjects (replacementObjects));
187156
}
188157

189-
/// <param name="obj">To be added.</param>
190-
/// <summary>To be added.</summary>
191-
/// <remarks>To be added.</remarks>
158+
/// <summary>Removes the specified object from the ordered set.</summary>
159+
/// <param name="obj">The object to remove from the set.</param>
192160
public void RemoveObject (TKey obj)
193161
{
194-
if (obj is null)
195-
throw new ArgumentNullException (nameof (obj));
196-
162+
ArgumentNullException.ThrowIfNull (obj);
197163
_RemoveObject (obj.Handle);
198164
GC.KeepAlive (obj);
199165
}
200166

201-
/// <param name="objects">To be added.</param>
202-
/// <summary>To be added.</summary>
203-
/// <remarks>To be added.</remarks>
167+
/// <summary>Removes the specified objects from the ordered set.</summary>
168+
/// <param name="objects">An array of objects to remove from the set.</param>
204169
public void RemoveObjects (params TKey [] objects)
205170
{
206-
if (objects is null)
207-
throw new ArgumentNullException (nameof (objects));
208-
171+
ArgumentNullException.ThrowIfNull (objects);
209172
_RemoveObjects (NSArray.FromNativeObjects (objects));
210173
}
211174

212175
#region IEnumerable<TKey>
213-
/// <summary>Returns an enumerator that iterates through the set.</summary>
214-
/// <returns>An enumerator that can be used to iterate through the set.</returns>
176+
/// <summary>Returns an enumerator that iterates through the ordered set.</summary>
177+
/// <returns>An enumerator that can be used to iterate through the ordered set.</returns>
215178
public new IEnumerator<TKey> GetEnumerator ()
216179
{
217180
return new NSFastEnumerator<TKey> (this);
218181
}
219182
#endregion
220183

221184
#region IEnumerable implementation
222-
/// <summary>To be added.</summary>
223-
/// <returns>To be added.</returns>
224-
/// <remarks>To be added.</remarks>
185+
/// <summary>Returns an enumerator that iterates through the ordered set.</summary>
186+
/// <returns>An enumerator that can be used to iterate through the ordered set.</returns>
225187
IEnumerator IEnumerable.GetEnumerator ()
226188
{
227189
return new NSFastEnumerator<TKey> (this);
228190
}
229191
#endregion
230192

231-
public static NSMutableOrderedSet<TKey> operator + (NSMutableOrderedSet<TKey> first, NSMutableOrderedSet<TKey> second)
193+
/// <summary>Computes the union of two ordered sets.</summary>
194+
/// <param name="first">The first ordered set.</param>
195+
/// <param name="second">The second ordered set.</param>
196+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing all objects from both ordered sets, or <see langword="null"/> if both are <see langword="null"/>.</returns>
197+
[return: NotNullIfNotNull (nameof (first))]
198+
[return: NotNullIfNotNull (nameof (second))]
199+
public static NSMutableOrderedSet<TKey>? operator + (NSMutableOrderedSet<TKey>? first, NSMutableOrderedSet<TKey>? second)
232200
{
233201
if (first is null)
234202
return second is not null ? new NSMutableOrderedSet<TKey> (second) : null;
@@ -239,7 +207,13 @@ IEnumerator IEnumerable.GetEnumerator ()
239207
return copy;
240208
}
241209

242-
public static NSMutableOrderedSet<TKey> operator + (NSMutableOrderedSet<TKey> first, NSSet<TKey> second)
210+
/// <summary>Computes the union of an ordered set and a set.</summary>
211+
/// <param name="first">The ordered set.</param>
212+
/// <param name="second">The set to add to the ordered set.</param>
213+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing all objects from both collections, or <see langword="null"/> if both are <see langword="null"/>.</returns>
214+
[return: NotNullIfNotNull (nameof (first))]
215+
[return: NotNullIfNotNull (nameof (second))]
216+
public static NSMutableOrderedSet<TKey>? operator + (NSMutableOrderedSet<TKey>? first, NSSet<TKey>? second)
243217
{
244218
if (first is null)
245219
return second is not null ? new NSMutableOrderedSet<TKey> (second) : null;
@@ -250,7 +224,13 @@ IEnumerator IEnumerable.GetEnumerator ()
250224
return copy;
251225
}
252226

253-
public static NSMutableOrderedSet<TKey> operator + (NSMutableOrderedSet<TKey> first, NSOrderedSet<TKey> second)
227+
/// <summary>Computes the union of two ordered sets.</summary>
228+
/// <param name="first">The first ordered set.</param>
229+
/// <param name="second">The second ordered set to add.</param>
230+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing all objects from both ordered sets, or <see langword="null"/> if both are <see langword="null"/>.</returns>
231+
[return: NotNullIfNotNull (nameof (first))]
232+
[return: NotNullIfNotNull (nameof (second))]
233+
public static NSMutableOrderedSet<TKey>? operator + (NSMutableOrderedSet<TKey>? first, NSOrderedSet<TKey>? second)
254234
{
255235
if (first is null)
256236
return second is not null ? new NSMutableOrderedSet<TKey> (second) : null;
@@ -261,7 +241,12 @@ IEnumerator IEnumerable.GetEnumerator ()
261241
return copy;
262242
}
263243

264-
public static NSMutableOrderedSet<TKey> operator - (NSMutableOrderedSet<TKey> first, NSMutableOrderedSet<TKey> second)
244+
/// <summary>Computes the difference between two ordered sets.</summary>
245+
/// <param name="first">The ordered set to subtract from.</param>
246+
/// <param name="second">The ordered set to subtract.</param>
247+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing objects in <paramref name="first"/> but not in <paramref name="second"/>, or <see langword="null"/> if <paramref name="first"/> is <see langword="null"/>.</returns>
248+
[return: NotNullIfNotNull (nameof (first))]
249+
public static NSMutableOrderedSet<TKey>? operator - (NSMutableOrderedSet<TKey>? first, NSMutableOrderedSet<TKey>? second)
265250
{
266251
if (first is null)
267252
return null;
@@ -272,7 +257,12 @@ IEnumerator IEnumerable.GetEnumerator ()
272257
return copy;
273258
}
274259

275-
public static NSMutableOrderedSet<TKey> operator - (NSMutableOrderedSet<TKey> first, NSSet<TKey> second)
260+
/// <summary>Computes the difference between an ordered set and a set.</summary>
261+
/// <param name="first">The ordered set to subtract from.</param>
262+
/// <param name="second">The set to subtract.</param>
263+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing objects in <paramref name="first"/> but not in <paramref name="second"/>, or <see langword="null"/> if <paramref name="first"/> is <see langword="null"/>.</returns>
264+
[return: NotNullIfNotNull (nameof (first))]
265+
public static NSMutableOrderedSet<TKey>? operator - (NSMutableOrderedSet<TKey>? first, NSSet<TKey>? second)
276266
{
277267
if (first is null)
278268
return null;
@@ -283,7 +273,12 @@ IEnumerator IEnumerable.GetEnumerator ()
283273
return copy;
284274
}
285275

286-
public static NSMutableOrderedSet<TKey> operator - (NSMutableOrderedSet<TKey> first, NSOrderedSet<TKey> second)
276+
/// <summary>Computes the difference between two ordered sets.</summary>
277+
/// <param name="first">The ordered set to subtract from.</param>
278+
/// <param name="second">The ordered set to subtract.</param>
279+
/// <returns>A new <see cref="NSMutableOrderedSet{TKey}"/> containing objects in <paramref name="first"/> but not in <paramref name="second"/>, or <see langword="null"/> if <paramref name="first"/> is <see langword="null"/>.</returns>
280+
[return: NotNullIfNotNull (nameof (first))]
281+
public static NSMutableOrderedSet<TKey>? operator - (NSMutableOrderedSet<TKey>? first, NSOrderedSet<TKey>? second)
287282
{
288283
if (first is null)
289284
return null;

tests/cecil-tests/Documentation.KnownFailures.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11853,12 +11853,6 @@ M:Foundation.NSMachPort.Dispose(System.Boolean)
1185311853
M:Foundation.NSMetadataQuery.Dispose(System.Boolean)
1185411854
M:Foundation.NSMutableDictionary.LowlevelSetObject(System.String,System.IntPtr)
1185511855
M:Foundation.NSMutableDictionary`2.FromObjectsAndKeys(`1[],`0[])
11856-
M:Foundation.NSMutableOrderedSet`1.op_Addition(Foundation.NSMutableOrderedSet{`0},Foundation.NSMutableOrderedSet{`0})
11857-
M:Foundation.NSMutableOrderedSet`1.op_Addition(Foundation.NSMutableOrderedSet{`0},Foundation.NSOrderedSet{`0})
11858-
M:Foundation.NSMutableOrderedSet`1.op_Addition(Foundation.NSMutableOrderedSet{`0},Foundation.NSSet{`0})
11859-
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSMutableOrderedSet{`0})
11860-
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSOrderedSet{`0})
11861-
M:Foundation.NSMutableOrderedSet`1.op_Subtraction(Foundation.NSMutableOrderedSet{`0},Foundation.NSSet{`0})
1186211856
M:Foundation.NSMutableSet.op_Addition(Foundation.NSMutableSet,Foundation.NSMutableSet)
1186311857
M:Foundation.NSMutableSet.op_Subtraction(Foundation.NSMutableSet,Foundation.NSMutableSet)
1186411858
M:Foundation.NSMutableSet`1.op_Addition(Foundation.NSMutableSet{`0},Foundation.NSMutableSet{`0})
@@ -21042,7 +21036,6 @@ P:Foundation.NSMutableDictionary.Item(Foundation.NSObject)
2104221036
P:Foundation.NSMutableDictionary.Item(Foundation.NSString)
2104321037
P:Foundation.NSMutableDictionary.Item(System.String)
2104421038
P:Foundation.NSMutableDictionary`2.Item(`0)
21045-
P:Foundation.NSMutableOrderedSet`1.Item(System.IntPtr)
2104621039
P:Foundation.NSNetDomainEventArgs.Domain
2104721040
P:Foundation.NSNetDomainEventArgs.MoreComing
2104821041
P:Foundation.NSNetServiceConnectionEventArgs.InputStream

0 commit comments

Comments
 (0)