【问题标题】:INIntent `setImage` make runtime crash in Swift 5INIntent `setImage` 使 Swift 5 中的运行时崩溃
【发布时间】:2019-09-08 05:32:16
【问题描述】:

自从添加了 Siri 快捷方式后,我一直使用 INIntent 对象。为此,我做了一个意图定义,它自动生成了一个INIntent 对象。

@available(iOS 12.0, watchOS 5.0, *)
@objc(SpotConditionIntent)
public class SpotConditionIntent: INIntent {

    @NSManaged public var spotId: String?
    @NSManaged public var spotName: String?

}

为了自定义 Siri 快捷语音记录屏幕,我添加了一个便利初始化程序。基本上就是加了suggestedInvocationPhrase和顶部的icon图片。

@available(iOS 12, *)
extension SpotConditionIntent {
    convenience init(spotId: String, spotName: String) {
        self.init()
        self.spotId = spotId
        self.spotName = spotName
        self.suggestedInvocationPhrase = "\(NSLocalizedString("how_are_waves_text", comment: "How are the waves at")) \(spotName)?"
        if let uiimage = UIImage(named: "check-surf-icon"), let data = uiimage.pngData() {
            let inImage = INImage(imageData: data)
            setImage(inImage, forParameterNamed: \.spotName)
        }
    }
}

今天,我尝试将整个项目转换为 Swift 5,构建没有问题。 (代码没有实际变化。)但是它在运行时崩溃并显示非常奇怪的消息。

线程 1:致命错误:无法从 XXXX9SpotConditionIntentCXD 解开关键路径类型

它指向setImage(inImage, forParameterNamed: \.spotName)

我刚刚发现文档中不存在setImage(,forParameterNamed)

https://developer.apple.com/documentation/sirikit/inintent

看来我需要使用 iOS 12 中添加的func keyImage() -> INImage?

但我不知道为什么它在 Swift 4.X 中有效,并且找不到任何弃用文档。有人知道这个问题吗?

【问题讨论】:

    标签: ios swift sirishortcuts swift5 swift-keypath


    【解决方案1】:

    据我所知...

    Objective-C 中提供了这两种方法。

    - imageForParameterNamed:

    - setImage:forParameterNamed:

    这些方法生成的接口显示为...

    // Set an image associated with a parameter on the receiver. This image will be used in display of the receiver throughout the system.
    @available(iOS 12.0, *)
    open func __setImage(_ image: INImage?, forParameterNamed parameterName: String)
    
    @available(iOS 12.0, *)
    open func __image(forParameterNamed parameterName: String) -> INImage?
    

    Xcode 的文档或代码建议不会向您显示这些,但您可以在 Swift 5 代码中使用它们:

        intent.__setImage(image, forParameterNamed: "spotName")
    

    不过,在将应用提交到 App Store 时,使用下划线开头的方法可能会被视为使用私有 API。

    当Apple swiftifying某些方法尚未完成时,有时会发生这种事情。

    至于现在,你能做的是……

    • 立即向 Apple 发送错误报告

    • 为这些方法编写一个 Objective-C 包装器并将其导入 Swift

    • 延迟提交直到新的 SDK 解决了这个问题 (可能是几年...)

    【讨论】:

    • 超级有趣。在iOS12 beta的某个版本中是__setImage,但Apple在黄金版中将其改为setImage。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2019-09-16
    • 2013-09-14
    • 2020-01-02
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多