2

Apple宣布通过 ARKit 3 (iOS 13)ARPlaneAnchor.Classification获得两个新值:

在此处输入图像描述

我的项目针对 iOS 12.1。

我的应用程序中有以下代码:

extension ARPlaneAnchor.Classification {
    var description: String {
        switch self {
        case .wall:
            return "Wall"
        case .floor:
            return "Floor"
        case .ceiling:
            return "Ceiling"
        case .table:
            return "Table"
        case .seat:
            return "Seat"
        case .window:
            return "Window"
        case .door:
            return "Door"
        case .none(.unknown):
            return "Unknown"
        default:
            return ""
        }
    }
}

当我尝试在运行 iOS 13 的 iPhone X 上运行它时,它运行得很好,并执行windowdoor.

当我尝试在运行 iOS 12.4 的 iPhone 8 上运行它时,它会在启动时崩溃:

dyld:找不到符号:_$sSo13ARPlaneAnchorC5ARKitE14ClassificationO4dooryA2EmFWC
引用自:/var/containers/Bundle/Application/...
预期在:/usr/lib/swift/libswiftARKit.dylib
在 /var/containers/Bundle/Application/...

搜索关键词: ARPlaneAnchor ARKit 分类门

如果我注释掉它case doorcase window它运行得很好。

这显然是一个错误,因为枚举案例doorwindow适用于 iOS 13。

当我命令单击定义时,我看到 Apple 将其定义为:

/**
 A value describing the classification of a plane anchor.
 */
@available(iOS 12.0, *)
public enum __ARPlaneClassification : Int {


    /** The classification is not any of the known classes. */
    case none

    /** The classification is not any of the known classes. */
    case wall

    /** The classification is not any of the known classes. */
    case floor

    /** The classification is not any of the known classes. */
    case ceiling

    /** The classification is not any of the known classes. */
    case table

    /** The classification is not any of the known classes. */
    case seat

    /** The classification is not any of the known classes. */
    case window

    /** The classification is not any of the known classes. */
    case door
}

或者有时看起来像这样:

@available(iOS 12.0, *)
extension ARPlaneAnchor {

    public enum Classification {

        // ...

        /** The classification is not any of the known classes. */
        case none(ARPlaneAnchor.Classification.Status)

        case wall

        case floor

        case ceiling

        case table

        case seat

        case window

        case door
    }

    public var classification: ARPlaneAnchor.Classification { get }
}

正如您在两个示例中看到的,新的 iOS 13 枚举(例如door)与旧的 iOS 12 枚举(例如 )完全相同wall。我相信这就是导致它编译得很好但在启动时崩溃的原因。

如何让代码在运行 iOS 13 和运行 iOS 12 的设备上运行良好?


更新:我尝试使用可用性检查,例如:

extension ARPlaneAnchor.Classification {
    var description: String {
        if #available(iOS 13, *) {
            return description_13()
        } else {
            return description_12()
        }
    }

    @available(iOS 12, *)
    private func description_12() -> String {
        switch self {
        case .wall:
            return "Wall"
        case .floor:
            return "Floor"
        case .ceiling:
            return "Ceiling"
        case .table:
            return "Table"
        case .seat:
            return "Seat"
        case .none(.unknown):
            return "Unknown"
        default:
            return ""
        }
    }

    @available(iOS 13, *)
    private func description_13() -> String {
        switch self {
        case .window:
            return "Window"
        case .door:
            return "Door"
        default:
            return description_12()
        }
    }
}

但是,这会产生与以前相同的“未找到符号”错误。关于如何让它工作的任何建议?

4

1 回答 1

0

我面临着同样的问题,最终我进行了反思。不太好,但似乎运作良好。

extension ARPlaneAnchor.Classification {
    var description: String {
        switch self {
        case .wall:
            return "Wall"
        case .floor:
            return "Floor"
        case .ceiling:
            return "Ceiling"
        case .table:
            return "Table"
        case .seat:
            return "Seat"
        case .none(.unknown):
            return "Unknown"
        default:
          return description_13 ?? ""
        }
    }

    private var description_13: String? {
        let reflection = String(reflecting: self)
        let components = reflection.components(separatedBy: ".")
        switch components.last {
        case "window":
            return "Window"
        case "door":
            return "Door"
        default:
            return nil
        }
    }
}
于 2021-12-30T13:25:59.737 回答