Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Foundation] Unify a few NSAttributedString constructor implementations. #16804

Merged

Conversation

rolfbjarne
Copy link
Member

Unify the code for the following constructors:

  • NSAttributedString (NSData data, NSDictionary options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
  • NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
  • NSAttributedString (NSData data, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);

These functions use 'ref' arguments instead of 'out' arguments for mobile
platforms (likely due to the generator not having proper 'out' parameter
support when these functions were implemented), so improve to use 'out'
parameters in XAMCORE_5_0 (and macOS, where they already use 'out'
parameters).

Also fix nullability.

Ref: #15216

Unify the code for the following constructors:

+ NSAttributedString (NSData data, NSDictionary options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
+ NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
+ NSAttributedString (NSData data, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);

These functions use 'ref' arguments instead of 'out' arguments for mobile
platforms (likely due to the generator not having proper 'out' parameter
support when these functions were implemented), so improve to use 'out'
parameters in XAMCORE_5_0 (and macOS, where they already use 'out'
parameters).

Also fix nullability.

Ref: xamarin#15216
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne changed the title [Foundation] Unify a NSAttributedString few constructor implementations. [Foundation] Unify a few NSAttributedString constructor implementations. Nov 18, 2022
@rolfbjarne
Copy link
Member Author

Test failure is unrelated (https://github.com/xamarin/maccore/issues/2632).

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

❗ API diff vs stable (Breaking changes)

Legacy Xamarin (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
.NET (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
Legacy Xamarin (stable) vs .NET

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 2a49e315643fac5d1577fdc2559418484101ff1f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Artifacts were not provided.

Pipeline on Agent XAMBOT-1104.Monterey'
Hash: 2a49e315643fac5d1577fdc2559418484101ff1f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: 2a49e315643fac5d1577fdc2559418484101ff1f [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build] Test results 🔥

Test results

❌ Tests failed on VSTS: simulator tests

0 tests crashed, 5 tests failed, 218 tests passed.

Failures

❌ bcl tests

5 tests failed, 64 tests passed.
  • [NUnit] Mono Mac OS X BCL tests group 3/Mac Full/Debug: Failed (Test run failed.
    Tests run: 11328 Passed: 10552 Inconclusive: 0 Failed: 1 Ignored: 98)
  • [NUnit] Mono Mac OS X BCL tests group 3/Mac Modern/Debug: Failed (Test run failed.
    Tests run: 8755 Passed: 8174 Inconclusive: 0 Failed: 1 Ignored: 46)
  • [NUnit] Mono BCL tests group 2/iOS Unified 64-bits - simulator/Debug: Failed
  • [NUnit] Mono BCL tests group 2/tvOS - simulator/Debug: Failed
  • [NUnit] Mono CorlibTests/watchOS 32-bits - simulator/Debug: Failed

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 8 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 7 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 23 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 2a49e315643fac5d1577fdc2559418484101ff1f [PR build]

@rolfbjarne
Copy link
Member Author

Test failures are unrelated (https://github.com/xamarin/maccore/issues/2629).

@rolfbjarne rolfbjarne merged commit c1606d5 into xamarin:main Nov 21, 2022
@rolfbjarne rolfbjarne deleted the nsattributed-string-ctor-unification-1 branch November 21, 2022 20:05
haritha-mohan pushed a commit to haritha-mohan/xamarin-macios that referenced this pull request Nov 22, 2022
…ns. (xamarin#16804)

Unify the code for the following constructors:

* NSAttributedString (NSData data, NSDictionary options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
* NSAttributedString (NSUrl url, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);
* NSAttributedString (NSData data, NSAttributedStringDocumentAttributes options, out NSDictionary resultDocumentAttributes, ref/out NSError error);

These functions use 'ref' arguments instead of 'out' arguments for mobile
platforms (likely due to the generator not having proper 'out' parameter
support when these functions were implemented), so improve to use 'out'
parameters in XAMCORE_5_0 (and macOS, where they already use 'out'
parameters).

Also fix nullability.

Ref: xamarin#15216
@tipa
Copy link

tipa commented Nov 26, 2024

@rolfbjarne can these constructors ever return a non-null NSError?
When I provide an invalid URL it throws an exception instead of returning null (what, from my understanding, is possible in the Apple-world, but not in .NET):

var s = new NSAttributedString(new NSUrl(""), new NSAttributedStringDocumentAttributes(), out NSError e);

or

var attributes = new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.RTF, };
var s = new NSMutableAttributedString(new NSAttributedString("", attributes, out NSError _));

System.Exception: Could not initialize an instance of the type 'Foundation.NSAttributedString': the native 'initWithURL:options:documentAttributes:error:' method returned nil.

What is the correct way to use these constructors? Just wrap them in try-catch instead of inspecting the error variable?
Perhaps this issue is related?

rolfbjarne added a commit that referenced this pull request Nov 29, 2024
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.

Ref: #16804 (comment)
@rolfbjarne
Copy link
Member Author

@tipa

What is the correct way to use these constructors? Just wrap them in try-catch instead of inspecting the error variable?

This is rather unfortunate, these constructors should really be bound as factory methods instead. For now, I guess the best is to wrap them in a try-catch, but then you won't be able to inspect the error variable.

I've created a PR to create factory methods for them instead: #21727

Perhaps this issue is related?

That's a fairly generic issue saying there are problems with the NSAttributedString ctors, which is true enough.

rolfbjarne added a commit that referenced this pull request Dec 2, 2024
… (#21727)

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.

Ref: #16804 (comment)

Fixes #14489.
rolfbjarne added a commit that referenced this pull request Dec 12, 2024
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

Missing:

* Manual tests.
rolfbjarne added a commit that referenced this pull request Dec 30, 2024
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants