Skip to content

Commit 31dd037

Browse files
authored
[Foundation] Fix/improve a few more NSAttributedString constructors. (#21803)
Managed constructors can't fail gracefully, which means that constructors with an "out NSError" parameter doesn't make much sense. Instead bind these constructors using a factory method. References: * #16804 (comment) * #21727
1 parent 07ac4a3 commit 31dd037

File tree

5 files changed

+213
-19
lines changed

5 files changed

+213
-19
lines changed

src/Foundation/NSAttributedString.cs

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#nullable enable
2929

3030
using System;
31+
using System.ComponentModel;
32+
3133
using CoreFoundation;
3234
using CoreText;
3335
using ObjCRuntime;
@@ -46,7 +48,7 @@ public partial class NSAttributedString {
4648
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
4749
/// <param name="resultDocumentAttributes">Upon return, a dictionary of document-specific keys.</param>
4850
/// <param name="error">The error if an error occurred.</param>
49-
public static NSAttributedString? Create (NSUrl url, NSDictionary options, out NSDictionary resultDocumentAttributes, out NSError error)
51+
public static NSAttributedString? Create (NSUrl url, NSDictionary options, out NSDictionary resultDocumentAttributes, out NSError? error)
5052
{
5153
var rv = new NSAttributedString (NSObjectFlag.Empty);
5254
rv.InitializeHandle (rv._InitWithUrl (url, options, out resultDocumentAttributes, out error), string.Empty, false);
@@ -62,17 +64,34 @@ public partial class NSAttributedString {
6264
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
6365
/// <param name="resultDocumentAttributes">Upon return, a dictionary of document-specific keys.</param>
6466
/// <param name="error">The error if an error occurred.</param>
65-
public static NSAttributedString? Create (NSUrl url, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, out NSError error)
67+
public static NSAttributedString? Create (NSUrl url, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, out NSError? error)
6668
{
6769
return Create (url, options.Dictionary, out resultDocumentAttributes, out error);
6870
}
6971

72+
/// <summary>Create a new <see cref="NSAttributedString" />.</summary>
73+
/// <param name="url">A url to the document to load.</param>
74+
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
75+
/// <param name="error">The error if an error occurred.</param>
76+
public static NSAttributedString? Create (NSUrl url, NSAttributedStringDocumentAttributes options, out NSError? error)
77+
{
78+
return Create (url, options.Dictionary, out var _, out error);
79+
}
80+
81+
/// <summary>Create a new <see cref="NSAttributedString" />.</summary>
82+
/// <param name="url">A url to the document to load.</param>
83+
/// <param name="error">The error if an error occurred.</param>
84+
public static NSAttributedString? Create (NSUrl url, out NSError? error)
85+
{
86+
return Create (url, new NSDictionary (), out var _, out error);
87+
}
88+
7089
/// <summary>Create a new <see cref="NSAttributedString" />.</summary>
7190
/// <param name="data">The data to load.</param>
7291
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
7392
/// <param name="resultDocumentAttributes">Upon return, a dictionary of document-specific keys.</param>
7493
/// <param name="error">The error if an error occurred.</param>
75-
public static NSAttributedString? Create (NSData data, NSDictionary options, out NSDictionary resultDocumentAttributes, out NSError error)
94+
public static NSAttributedString? Create (NSData data, NSDictionary options, out NSDictionary resultDocumentAttributes, out NSError? error)
7695
{
7796
var rv = new NSAttributedString (NSObjectFlag.Empty);
7897
rv.InitializeHandle (rv._InitWithData (data, options, out resultDocumentAttributes, out error), string.Empty, false);
@@ -88,33 +107,114 @@ public partial class NSAttributedString {
88107
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
89108
/// <param name="resultDocumentAttributes">Upon return, a dictionary of document-specific keys.</param>
90109
/// <param name="error">The error if an error occurred.</param>
91-
public static NSAttributedString? Create (NSData data, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, out NSError error)
110+
public static NSAttributedString? Create (NSData data, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, out NSError? error)
92111
{
93112
return Create (data, options.Dictionary, out resultDocumentAttributes, out error);
94113
}
95114

115+
/// <summary>Create a new <see cref="NSAttributedString" />.</summary>
116+
/// <param name="data">The data to load.</param>
117+
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
118+
/// <param name="error">The error if an error occurred.</param>
119+
public static NSAttributedString? Create (NSData data, NSAttributedStringDocumentAttributes options, out NSError? error)
120+
{
121+
return Create (data, options.Dictionary, out var _, out error);
122+
}
123+
124+
/// <summary>Create a new <see cref="NSAttributedString" />.</summary>
125+
/// <param name="data">The data to load.</param>
126+
/// <param name="error">The error if an error occurred.</param>
127+
public static NSAttributedString? Create (NSData data, out NSError? error)
128+
{
129+
return Create (data, new NSDictionary (), out var _, out error);
130+
}
131+
132+
/// <summary>Create a new <see cref="NSAttributedString" /> from a markdown file.</summary>
133+
/// <param name="markdownFile">The url of the file to load.</param>
134+
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
135+
/// <param name="baseUrl">The base url to use when resolving markdown urls.</param>
136+
/// <param name="error">The error if an error occurred.</param>
137+
public static NSAttributedString? Create (NSUrl markdownFile, NSAttributedStringMarkdownParsingOptions? options, NSUrl? baseUrl, out NSError? error)
138+
{
139+
var rv = new NSAttributedString (NSObjectFlag.Empty);
140+
rv.InitializeHandle (rv._InitWithContentsOfMarkdownFile (markdownFile, options, baseUrl, out error), string.Empty, false);
141+
if (rv.Handle == IntPtr.Zero) {
142+
rv.Dispose ();
143+
return null;
144+
}
145+
return rv;
146+
}
147+
148+
/// <summary>Create a new <see cref="NSAttributedString" /> from markdown data.</summary>
149+
/// <param name="markdown">The markdown data to load.</param>
150+
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
151+
/// <param name="baseUrl">The base url to use when resolving markdown urls.</param>
152+
/// <param name="error">The error if an error occurred.</param>
153+
public static NSAttributedString? Create (NSData markdown, NSAttributedStringMarkdownParsingOptions? options, NSUrl? baseUrl, out NSError? error)
154+
{
155+
var rv = new NSAttributedString (NSObjectFlag.Empty);
156+
rv.InitializeHandle (rv._InitWithMarkdown (markdown, options, baseUrl, out error), string.Empty, false);
157+
if (rv.Handle == IntPtr.Zero) {
158+
rv.Dispose ();
159+
return null;
160+
}
161+
return rv;
162+
}
163+
164+
/// <summary>Create a new <see cref="NSAttributedString" /> from a string with markdown.</summary>
165+
/// <param name="markdownString">The markdown string to load.</param>
166+
/// <param name="options">A dictionary of attributes that specifies how to interpret the document contents.</param>
167+
/// <param name="baseUrl">The base url to use when resolving markdown urls.</param>
168+
/// <param name="error">The error if an error occurred.</param>
169+
public static NSAttributedString? Create (string markdownString, NSAttributedStringMarkdownParsingOptions? options, NSUrl? baseUrl, out NSError? error)
170+
{
171+
var rv = new NSAttributedString (NSObjectFlag.Empty);
172+
rv.InitializeHandle (rv._InitWithMarkdownString (markdownString, options, baseUrl, out error), string.Empty, false);
173+
if (rv.Handle == IntPtr.Zero) {
174+
rv.Dispose ();
175+
return null;
176+
}
177+
return rv;
178+
}
179+
96180
#if __MACOS__ || XAMCORE_5_0
181+
[EditorBrowsable (EditorBrowsableState.Never)]
182+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
97183
public NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes documentAttributes, out NSError error)
98184
: this (url, documentAttributes, out var _, out error) {}
99185

186+
[EditorBrowsable (EditorBrowsableState.Never)]
187+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
100188
public NSAttributedString (NSData data, NSAttributedStringDocumentAttributes documentAttributes, out NSError error)
101189
: this (data, documentAttributes, out var _, out error) {}
102190

191+
[EditorBrowsable (EditorBrowsableState.Never)]
192+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
103193
public NSAttributedString (NSUrl url, out NSError error)
104194
: this (url, new NSDictionary (), out var _, out error) {}
105195

196+
[EditorBrowsable (EditorBrowsableState.Never)]
197+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
106198
public NSAttributedString (NSData data, out NSError error)
107199
: this (data, new NSDictionary (), out var _, out error) {}
108200
#else
201+
[EditorBrowsable (EditorBrowsableState.Never)]
202+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
109203
public NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes documentAttributes, ref NSError error)
110204
: this (url, documentAttributes, out var _, ref error) { }
111205

206+
[EditorBrowsable (EditorBrowsableState.Never)]
207+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
112208
public NSAttributedString (NSData data, NSAttributedStringDocumentAttributes documentAttributes, ref NSError error)
113209
: this (data, documentAttributes, out var _, ref error) { }
114210

211+
[EditorBrowsable (EditorBrowsableState.Never)]
212+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
115213
public NSAttributedString (NSUrl url, ref NSError error)
116214
: this (url, new NSDictionary (), out var _, ref error) { }
117215

216+
[EditorBrowsable (EditorBrowsableState.Never)]
217+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
118218
public NSAttributedString (NSData data, ref NSError error)
119219
: this (data, new NSDictionary (), out var _, ref error) { }
120220
#endif

src/foundation.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,17 +788,44 @@ partial interface NSAttributedString : NSCoding, NSMutableCopying, NSSecureCodin
788788
[Wrap ("LoadFromHtml (data, options.GetDictionary ()!, completionHandler)")]
789789
void LoadFromHtml (NSData data, NSAttributedStringDocumentAttributes options, NSAttributedStringCompletionHandler completionHandler);
790790

791+
#if !XAMCORE_5_0
792+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
791793
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
792794
[Export ("initWithContentsOfMarkdownFileAtURL:options:baseURL:error:")]
793795
NativeHandle Constructor (NSUrl markdownFile, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
796+
#endif
797+
798+
[Internal]
799+
[Sealed]
800+
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
801+
[Export ("initWithContentsOfMarkdownFileAtURL:options:baseURL:error:")]
802+
NativeHandle _InitWithContentsOfMarkdownFile (NSUrl markdownFile, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
794803

804+
#if !XAMCORE_5_0
805+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
795806
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
796807
[Export ("initWithMarkdown:options:baseURL:error:")]
797808
NativeHandle Constructor (NSData markdown, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
809+
#endif
810+
811+
[Internal]
812+
[Sealed]
813+
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
814+
[Export ("initWithMarkdown:options:baseURL:error:")]
815+
NativeHandle _InitWithMarkdown (NSData markdown, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
798816

817+
#if !XAMCORE_5_0
818+
[Obsolete ("Use the 'Create' method instead, because there's no way to return an error from a constructor.")]
799819
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
800820
[Export ("initWithMarkdownString:options:baseURL:error:")]
801821
NativeHandle Constructor (string markdownString, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
822+
#endif
823+
824+
[Internal]
825+
[Sealed]
826+
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
827+
[Export ("initWithMarkdownString:options:baseURL:error:")]
828+
NativeHandle _InitWithMarkdownString (string markdownString, [NullAllowed] NSAttributedStringMarkdownParsingOptions options, [NullAllowed] NSUrl baseUrl, [NullAllowed] out NSError error);
802829

803830
[Watch (8, 0), TV (15, 0), iOS (15, 0), MacCatalyst (15, 0)]
804831
[Export ("attributedStringByInflectingString")]

tests/cecil-tests/ConstructorTest.KnownFailures.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ public partial class ConstructorTest {
122122
"CoreML.MLMultiArray::.ctor(System.IntPtr,Foundation.NSNumber[],CoreML.MLMultiArrayDataType,Foundation.NSNumber[],System.Action`1<System.IntPtr>,Foundation.NSError&)",
123123
"CoreML.MLMultiArray::.ctor(System.IntPtr,System.IntPtr[],CoreML.MLMultiArrayDataType,System.IntPtr[],System.Action`1<System.IntPtr>,Foundation.NSError&)",
124124
"CoreML.MLMultiArray::.ctor(System.IntPtr[],CoreML.MLMultiArrayDataType,Foundation.NSError&)",
125-
"Foundation.NSAttributedString::.ctor(Foundation.NSData,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSError&)",
126-
"Foundation.NSAttributedString::.ctor(Foundation.NSData,Foundation.NSAttributedStringMarkdownParsingOptions,Foundation.NSUrl,Foundation.NSError&)",
127-
"Foundation.NSAttributedString::.ctor(Foundation.NSData,Foundation.NSError&)",
128-
"Foundation.NSAttributedString::.ctor(Foundation.NSUrl,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSError&)",
129-
"Foundation.NSAttributedString::.ctor(Foundation.NSUrl,Foundation.NSAttributedStringMarkdownParsingOptions,Foundation.NSUrl,Foundation.NSError&)",
130-
"Foundation.NSAttributedString::.ctor(Foundation.NSUrl,Foundation.NSError&)",
131-
"Foundation.NSAttributedString::.ctor(System.String,Foundation.NSAttributedStringMarkdownParsingOptions,Foundation.NSUrl,Foundation.NSError&)",
132125
"Foundation.NSDataDetector::.ctor(Foundation.NSTextCheckingType,Foundation.NSError&)",
133126
"Foundation.NSDataDetector::.ctor(Foundation.NSTextCheckingTypes,Foundation.NSError&)",
134127
"Foundation.NSDictionary::.ctor(Foundation.NSUrl,Foundation.NSError&)",

tests/cecil-tests/Documentation.KnownFailures.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33100,15 +33100,11 @@ M:Foundation.NSArray`1.FromNSObjects(`0[])
3310033100
M:Foundation.NSArray`1.FromNSObjects(System.Int32,`0[])
3310133101
M:Foundation.NSArray`1.ToArray
3310233102
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSDictionary@)
33103-
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSError@)
3310433103
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSDictionary,Foundation.NSDictionary@)
3310533104
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSDictionary@)
33106-
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSError@)
3310733105
M:Foundation.NSAttributedString.#ctor(Foundation.NSData,Foundation.NSUrl,Foundation.NSDictionary@)
3310833106
M:Foundation.NSAttributedString.#ctor(Foundation.NSFileWrapper,Foundation.NSDictionary@)
33109-
M:Foundation.NSAttributedString.#ctor(Foundation.NSUrl,Foundation.NSAttributedStringDocumentAttributes,Foundation.NSError@)
3311033107
M:Foundation.NSAttributedString.#ctor(Foundation.NSUrl,Foundation.NSDictionary@)
33111-
M:Foundation.NSAttributedString.#ctor(Foundation.NSUrl,Foundation.NSError@)
3311233108
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)
3311333109
M:Foundation.NSAttributedString.#ctor(System.String,AppKit.NSStringAttributes)
3311433110
M:Foundation.NSAttributedString.#ctor(System.String,CoreText.CTStringAttributes)

0 commit comments

Comments
 (0)