iphone - CLLocationCooperative2D 空值

标签 iphone xamarin.ios mkmapview

在我的 monodroid 应用程序中,我需要将许多类型的 CLLocation 对象分配给 null 值。我需要它来检测某些特定条件。例如,假设一个函数对 CLLocationCooperative2D 对象执行某些操作,我们希望 if 和异常符合此函数,它返回 null。但是 monotouch 不允许此类型为 null 值。

这是我用于此建议的测试方法:

        private static CLLocationCoordinate2D sampleMethod ()
    {
        try {
            return new CLLocationCoordinate2D ();
        } catch (Exception ex) {
            return null;
        }
    }

和错误一致:

Error CS0037: Cannot convert null to `MonoTouch.CoreLocation.CLLocationCoordinate2D' because it is a value type (CS0037) (Realtyna_iPhone_Project)

在这种情况下我们应该怎么做?

最佳答案

CLLocationCooredinate2D 是值类型,不能为 null。但是,C# 允许您将值类型包装在 Nullable 中。包装器,以便它们可以包含空值。

例如,这应该是有效的:

private CLLocationCoordinate2D? ProcessLocation ()
{
    CLLocationCoordinate2D? retLoc;

    try {
        // do something with retLoc here
    } catch (Exception ex) {
        retLoc = null;
    }

    return retLoc;
}

关于iphone - CLLocationCooperative2D 空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14799478/

相关文章:

ios - 在 setVisibleMapRect 上保留 map View 相机间距(3d 角度)

xamarin.ios - Xamarin.iOS 中的单选按钮听点击?

xamarin.ios - 从哪里可以下载 xamarin studio 5.8?

swift - 如何从 SwiftUI 中的 UIViewRepresentable 外部调用 UIView 上的方法?

iphone - NSDateFormatter - 在某些情况下不尊重 12/24 小时(am/pm)系统设置

c# - 输入显示和隐藏密码

ios - Mkmapview注释仅在平移两次后出现

ios - 如何强制 UIView 的 layoutSubviews?

iphone - 调试 iOS 异常的好方法

android - 手机应用图片