Skip to content

Using @Environment will crash in Preview #114

@zhuscat

Description

@zhuscat

Description

Here is my code:

import SwiftUI
import Perception

@Perceptible
class HelloModel {
  var msg: String = "Hello, World"
}

struct HelloView: View {
  @Environment(HelloModel.self) var model

    var body: some View {
      WithPerceptionTracking {
        Text(model.msg)
      }
    }
}

#Preview {
    HelloView()
      .environment(HelloModel())
}

It crashes in preview:

== PAUSED REASON:

    processCrashed



== PREVIEW UPDATE ERROR:

    CrashReportError: Fatal Error in Environment.swift
    
    fito crashed due to fatalError in Environment.swift at line 54.
    
    No perceptible object of type HelloModel found. A View.environment(_:) for HelloModel may be missing as an ancestor of this view.

Checklist

  • I have determined that this bug is not reproducible using Swift's observation tools. If the bug is reproducible using the @Observable macro or another tool from the Observation framework, please file it directly with Apple.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

No crash happened

Actual behavior

Crash happened

Steps to reproduce

Described in description

Perception version information

1.4.1

Destination operating system

iOS 16.0

Xcode version information

Version 16.1 (16B40)

Swift Compiler version information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    apple bugSomething isn't working due to a bug on Apple's platforms.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions