System.ExceptionCould not initialize an instance of the type 'Foundation.NSAttributedString':

HrishikeshPolHrishikeshPol ✭✭USMember ✭✭

Hi,

I'm getting below exception in my production build only. Don't know what is the root cause of it as it is working fine for me my end with debug and release mode.

System.ExceptionCould not initialize an instance of the type 'Foundation.NSAttributedString': the native 'initWithData:options:documentAttributes:error:' method returned nil. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

Can anyone help me with this.

Thanks,
Hrishikesh Pol.

Tagged:

Posts

  • geoffsgeoffs ✭✭ USUniversity ✭✭

    Can confirm we're seeing the same issue, and only in release/appstore builds

  • HrishikeshPolHrishikeshPol ✭✭ USMember ✭✭

    Yes it was happening in production build only. Below is my code.

                NSAttributedString attributedString = new NSAttributedString (myHTMLText , attr, ref nsError);
    
  • braderbrader ✭✭ USMember ✭✭

    I also started having this issue very recently and haven't been able to reproduce it while debugging. Here's my code:

    NSData htmlData = NSData.FromString(html, NSStringEncoding.Unicode);
    NSAttributedStringDocumentAttributes documentAttributes =
        new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.HTML };
    NSError nsError = null;
    var htmlAttributedText = new NSAttributedString(htmlData, documentAttributes, ref nsError); // Exception thrown here
    

    I had a few theories that I tested in debug mode, none of them caused the exception to be thrown.

    1. html is an empty string, which causes htmlData to be null. This is not the case, htmlData will still have a value and no exception was thrown.
    2. html is invalid HTML, which causes the initializer to fail. I tried <span>test<\spa to check if a tag didn't get closed. No exception and the word "test" even displayed in some ugly serif-ed text.
    3. html is null. This properly throws an ArgumentNullException when calling NSData.FromString(html, NSStringEncoding.Unicode).
  • XasanrahXasanrah ✭✭ CZMember ✭✭

    Getting same exception in production build. Can someone advise any solution

  • SongtiveSongtive ✭✭ LVMember ✭✭

    I've noticed the same problem. Did you find the solution?

    My code is:

    var err = new NSError();
    var atts = new NSAttributedStringDocumentAttributes
    {
       DocumentType = NSDocumentType.HTML,
       StringEncoding = NSStringEncoding.UTF8
    };
    
    var attStr = new NSAttributedString(NSData.FromString(textString), atts, ref err);
    if (err != null) throw new Exception(err.ToString());
    return attStr;
    

    And the exception for textString = "" (or any other text):

    The operation couldn’t be completed. (Invalid .ctor used error 0.)}
    
Sign In or Register to comment.