首页 > 解决方案 > 错误 - 顶层不允许使用表达式

问题描述

我在运行此代码时遇到了一些问题,我在另一篇文章中找到了它Stack OverflowARKit:如何将 UIView 添加到 ARKit 场景? 想看看它是否对我有用。但我无法弄清楚为什么会出现错误“表达式不允许在顶层”。

let text = "Hello, Stack Overflow."
let font = UIFont(name: "Arial", size: CGFloat(12))
let width = 128
let height = 128

let fontAttrs: [NSAttributedString.Key: Any] =
        [NSAttributedString.Key.font: font as! UIFont]

let renderer = UIGraphicsImageRenderer(size: CGSize(width: CGFloat(width), 
height: CGFloat(height)))
let image = renderer.image { context in

    let color = UIColor.blue.withAlphaComponent(CGFloat(0.5))

    color.setFill()
    context.fill(CGRect(
            origin: CGPoint(x: 0, y: 0),
            size: UIScreen.main.bounds.size))

    text.draw(with: (CGRect( origin: CGPoint(x: 0, y: 0), size: 
    UIScreen.main.bounds.size)), options: .usesLineFragmentOrigin, attributes: 
            fontAttrs, context: nil)
}

let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))
plane.firstMaterial?.diffuse.contents = image
let node = SCNNode(geometry: plane)

在以下代码行中发现错误“表达式不允许在顶层”:

plane.firstMaterial?.diffuse.contents = image 

先感谢您。

标签: iosuiviewuikitscenekitarkit

解决方案


推荐阅读