You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI项目中反复出现“Editor placeholder in source file”错误求助

SwiftUI项目中反复出现“Editor placeholder in source file”错误

我在SwiftUI项目里一直碰到“Editor placeholder in source file”错误,点击错误跳转到对应文件后错误会马上消失,但过会儿又会重新出现。

报错文件完整代码

import SwiftUI

@main
struct FooodApp: App {
    @StateObject var storage = Storage()
    @StateObject var anotherStorage = AnotherStorage()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(storage)
                .environmentObject(anotherStorage)
                .task {
                    do {
                        try await storage.load()
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
                .task {
                    do {
                        try await anotherStorage.load()
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
                
        }
    }
}

已尝试的解决方法

  • 剪切并重新粘贴文件内全部代码
  • 重新手动编写文件代码
  • 使用Cmd + Shift + B编译
  • 执行Product > Clean Build Folder...操作
  • 关闭项目
  • 重启Xcode

我的Storage是带有已初始化@Published属性的ObservableObject类。有没有我忽略的明显问题?


编辑1

应要求提供ContentView代码,原结构体也略有修改(见上方):

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var storage: Storage
    @EnvironmentObject var anotherStorage: AnotherStorage
    
    @State private var selectedTab: Tab = .anotherView
    
    enum Tab {
        case someView
        case anotherView
    }
    
    var body: some View {
        TabView(selection: $selectedTab) {
            SomeView() {
                Task {
                    do {
                        try await storage.save(items: storage.items)
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
            }
            .tabItem {
                Label("View", systemImage: "bookmark.fill")
            }
            .tag(Tab.someView)
            
            AnotherView {
                Task {
                    do {
                        try await anotherStorage.save(item: anotherStorage.item)
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
            }
            .tabItem {
                Label("Another", systemImage: "person.fill")
            }
            .tag(Tab.anotherView)
        }
        .onAppear {
            let appearance = UITabBarAppearance()
            appearance.backgroundEffect = UIBlurEffect(style: .systemUltraThinMaterial)
            UITabBar.appearance().standardAppearance = appearance
            UITabBar.appearance().scrollEdgeAppearance = appearance
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(Storage())
            .environmentObject(AnotherStorage())
    }
}

编辑2

项目还存在另一个由Source Control导致的问题:Xcode未将所有变更推送至远程仓库,甚至在空闲时也会每隔几分钟崩溃。解决该问题后,上述错误也随之消失。

这两个问题存在关联——IDE可能读取到了已不存在的内容。通常该错误会在重启项目或Xcode(或修复代码)后消失,但我需要提交并推送Xcode遗漏的变更才能解决。

内容的提问来源于stack exchange,提问作者ASCIImo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 03:57:42