Bug 25438 - NSString.FromData incorrect behaviour
Summary: NSString.FromData incorrect behaviour
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: master
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Paola Villarreal
Depends on:
Reported: 2014-12-16 13:25 UTC by Rustam Zaitov
Modified: 2014-12-22 16:52 UTC (History)
2 users (show)

Is this bug a regression?: ---
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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Rustam Zaitov 2014-12-16 13:25:45 UTC
Internally this method calls NSString ctor. Here is the case where such behaviour is wrong.

* Read binary data (ex from picture.png)
* Pass NSData to FromData method. Exception here ...

From my point of view this is a correct workflow but incorrect result. An obj-c runtime return's nil from initWithData:encoding: and then NSString ctor can't create NSString from nil.

Here exception message:
> Could not initialize an instance of the type 'Foundation.NSString': the native 'initWithData:encoding:' method returned nil. It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.

Expected result:
FromData(NSData, NSStringEncoding) should returns null if NSData is not an actual string.

Comment 1 Sebastien Pouliot 2014-12-16 13:49:14 UTC
@Paola this is similar to the fix you did last week for AVFoundation. Please add a unit test :)
Comment 2 Paola Villarreal 2014-12-22 16:52:29 UTC
Fixed on maccore: 5e8de4de57a6cbf7eafbcd63d94ff8aee4a9a7f3
Unit test on monotouch: 37128c8258dbace75f068acea3b2a260189f1919