NSXPCInterface setClasses在Xcode 14+ Swift @objc枚举上导致“惰性名称处理程序未命名惰性名称类0x 600000 xxxxxx”

xzlaal3s  于 2023-02-15  发布在  Swift
关注(0)|答案(1)|浏览(122)

在升级到Xcode 14之后,我遇到了SIGABRT崩溃“惰性名称类0x600000dc6520不是由惰性名称处理程序命名的”。在最新版本的Xcode 13中,它编译和运行时没有任何缺陷。唯一与最基本的不同之处是,我使用MyAppSharedObjects在XPC服务和主应用程序之间跨越更复杂的对象。除此之外,它看起来不像任何超出教程级别的NSXPCInterface代码给我:

import MyAppScriptSandbox
import MyAppSharedObjects
import Foundation

class ScriptExecutor {
    init?() {
        let incomingClasses = NSSet(array: [
            NSArray.self,
            NSString.self,
            NSValue.self,
            NSNumber.self,
            NSData.self,
            NSDate.self,
            NSNull.self,
            NSURL.self,
            NSUUID.self,
            NSError.self,
            NSDictionary.self,
            ScriptSandboxReply.self,
            AppleScriptError.self,
            AppleScriptErrorType.self
        ]) as Set

        let remoteInterface = NSXPCInterface(with: MyAppScriptSandboxProtocol.self)
        remoteInterface.setClasses( // **** CRASH HAPPENS HERE ****
            incomingClasses,
            for: #selector(MyAppScriptSandboxProtocol.execute(script:withReply:)),
            argumentIndex: 0,
            ofReply: true
        )

import Foundation
import MyAppSharedObjects

@objc
public protocol MyAppScriptSandboxProtocol {
    func execute(
        script: String,
        withReply reply: @escaping (ScriptSandboxReply) -> Void
    )
    func terminate()
}

@objc(ScriptSandboxReply)
public class ScriptSandboxReply: NSObject, NSSecureCoding {
    public static let supportsSecureCoding = true

    public func encode(with coder: NSCoder) {
        // Removed company specific code
    }

    required public init?(coder: NSCoder) {
        // Removed company specific code
    }
}

这个数据类型就是问题所在:

@objc(AppleScriptErrorType)
public enum AppleScriptErrorType: Int {
    case error
    case noResult
    case errorNorResult // This really shouldn't happen IRL

    static let key = "AppleScriptErrorType"
}
w46czmvw

w46czmvw1#

经过一段时间的摸索,我发现了这个问题。我开始注解掉incomingClasses列表中的自定义数据类型,当应用程序在收到第一条消息时崩溃,它在init时停止崩溃。通过一个接一个地取消注解自定义数据类型,我终于找到了罪魁祸首:

@objc(AppleScriptErrorType)
public enum AppleScriptErrorType: Int {
    case error
    case noResult
    case errorNorResult // This really shouldn't happen IRL

    static let key = "AppleScriptErrorType"
}

我不知道为什么这个完全正常的枚举类型在Xcode 14在Xcode 13.4.1中工作时被丢弃后就不能再传输了,我想这可能是苹果的bug。但是我可以继续我的生活,使用rawValue(所以实际上只是int值)从一端发送值,并在接收端重建AppleScriptErrorTypeenum

相关问题