Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No ObservableObject of type Navigator found." caused by xcode preview #29

Closed
qtisan opened this issue Sep 29, 2021 · 2 comments
Closed

Comments

@qtisan
Copy link

qtisan commented Sep 29, 2021

image

As seen, I successfully run with it on my actual device, but failed in preview.
Am I made something wrong?

MacOS BigSur 11.6 (20G165)
XCode Version 13.0 (13A233)
Swift 5.5

Just simple code

import SwiftUI
import SwiftUIRouter

struct Simple: View {
  
    var body: some View {
      
      NavLink(to: "/welcome") {
        Text("Enter")
      }
      
    }
}

struct Simple_Previews: PreviewProvider {
    static var previews: some View {
        Simple()
    }
}

Diagnostics

No ObservableObject of type Navigator found. A View.environmentObject(_:) for Navigator may be missing as an ancestor of this view.

----------------------------------------

MissingEnvironmentObjectError: Preview is missing environment object "Navigator"

[Sample] crashed due to missing environment of type: Navigator. To resolve this add `.environmentObject(Navigator(...))` to the appropriate preview.

Process: [Sample][65512]
Date/Time: 2021-09-29 01:02:26 +0000
Log File: <none>

Application Specific Information:
     [
        dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/[Sample]-dcfjwaohgmozctgpvtbixewazoqo/Build/Intermediates.noindex/Previews/[Sample]/Products/Debug-iphonesimulator DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot//System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection DYLD_FRAMEWORK_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/[Sample]-dcfjwaohgmozctgpvtbixewazoqo/Build/Intermediates.noindex/Previews/[Sample]/Products/Debug-iphonesimulator 
        SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type Navigator found. A View.environmentObject(_:) for Navigator may be missing as an ancestor of this view.
         
        CoreSimulator 776.3 - Device: iPhone 12 Pro (48F2C8C7-D179-42EE-8709-82EB7E969381) - Runtime: iOS 15.0 (19A339) - DeviceType: iPhone 12 Pro
        
    ]

==================================

|  MessageSendFailure: Message send failure for send previewInstances message to agent
|  
|  ==================================
|  
|  |  RemoteHumanReadableError: Cannot send message on <UVAgentPreviewServiceServerConnection: 0x600001ee54a0>: connection has invalidated
|  |  
|  |  com.apple.dt.UITestingAgent (-1):
@frzi
Copy link
Owner

frzi commented Sep 29, 2021

When you use Previews, your View will be rendered in a very isolated environment. All SwiftUIRouter Views have to be used somewhere inside a Router.

Try this instead:

struct Simple_Previews: PreviewProvider {
    static var previews: some View {
        Router {
            Simple()
        }
    }
}

All SwiftUIRouter Views (except for Router) depend on several environment objects and values. The Router View initiates all these objects and values. 😃

@qtisan
Copy link
Author

qtisan commented Sep 29, 2021

Thank you, it looks like nice!

@qtisan qtisan closed this as completed Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants