NSString crash when I don't have emoji in my string

242 Views Asked by At

I have a conversational view on my application. I want to display emoji on the good way in this view. So I stock my text on a NSString create with the parameter encoding set to : "NSStringEncoding.NonLossyASCII". It works when I have an emoji on my string. But when there is no emoji on the string, there is a crash with the log :

Exception masked Exception: 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. at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_IntPtr_bool (intptr,intptr,intptr,intptr,bool) at Foundation.NSObject.InvokeOnMainThread (System.Action action) [0x00007] in /Users/builder/data/lanes/3988/e02d2723/source/xamarin-macios/src/Foundation/NSObject2.cs:560

Is it possible to check if there's emoji on a string or not? Thank you

1

There are 1 best solutions below

5
On

There is no way emojis can be expressed using ASCII encoding (it is ancient and contains only 128 essential characters). For this reason string initialization fails (as suggested by the error message). Use another encoding (NSUTF8StringEncoding should work).