简体   繁体   中英

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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM