Ejemplo n.º 1
0
        static string GetValueForNSObject(IntPtr obj)
        {
            using (var value = ObjCRuntime.Runtime.GetNSObject <NSObject> (obj, true)) {
                if (!value.IsKindOfClass(nsDataClass.Value))
                {
                    return(value.ToString());
                }

                var data = ObjCRuntime.Runtime.GetINativeObject <NSData> (obj, false);
                if (data == null)
                {
                    return(value.ToString());
                }

                nuint detectedEncoding = NSString.DetectStringEncoding(data, new EncodingDetectionOptions {
                    EncodingDetectionAllowLossy = false,
                    EncodingDetectionUseOnlySuggestedEncodings = true,
                    EncodingDetectionSuggestedEncodings        = new NSStringEncoding [] {
                        NSStringEncoding.UTF8,
                        NSStringEncoding.Unicode,
                        NSStringEncoding.ASCIIStringEncoding
                    },
                }, out string convertedString, out bool isLossy);

                if (convertedString == null)
                {
                    return(null);
                }

                if (convertedString.Length > 0 && detectedEncoding != 0 && !isLossy)
                {
                    return(convertedString);
                }

                if (TryGetNumberValueFromNSObject(data, out var numberString))
                {
                    return(numberString);
                }

                return(value.ToString());
            }
        }