Xcode 15 - 使用 UIKit UIView 预览宏错误

flo*_*hot 5 uikit ios swift xcode-previews xcode15

在我使用公共 Xcode 15.0 的旧代码库中,我无法使用新的#Preview宏预览任何 UIKit 视图

import SwiftUI

#Preview {
    let uiView = UIView()

    return uiView
}
Run Code Online (Sandbox Code Playgroud)

画布无法加载预览并进行故障诊断 Compiling failed: return expression of type 'UIView' does not conform to 'View'

rob*_*off 12

我发现将这一单语句预览限制在 iOS 17 上是有效的:

@available(iOS 17, *)
#Preview {
    return UIView()
}
Run Code Online (Sandbox Code Playgroud)

然而,这个两语句预览仍然不起作用:

@available(iOS 17, *)
#Preview {
    let uiView = UIView()
    return uiView //  Value of optional type 'UIView?' must be unwrapped to a value of type 'UIView'
}
Run Code Online (Sandbox Code Playgroud)

为了使两条语句预览正常工作,我必须明确变量类型:

@available(iOS 17, *)
#Preview {
    let uiView: UIView = UIView()
    return uiView
}
Run Code Online (Sandbox Code Playgroud)