【问题标题】:Xcode RealityKit / Failed to produce diagnostic for expressionXcode RealityKit / 无法为表达式生成诊断
【发布时间】:2020-10-04 01:36:10
【问题描述】:

我正在尝试使用 RealityKit 制作增强现实应用程序,但 ContentView.swift 我遇到了一些问题 这里缺少什么?`您可以在我分享的图片上看到错误。我学习了一些教程,所以我是 Xcode 和 Realitykit 的新手。

Failed to produce diagnostic for expression; please file a bug report
Cannot find 'PlacementButtonsView' in scope

import SwiftUI
import RealityKit

struct ContentView : View {
    var models: [String] = {
        
        let filemanager = FileManager.default
        
        guard let path = Bundle.main.resourcePath, let files = try?
            filemanager.contentsOfDirectory(atPath:path) else
                { return[]
            }
        var avaliableModels: [String] = []
        for filename in files where filename.hasSuffix("usdz") {
            let modelName = filename.replacingOccurrences(of: ".usdz", with: "")
            avaliableModels.append(modelName)
        }
        
        return avaliableModels
    }()
    
    var body: some View {
        ZStack(alignment: .bottom) {
            ARViewContainer()
            
            ModelPickerView(models: self.models)
            
            PlacementButtonsView()
        }
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
    
}

struct ModelPickerView: View {
    var models: [String]
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(0 ..<
                    self.models.count) { index in
                    Button(action: {
                        print("DEBUG: selected model with name: \(self.models[index])")
                    }) {
                        Image(uiImage: UIImage(named: self.models[index])!)
                            .resizable()
                            .frame(height: 60)
                            .aspectRatio(1/1,contentMode: .fit)
                            .background(Color.white)
                            .cornerRadius(12)

                         }
                             .buttonStyle (PlainButtonStyle())
                    
                      }
             }
       }
        .padding(15)
        .background(Color.black.opacity(0.5))
}

struct PlacementButtonsView: View {
    var body: some View {
        HStack {
            //Cancel Button
            Button(action: {
                print("DEBUG: model placement canceled.")
            }) {
                Image(systemName: "xmark")
                .frame(width: 60, height: 60)
                .font(.title)
                .background(Color.white.opacity(0.75))
                .cornerRadius(30)
                .padding(20)
                
            }
            
            //Confirm Button
            Button(action: {
                print("DEBUG: model placement confirmed.")
            }) {
                Image(systemName: "checkmark")
                .frame(width: 60, height: 60)
                .font(.title)
                .background(Color.white.opacity(0.65))
                .cornerRadius(30)
                .padding(20)
                
            }
        }
    }
}
    
#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

    
}

【问题讨论】:

    标签: xcode arkit realitykit


    【解决方案1】:

    检查你的大括号。PlacementButtonsView 结构实际上嵌套在ModelPickerView 结构中,并且不是全局可用的,因此为什么它在你的ContentView 中不可用。

    顺便说一句,在 Xcode 中,您可以通过选择单击PlacementButtonsView 的声明来找到它:

    ModelPickerView.PlacementButtonsView 告诉你这里出了什么问题; PlacementButtonsView 嵌套在 ModelPickerView 中。这就是为什么您的代码示例的最后一行似乎有一个奇怪的右大括号 - 预览也会出现同样的问题,因为它也嵌套在 ModelPickerView 中。

    为了让这个问题更加明显,并且在以后更容易看到类似的问题,您还可以通过选择全部 (Cmd + A) 然后按 Control + I 让 Xcode 为您缩进您的代码。您将查看 PlacementButtonsView 结构缩进,更清楚地表明它不是全局可用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2022-01-07
      相关资源
      最近更新 更多