[英]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". 因此,我将文本存储在参数编码设置为“ NSStringEncoding.NonLossyASCII”的NSString创建中。 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. 屏蔽的异常异常:无法初始化类型为Foundation.NSString的实例:本机的“ initWithData:encoding:”方法返回nil。 It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false. 通过将MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure设置为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 在(包装器托管到本地)ObjCRuntime.Messaging:void_obj_msgSend_IntPtr_IntPtr_bool(intptr,intptr,intptr,intptr,bool)在/ Users / builder / data /中的Foundation.NSObject.InvokeOnMainThread(System.Action操作)[0x00007] 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). 不能使用ASCII编码来表达表情符号(它很古老,仅包含128个基本字符)。 For this reason string initialization fails (as suggested by the error message). 因此,字符串初始化失败(错误消息建议)。 Use another encoding ( NSUTF8StringEncoding
should work). 使用其他编码( NSUTF8StringEncoding
应该起作用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.