KEMBAR78
`NSAttributedString.CreateWithRTF` throws exception instead of returning null · Issue #23584 · dotnet/macios · GitHub
Skip to content

NSAttributedString.CreateWithRTF throws exception instead of returning null #23584

@tipa

Description

@tipa

Apple platform

macOS

Framework version

net9.0-*

Affected platform version

.NET 9

Description

NSAttributedString.CreateWithRTF throws an exception when it cannot initialize the NSAttributedString. According to the documentation it should return null in such a case.
The problem also happens for NSAttributedString.CreateWithRTFD and NSAttributedString.CreateWithDocFormat

System.Exception: Could not initialize an instance of the type 'Foundation.NSAttributedString': the native 'initWithRTF:documentAttributes:' method returned nil.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
   at Foundation.NSObject.InitializeHandle(NativeHandle handle, String initSelector, Boolean throwOnInitFailure) in /Users/builder/azdo/_work/1/s/macios/src/Foundation/NSObject2.cs:line 709
   at Foundation.NSObject.InitializeHandle(NativeHandle handle, String initSelector) in /Users/builder/azdo/_work/1/s/macios/src/Foundation/NSObject2.cs:line 695
   at Foundation.NSAttributedString..ctor(NSData data, NSAttributedStringDataType type, NSDictionary& resultDocumentAttributes) in /Users/builder/azdo/_work/1/s/macios/src/Foundation/NSAttributedString.mac.cs:line 93
   at Foundation.NSAttributedString.CreateWithRTFD(NSData rtfdData, NSDictionary& resultDocumentAttributes) in /Users/builder/azdo/_work/1/s/macios/src/Foundation/NSAttributedString.mac.cs:line 115
   at My.App.WillFinishLaunching(NSNotification notification) in /...AppDelegate.cs:line 149
   at InvokeStub_App.WillFinishLaunching(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

Background: I am using this method to get RTF data from the system pasteboard (NSPasteboard.GeneralPasteboard).

Steps to Reproduce

NSAttributedString.CreateWithRTF(NSData.FromArray([0]), out _);
NSAttributedString.CreateWithRTFD(NSData.FromArray([0]), out _);
NSAttributedString.CreateWithDocFormat(NSData.FromArray([0]), out _);

Did you find any workaround?

using try-catch

Relevant logs

No response

Metadata

Metadata

Assignees

Labels

api-bindingsbugIf an issue is a bug or a pull request a bug fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions