Bug 60707 - We throw "the native method returned nil" when ObjC and Swift simply log the actual solution to the issue
Summary: We throw "the native method returned nil" when ObjC and Swift simply log the ...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: master
Hardware: PC Mac OS
: Normal enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2017-11-16 00:47 UTC by Vincent Dondain [MSFT]
Modified: 2017-11-16 15:41 UTC (History)
4 users (show)

Is this bug a regression?: No
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 60707 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Vincent Dondain [MSFT] 2017-11-16 00:47:11 UTC
var a = new CIQRCodeDescriptor (new NSData (), 0, 0, CIQRCodeErrorCorrectionLevel.Q);

This C# API throws: `Could not initialize an instance of the type 'CoreImage.CIQRCodeDescriptor': the native 'initWithPayload:symbolVersion:maskPattern:errorCorrectionLevel:' method returned nil.`

The same code in native not only doesn't throw but also tells the user the exact problem and solution.

CIQRCodeDescriptor * desc = [[CIQRCodeDescriptor alloc] initWithPayload:[[NSData alloc] init] symbolVersion:0 maskPattern:0 errorCorrectionLevel:CIQRCodeErrorCorrectionLevelQ];

let code = CIQRCodeDescriptor (payload: Data (), symbolVersion: 0, maskPattern: 0, errorCorrectionLevel: .levelQ)

In the logs we get: `[api] -[CIQRCodeDescriptor isValid] CIQRCodeDescriptor: symbolVersion must be in the range of [1,40]`

This is nowhere to be found when using the managed API and I'm wondering if it's just a case where we can't do anything about it, have to use the API and add a validation check to our code or if we can somehow leverage the native behavior and report that automatically to our users.
Comment 1 Alex Soto [MSFT] 2017-11-16 00:55:15 UTC
Could be that we do not support os_log and friends[1]? I might be wrong :) I think @Rolf mentioned something about it last year but could not find it :) 

[1]: https://developer.apple.com/documentation/os/logging?language=objc
Comment 2 Alex Soto [MSFT] 2017-11-16 00:55:50 UTC
cc @Rolf :D
Comment 3 Sebastien Pouliot 2017-11-16 01:31:39 UTC
Apple API are inconsistent. Some `init*` method can return an `out NSError` argument, that would contain such information (and we bind them as such).

However most of `init*` returns `nil` without any extra information. Some will assert, which crash the app (much worse). Some will throw an ObjC exception (tricky). Finally a few will log something - and that should remain the case when called from C#.

In some cases it's possible to add the same validation in managed code (and throw a more accurate exception). However if the validation is not documented then it's risky to be over protective (and disallow acceptable data) in addition to adding extra code into the application.

We could add a more elaborate message (or an URL, the existing message is already long) that describe the issue and suggest looking into the logs for, possible, extra information.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2017-11-16 15:41:46 UTC
The native method prints the message to stderr, which should show up in the Application Output in the IDE (I tested this by closing stderr in an Xcode project ("close (2);"), and the message went away).