Skip to content

Commit 298a4dd

Browse files
authored
[Foundation] Fix nullability in NSCoder. (#24179)
This is file 2 of 47 files with nullability disabled in Foundation. Also: * Remove redundant availability attributes. * Add xml docs (by Copilot). Contributes towards #17285.
1 parent eafd219 commit 298a4dd

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

src/Foundation/NSCoder.cs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
//
2828
using System.Collections.Generic;
2929

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

3332
namespace Foundation {
3433

@@ -96,7 +95,7 @@ public void Encode (byte [] buffer, int offset, int count, string key)
9695
/// <summary>Decodes the requested key as an array of bytes.</summary>
9796
/// <param name="key">The key identifying the item to decode.</param>
9897
/// <returns>The decoded array of bytes.</returns>
99-
public byte [] DecodeBytes (string key)
98+
public byte []? DecodeBytes (string key)
10099
{
101100
nuint len = 0;
102101
IntPtr ret = DecodeBytes (key, out len);
@@ -112,7 +111,7 @@ public byte [] DecodeBytes (string key)
112111
/// <remarks>The decoded array of bytes.</remarks>
113112
/// <summary>Decodes the next item as an array of bytes.</summary>
114113
/// <returns>The array of bytes decoded from the stream.</returns>
115-
public byte [] DecodeBytes ()
114+
public byte []? DecodeBytes ()
116115
{
117116
nuint len = 0;
118117
IntPtr ret = DecodeBytes (out len);
@@ -125,6 +124,10 @@ public byte [] DecodeBytes ()
125124
return retarray;
126125
}
127126

127+
/// <summary>Attempts to decode a boolean value associated with the specified key.</summary>
128+
/// <param name="key">The key identifying the item to decode.</param>
129+
/// <param name="result">When this method returns, contains the decoded boolean value if the key exists; otherwise, false.</param>
130+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
128131
public bool TryDecode (string key, out bool result)
129132
{
130133
if (ContainsKey (key)) {
@@ -135,6 +138,10 @@ public bool TryDecode (string key, out bool result)
135138
return false;
136139
}
137140

141+
/// <summary>Attempts to decode a double value associated with the specified key.</summary>
142+
/// <param name="key">The key identifying the item to decode.</param>
143+
/// <param name="result">When this method returns, contains the decoded double value if the key exists; otherwise, 0.</param>
144+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
138145
public bool TryDecode (string key, out double result)
139146
{
140147
if (ContainsKey (key)) {
@@ -145,6 +152,10 @@ public bool TryDecode (string key, out double result)
145152
return false;
146153
}
147154

155+
/// <summary>Attempts to decode a float value associated with the specified key.</summary>
156+
/// <param name="key">The key identifying the item to decode.</param>
157+
/// <param name="result">When this method returns, contains the decoded float value if the key exists; otherwise, 0.</param>
158+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
148159
public bool TryDecode (string key, out float result)
149160
{
150161
if (ContainsKey (key)) {
@@ -155,6 +166,10 @@ public bool TryDecode (string key, out float result)
155166
return false;
156167
}
157168

169+
/// <summary>Attempts to decode an integer value associated with the specified key.</summary>
170+
/// <param name="key">The key identifying the item to decode.</param>
171+
/// <param name="result">When this method returns, contains the decoded integer value if the key exists; otherwise, 0.</param>
172+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
158173
public bool TryDecode (string key, out int result)
159174
{
160175
if (ContainsKey (key)) {
@@ -165,6 +180,10 @@ public bool TryDecode (string key, out int result)
165180
return false;
166181
}
167182

183+
/// <summary>Attempts to decode a long integer value associated with the specified key.</summary>
184+
/// <param name="key">The key identifying the item to decode.</param>
185+
/// <param name="result">When this method returns, contains the decoded long integer value if the key exists; otherwise, 0.</param>
186+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
168187
public bool TryDecode (string key, out long result)
169188
{
170189
if (ContainsKey (key)) {
@@ -175,6 +194,10 @@ public bool TryDecode (string key, out long result)
175194
return false;
176195
}
177196

197+
/// <summary>Attempts to decode a native integer value associated with the specified key.</summary>
198+
/// <param name="key">The key identifying the item to decode.</param>
199+
/// <param name="result">When this method returns, contains the decoded native integer value if the key exists; otherwise, 0.</param>
200+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
178201
public bool TryDecode (string key, out nint result)
179202
{
180203
if (ContainsKey (key)) {
@@ -185,7 +208,11 @@ public bool TryDecode (string key, out nint result)
185208
return false;
186209
}
187210

188-
public bool TryDecode (string key, out NSObject result)
211+
/// <summary>Attempts to decode an NSObject associated with the specified key.</summary>
212+
/// <param name="key">The key identifying the item to decode.</param>
213+
/// <param name="result">When this method returns, contains the decoded NSObject if the key exists; otherwise, <see langword="null" />.</param>
214+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
215+
public bool TryDecode (string key, out NSObject? result)
189216
{
190217
if (ContainsKey (key)) {
191218
result = DecodeObject (key);
@@ -195,7 +222,11 @@ public bool TryDecode (string key, out NSObject result)
195222
return false;
196223
}
197224

198-
public bool TryDecode (string key, out byte [] result)
225+
/// <summary>Attempts to decode a byte array associated with the specified key.</summary>
226+
/// <param name="key">The key identifying the item to decode.</param>
227+
/// <param name="result">When this method returns, contains the decoded byte array if the key exists; otherwise, <see langword="null" />.</param>
228+
/// <returns><see langword="true" /> if the key exists and the value was decoded; otherwise, <see langword="false" />.</returns>
229+
public bool TryDecode (string key, out byte []? result)
199230
{
200231
if (ContainsKey (key)) {
201232
result = DecodeBytes (key);
@@ -205,24 +236,26 @@ public bool TryDecode (string key, out byte [] result)
205236
return false;
206237
}
207238

208-
[SupportedOSPlatform ("ios")]
209-
[SupportedOSPlatform ("macos")]
210-
[SupportedOSPlatform ("maccatalyst")]
211-
[SupportedOSPlatform ("tvos")]
212-
public NSObject DecodeTopLevelObject (Type type, string key, out NSError error)
239+
/// <summary>Decodes a top-level object of the specified type associated with the specified key.</summary>
240+
/// <param name="type">The type of the object to decode.</param>
241+
/// <param name="key">The key identifying the item to decode.</param>
242+
/// <param name="error">When this method returns, contains an error object if decoding failed; otherwise, <see langword="null" />.</param>
243+
/// <returns>The decoded object, or <see langword="null" /> if decoding failed.</returns>
244+
public NSObject? DecodeTopLevelObject (Type type, string key, out NSError? error)
213245
{
214246
if (type is null)
215247
throw new ArgumentNullException (nameof (type));
216248
return DecodeTopLevelObject (new Class (type), key, out error);
217249
}
218250

219-
[SupportedOSPlatform ("ios")]
220-
[SupportedOSPlatform ("macos")]
221-
[SupportedOSPlatform ("maccatalyst")]
222-
[SupportedOSPlatform ("tvos")]
223-
public NSObject DecodeTopLevelObject (Type [] types, string key, out NSError error)
251+
/// <summary>Decodes a top-level object of one of the specified types associated with the specified key.</summary>
252+
/// <param name="types">An array of types that the decoded object can be. If <see langword="null" />, any type is allowed.</param>
253+
/// <param name="key">The key identifying the item to decode.</param>
254+
/// <param name="error">When this method returns, contains an error object if decoding failed; otherwise, <see langword="null" />.</param>
255+
/// <returns>The decoded object, or <see langword="null" /> if decoding failed.</returns>
256+
public NSObject? DecodeTopLevelObject (Type []? types, string key, out NSError? error)
224257
{
225-
NSSet<Class> typeSet = null;
258+
NSSet<Class>? typeSet = null;
226259
if (types is not null) {
227260
var classes = new Class [types.Length];
228261
for (int i = 0; i < types.Length; i++)

tests/cecil-tests/Documentation.KnownFailures.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11807,16 +11807,6 @@ M:Foundation.NSCache.SetObjectForKey(Foundation.NSObject,Foundation.NSObject)
1180711807
M:Foundation.NSCalendar.#ctor(Foundation.NSCalendarType)
1180811808
M:Foundation.NSCoder.DecodeObject(System.Type,System.String)
1180911809
M:Foundation.NSCoder.DecodeObject(System.Type[],System.String)
11810-
M:Foundation.NSCoder.DecodeTopLevelObject(System.Type,System.String,Foundation.NSError@)
11811-
M:Foundation.NSCoder.DecodeTopLevelObject(System.Type[],System.String,Foundation.NSError@)
11812-
M:Foundation.NSCoder.TryDecode(System.String,Foundation.NSObject@)
11813-
M:Foundation.NSCoder.TryDecode(System.String,System.Boolean@)
11814-
M:Foundation.NSCoder.TryDecode(System.String,System.Byte[]@)
11815-
M:Foundation.NSCoder.TryDecode(System.String,System.Double@)
11816-
M:Foundation.NSCoder.TryDecode(System.String,System.Int32@)
11817-
M:Foundation.NSCoder.TryDecode(System.String,System.Int64@)
11818-
M:Foundation.NSCoder.TryDecode(System.String,System.IntPtr@)
11819-
M:Foundation.NSCoder.TryDecode(System.String,System.Single@)
1182011810
M:Foundation.NSConnection.Dispose(System.Boolean)
1182111811
M:Foundation.NSDataDetector.#ctor(Foundation.NSTextCheckingType,Foundation.NSError@)
1182211812
M:Foundation.NSDataDetector.Create(Foundation.NSTextCheckingType,Foundation.NSError@)

0 commit comments

Comments
 (0)