Home > OS >  Swiftui Preview Crashing using Navigationstack (Problem Report included)
Swiftui Preview Crashing using Navigationstack (Problem Report included)

Time:12-04

I can't figure out why my preview is crashing each time I press the "Hello World" text. I have a programatically navigationstack setup, and each time a view is appended it crashes in the preview. The app on my phone works great though, but I thought I must be doing something wrong if the preview is messing up. Thanks!

Here is the problem report:


-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: 980494AC-44F1-43DF-B8BE-AB6FAA94E7A7
CrashReporter Key:   2F1EEC48-C1DA-2145-0171-733D5994FA2F
Hardware Model:      MacBookAir9,1
Process:             Previewtest [88699]
Path:                /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/B5B42958-9616-46D6-A056-B44D3D125005/data/Containers/Bundle/Application/E4B38613-49C2-4582-9650-7295F743C525/Previewtest.app/Previewtest
Identifier:          Partyhallen.Previewtest
Version:             1.0 (1)
Code Type:           X86-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [87351]
Coalition:           com.apple.CoreSimulator.SimDevice.B5B42958-9616-46D6-A056-B44D3D125005 [11821]
Responsible Process: SimulatorTrampoline [2117]

Date/Time:           2022-12-03 00:13:50.6532  0100
Launch Time:         2022-12-03 00:13:49.4824  0100
OS Version:          macOS 12.6 (21G115)
Release Type:        User
Report Version:      104

Exception Type:  EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 4 Illegal instruction: 4
Terminating Process: exc handler [88699]

Triggered by Thread:  0

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib                  0x7ff80d700cc8 _assertionFailure(_:_:file:line:flags:)   424
1   SwiftUI                                0x10d6c9ed8 0x10c316000   20659928
2   SwiftUI                                0x10d6c9de5 0x10c316000   20659685
3   Previewtest                            0x1081105ab ContentView.router.getter   139
4   ContentView.1.preview-thunk.dylib          0x10e4b761e closure #1 in closure #1 in ContentView.__preview__body.getter   30 (ContentView.swift:13)
5   SwiftUI                                0x10c7fc79d 0x10c316000   5138333
6   SwiftUI                                0x10d08588a 0x10c316000   14088330
7   SwiftUI                                0x10cded270 0x10c316000   11367024
8   SwiftUI                                0x10cded284 0x10c316000   11367044
9   SwiftUI                                0x10cded270 0x10c316000   11367024
10  SwiftUI                                0x10cb03491 0x10c316000   8311953
11  SwiftUI                                0x10cb02df2 0x10c316000   8310258
12  SwiftUI                                0x10cc947a5 0x10c316000   9955237
13  SwiftUI                                0x10d4437c8 0x10c316000   18012104
14  SwiftUI                                0x10d441e5e 0x10c316000   18005598
15  SwiftUI                                0x10d441f42 0x10c316000   18005826
16  SwiftUI                                0x10d441720 0x10c316000   18003744
17  UIKitCore                              0x1090274b9 -[UIGestureRecognizer _componentsEnded:withEvent:]   153
18  UIKitCore                              0x1096c6ebd -[UITouchesEvent _sendEventToGestureRecognizer:]   662
19  UIKitCore                              0x1090176f7 -[UIGestureEnvironment _updateForEvent:window:]   469
20  UIKitCore                              0x109669edb -[UIWindow sendEvent:]   5282
21  UIKitCore                              0x10963d7f2 -[UIApplication sendEvent:]   898
22  UIKitCore                              0x1096e4e61 __dispatchPreprocessedEventFromEventQueue   9381
23  UIKitCore                              0x1096e7569 __processEventQueue   8334
24  UIKitCore                              0x1096dd8a1 __eventFetcherSourceCallback   272
25  CoreFoundation                      0x7ff800387035 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__   17
26  CoreFoundation                      0x7ff800386f74 __CFRunLoopDoSource0   157
27  CoreFoundation                      0x7ff800386771 __CFRunLoopDoSources0   212
28  CoreFoundation                      0x7ff800380e73 __CFRunLoopRun   927
29  CoreFoundation                      0x7ff8003806f7 CFRunLoopRunSpecific   560
30  GraphicsServices                    0x7ff809c5c28a GSEventRunModal   139
31  UIKitCore                              0x10961c62b -[UIApplication _run]   994
32  UIKitCore                              0x109621547 UIApplicationMain   123
33  SwiftUI                                0x10d3fbcfb 0x10c316000   17718523
34  SwiftUI                                0x10d3fbba8 0x10c316000   17718184
35  SwiftUI                                0x10cab1b7d 0x10c316000   7977853
36  Previewtest                            0x108112d4e static PreviewtestApp.$main()   30 (PreviewtestApp.swift:4)
37  Previewtest                            0x108112e09 main   9
38  dyld_sim                               0x1083662bf start_sim   10
39  dyld                                   0x11476f52e start   462

Thread 1:
0   libsystem_pthread.dylib             0x7ff834c38f48 start_wqthread   0

Thread 2:
0   libsystem_pthread.dylib             0x7ff834c38f48 start_wqthread   0

Thread 3:
0   libsystem_pthread.dylib             0x7ff834c38f48 start_wqthread   0

Thread 4:
0   libsystem_pthread.dylib             0x7ff834c38f48 start_wqthread   0

Thread 5:
0   libsystem_pthread.dylib             0x7ff834c38f48 start_wqthread   0

Thread 6:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib              0x7ff834bdf97a mach_msg_trap   10
1   libsystem_kernel.dylib              0x7ff834bdfce8 mach_msg   56
2   CoreFoundation                      0x7ff8003868de __CFRunLoopServiceMachPort   145
3   CoreFoundation                      0x7ff80038102f __CFRunLoopRun   1371
4   CoreFoundation                      0x7ff8003806f7 CFRunLoopRunSpecific   560
5   Foundation                          0x7ff800c5595c -[NSRunLoop(NSRunLoop) runMode:beforeDate:]   213
6   Foundation                          0x7ff800c55bd5 -[NSRunLoop(NSRunLoop) runUntilDate:]   72
7   UIKitCore                              0x1096f0886 -[UIEventFetcher threadMain]   535
8   Foundation                          0x7ff800c7f1c3 __NSThread__start__   1009
9   libsystem_pthread.dylib             0x7ff834c3d4e1 _pthread_start   125
10  libsystem_pthread.dylib             0x7ff834c38f6b thread_start   15


Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000200000003  rbx: 0x000000010d6ff670  rcx: 0xfffffffe00000000  rdx: 0x0000000000000003
  rdi: 0x00007f7959c0f888  rsi: 0x000000000000001b  rbp: 0x00007ff7b7def100  rsp: 0x00007ff7b7def0b0
   r8: 0x0000000000000071   r9: 0x0000000000000070  r10: 0x0000000000000f30  r11: 0x0000600001a61aa8
  r12: 0x0000000000000000  r13: 0x0000000000000046  r14: 0x00007f7959c0f880  r15: 0x000000000000000b
  rip: 0x00007ff80d700cc8  rfl: 0x0000000000010246  cr2: 0x000000012793b000
  
Logical CPU:     1
Error Code:      0x00000000 
Trap Number:     6


Binary Images:
    0x7ff80d6d4000 -     0x7ff80db90ff4 libswiftCore.dylib (*) <1d23cd50-8b48-349b-9163-f7990e0f95bd> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib
       0x10c316000 -        0x10d9eefff com.apple.SwiftUI (4.1.17.100) <1622c162-0a3a-354c-94ea-62c5dd369612> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/SwiftUI.framework/SwiftUI
       0x10810c000 -        0x108113fff Partyhallen.Previewtest (1.0) <a989b4b0-9e28-34db-865c-bbcabac9f176> /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/B5B42958-9616-46D6-A056-B44D3D125005/data/Containers/Bundle/Application/E4B38613-49C2-4582-9650-7295F743C525/Previewtest.app/Previewtest
       0x10e4b5000 -        0x10e4b8fff ContentView.1.preview-thunk.dylib (*) <5d91714e-5553-3450-88be-8850c5896cb4> /Users/USER/Library/Developer/Xcode/DerivedData/Previewtest-haqnqqxsierjlvhdmnfotfqgnorc/Build/Intermediates.noindex/Previews/Previewtest/Intermediates.noindex/Previewtest.build/Debug-iphonesimulator/Previewtest.build/Objects-normal/x86_64/ContentView.1.preview-thunk.dylib
       0x1087e1000 -        0x10a29dfff com.apple.UIKitCore (1.0) <c2258b63-cdcc-3504-a06e-8067adba9c34> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
    0x7ff800302000 -     0x7ff80068affc com.apple.CoreFoundation (6.9) <55edff37-af14-3fed-b932-031049d0a665> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
    0x7ff809c59000 -     0x7ff809c60ff2 com.apple.GraphicsServices (1.0) <5dad91c5-e70d-3f9a-88f2-2d1ed7c8dd24> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
       0x108364000 -        0x1083c3fff dyld_sim (*) <638f8a1f-2a32-396d-8389-8d7a60b96b8d> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim
       0x11476a000 -        0x1147d5fff dyld (*) <71febccd-d9dc-3599-9971-2b3407c588a8> /usr/lib/dyld
    0x7ff834c37000 -     0x7ff834c42ff7 libsystem_pthread.dylib (*) <b5454e27-e8c7-3fdb-b77f-714f1e82e70b> /usr/lib/system/libsystem_pthread.dylib
    0x7ff834bde000 -     0x7ff834c15fff libsystem_kernel.dylib (*) <8cc28466-fd2f-3c80-9834-9525b7beac19> /usr/lib/system/libsystem_kernel.dylib
    0x7ff8006fd000 -     0x7ff80102dffc com.apple.Foundation (6.9) <353e6739-fc3a-3636-89f2-194adba7203b> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation

EOF

-----------
Full Report
-----------

{"app_name":"Previewtest","timestamp":"2022-12-03 00:13:51.00  0100","app_version":"1.0","slice_uuid":"a989b4b0-9e28-34db-865c-bbcabac9f176","build_version":"1","platform":7,"bundleID":"Partyhallen.Previewtest","share_with_app_devs":0,"is_first_party":0,"bug_type":"309","os_version":"macOS 12.6 (21G115)","incident_id":"980494AC-44F1-43DF-B8BE-AB6FAA94E7A7","name":"Previewtest"}
{
  "uptime" : 370000,
  "procLaunch" : "2022-12-03 00:13:49.4824  0100",
  "procRole" : "Foreground",
  "version" : 2,
  "userID" : 501,
  "deployVersion" : 210,
  "modelCode" : "MacBookAir9,1",
  "procStartAbsTime" : 374779515462303,
  "coalitionID" : 11821,
  "osVersion" : {
    "train" : "macOS 12.6",
    "build" : "21G115",
    "releaseType" : "User"
  },
  "captureTime" : "2022-12-03 00:13:50.6532  0100",
  "incident" : "980494AC-44F1-43DF-B8BE-AB6FAA94E7A7",
  "bug_type" : "309",
  "pid" : 88699,
  "procExitAbsTime" : 374780663063696,
  "cpuType" : "X86-64",
  "procName" : "Previewtest",
  "procPath" : "\/Users\/USER\/Library\/Developer\/Xcode\/UserData\/Previews\/Simulator Devices\/B5B42958-9616-46D6-A056-B44D3D125005\/data\/Containers\/Bundle\/Application\/E4B38613-49C2-4582-9650-7295F743C525\/Previewtest.app\/Previewtest",
  "bundleInfo" : {"CFBundleShortVersionString":"1.0","CFBundleVersion":"1","CFBundleIdentifier":"Partyhallen.Previewtest"},
  "storeInfo" : {"deviceIdentifierForVendor":"B5E785AA-2399-55A2-8DC1-FCE60C089E4D","thirdParty":true},
  "parentProc" : "launchd_sim",
  "parentPid" : 87351,
  "coalitionName" : "com.apple.CoreSimulator.SimDevice.B5B42958-9616-46D6-A056-B44D3D125005",
  "crashReporterKey" : "2F1EEC48-C1DA-2145-0171-733D5994FA2F",
  "responsiblePid" : 2117,
  "responsibleProc" : "SimulatorTrampoline",
  "wakeTime" : 5437,
  "bridgeVersion" : {"build":"19P6067","train":"6.6"},
  "sleepWakeUUID" : "407BF33E-E77C-46C9-BE09-2761B3AEE27C",
  "sip" : "enabled",
  "isCorpse" : 1,
  "exception" : {"codes":"0x0000000000000001, 0x0000000000000000","rawCodes":[1,0],"type":"EXC_BAD_INSTRUCTION","signal":"SIGILL"},
  "termination" : {"flags":0,"code":4,"namespace":"SIGNAL","indicator":"Illegal instruction: 4","byProc":"exc handler","byPid":88699},
  "extMods" : {"caller":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"system":{"thread_create":0,"thread_set_state":0,"task_for_pid":28},"targeted":{"thread_create":0,"thread_set_state":0,"task_for_pid":0},"warnings":0},
  "faultingThread" : 0,
  "threads" : [{"triggered":true,"id":4314897,"instructionState":{"instructionStream":{"bytes":[184,0,0,0,0,0,0,0,16,73,133,196,15,132,198,1,0,0,72,131,192,255,76,137,247,72,33,199,72,131,199,32,72,184,255,255,255,255,255,255,0,0,73,33,196,72,131,236,8,68,15,182,69,24,76,137,230,72,139,85,208,72,139,77,16,73,137,217,69,49,228,139,69,40,80,65,85,65,87,232,20,18,0,0,72,131,196,32,76,137,247,232,56,65,52,0,15,11,72,131,236,8,72,141,5,251,241,69,0,72,141,61,184,237,69,0,72,141,13,205,240,69,0,190,11,0,0,0,65,184,57,0,0,0,186,2,0,0,0,65,185,2,0,0,0,106,0,104,148,0,0,0,106,2,106,24,80,232,182,2,0,0,72,131,236,8,72,141,5,203,237,69,0,72,141,61,120,237,69,0,72,141,13,141,237,69,0,190,11,0,0,0],"offset":96}},"threadState":{"r13":{"value":70},"rax":{"value":8589934595},"rflags":{"value":66118},"cpu":{"value":1},"r14":{"value":140159173589120},"rsi":{"value":27},"r8":{"value":113},"cr2":{"value":4958957568},"rdx":{"value":3},"r10":{"value":3888},"r9":{"value":112},"r15":{"value":11},"rbx":{"value":4520408688},"trap":{"value":6},"err":{"value":0},"r11":{"value":105553143929512},"rip":{"value":140703354064072,"matchesCrashFrame":1},"rbp":{"value":140701918490880},"rsp":{"value":140701918490800},"r12":{"value":0},"rcx":{"value":18446744065119617024},"flavor":"x86_THREAD_STATE","rdi":{"value":140159173589128}},"queue":"com.apple.main-thread","frames":[{"imageOffset":183496,"symbol":"_assertionFailure(_:_:file:line:flags:)","symbolLocation":424,"imageIndex":0},{"imageOffset":20659928,"imageIndex":1},{"imageOffset":20659685,"imageIndex":1},{"imageOffset":17835,"sourceFile":"ContentView.swift","symbol":"ContentView.router.getter","symbolLocation":139,"imageIndex":2},{"imageOffset":9758,"sourceLine":13,"sourceFile":"ContentView.swift","symbol":"closure #1 in closure #1 in ContentView.__preview__body.getter","imageIndex":3,"symbolLocation":30},{"imageOffset":5138333,"imageIndex":1},{"imageOffset":14088330,"imageIndex":1},{"imageOffset":11367024,"imageIndex":1},{"imageOffset":11367044,"imageIndex":1},{"imageOffset":11367024,"imageIndex":1},{"imageOffset":8311953,"imageIndex":1},{"imageOffset":8310258,"imageIndex":1},{"imageOffset":9955237,"imageIndex":1},{"imageOffset":18012104,"imageIndex":1},{"imageOffset":18005598,"imageIndex":1},{"imageOffset":18005826,"imageIndex":1},{"imageOffset":18003744,"imageIndex":1},{"imageOffset":8676537,"symbol":"-[UIGestureRecognizer _componentsEnded:withEvent:]","symbolLocation":153,"imageIndex":4},{"imageOffset":15621821,"symbol":"-[UITouchesEvent _sendEventToGestureRecognizer:]","symbolLocation":662,"imageIndex":4},{"imageOffset":8611575,"symbol":"-[UIGestureEnvironment _updateForEvent:window:]","symbolLocation":469,"imageIndex":4},{"imageOffset":15240923,"symbol":"-[UIWindow sendEvent:]","symbolLocation":5282,"imageIndex":4},{"imageOffset":15058930,"symbol":"-[UIApplication sendEvent:]","symbolLocation":898,"imageIndex":4},{"imageOffset":15744609,"symbol":"__dispatchPreprocessedEventFromEventQueue","symbolLocation":9381,"imageIndex":4},{"imageOffset":15754601,"symbol":"__processEventQueue","symbolLocation":8334,"imageIndex":4},{"imageOffset":15714465,"symbol":"__eventFetcherSourceCallback","symbolLocation":272,"imageIndex":4},{"imageOffset":544821,"symbol":"__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__","symbolLocation":17,"imageIndex":5},{"imageOffset":544628,"symbol":"__CFRunLoopDoSource0","symbolLocation":157,"imageIndex":5},{"imageOffset":542577,"symbol":"__CFRunLoopDoSources0","symbolLocation":212,"imageIndex":5},{"imageOffset":519795,"symbol":"__CFRunLoopRun","symbolLocation":927,"imageIndex":5},{"imageOffset":517879,"symbol":"CFRunLoopRunSpecific","symbolLocation":560,"imageIndex":5},{"imageOffset":12938,"symbol":"GSEventRunModal","symbolLocation":139,"imageIndex":6},{"imageOffset":14923307,"symbol":"-[UIApplication _run]","symbolLocation":994,"imageIndex":4},{"imageOffset":14943559,"symbol":"UIApplicationMain","symbolLocation":123,"imageIndex":4},{"imageOffset":17718523,"imageIndex":1},{"imageOffset":17718184,"imageIndex":1},{"imageOffset":7977853,"imageIndex":1},{"imageOffset":27982,"sourceLine":4,"sourceFile":"PreviewtestApp.swift","symbol":"static PreviewtestApp.$main()","imageIndex":2,"symbolLocation":30},{"imageOffset":28169,"sourceFile":"PreviewtestApp.swift","symbol":"main","symbolLocation":9,"imageIndex":2},{"imageOffset":8895,"symbol":"start_sim","symbolLocation":10,"imageIndex":7},{"imageOffset":21806,"symbol":"start","symbolLocation":462,"imageIndex":8}]},{"id":4314907,"frames":[{"imageOffset":8008,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":9}]},{"id":4314908,"frames":[{"imageOffset":8008,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":9}]},{"id":4314909,"frames":[{"imageOffset":8008,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":9}]},{"id":4314910,"frames":[{"imageOffset":8008,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":9}]},{"id":4314911,"frames":[{"imageOffset":8008,"symbol":"start_wqthread","symbolLocation":0,"imageIndex":9}]},{"id":4314912,"name":"com.apple.uikit.eventfetch-thread","frames":[{"imageOffset":6522,"symbol":"mach_msg_trap","symbolLocation":10,"imageIndex":10},{"imageOffset":7400,"symbol":"mach_msg","symbolLocation":56,"imageIndex":10},{"imageOffset":542942,"symbol":"__CFRunLoopServiceMachPort","symbolLocation":145,"imageIndex":5},{"imageOffset":520239,"symbol":"__CFRunLoopRun","symbolLocation":1371,"imageIndex":5},{"imageOffset":517879,"symbol":"CFRunLoopRunSpecific","symbolLocation":560,"imageIndex":5},{"imageOffset":5605724,"symbol":"-[NSRunLoop(NSRunLoop) runMode:beforeDate:]","symbolLocation":213,"imageIndex":11},{"imageOffset":5606357,"symbol":"-[NSRunLoop(NSRunLoop) runUntilDate:]","symbolLocation":72,"imageIndex":11},{"imageOffset":15792262,"symbol":"-[UIEventFetcher threadMain]","symbolLocation":535,"imageIndex":4},{"imageOffset":5775811,"symbol":"__NSThread__start__","symbolLocation":1009,"imageIndex":11},{"imageOffset":25825,"symbol":"_pthread_start","symbolLocation":125,"imageIndex":9},{"imageOffset":8043,"symbol":"thread_start","symbolLocation":15,"imageIndex":9}]}],
  "usedImages" : [
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140703353880576,
    "size" : 4968437,
    "uuid" : "1d23cd50-8b48-349b-9163-f7990e0f95bd",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/usr\/lib\/swift\/libswiftCore.dylib",
    "name" : "libswiftCore.dylib"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4499529728,
    "CFBundleShortVersionString" : "4.1.17.100",
    "CFBundleIdentifier" : "com.apple.SwiftUI",
    "size" : 23957504,
    "uuid" : "1622c162-0a3a-354c-94ea-62c5dd369612",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/System\/Library\/Frameworks\/SwiftUI.framework\/SwiftUI",
    "name" : "SwiftUI",
    "CFBundleVersion" : "4.1.17.100"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4430282752,
    "CFBundleShortVersionString" : "1.0",
    "CFBundleIdentifier" : "Partyhallen.Previewtest",
    "size" : 32768,
    "uuid" : "a989b4b0-9e28-34db-865c-bbcabac9f176",
    "path" : "\/Users\/USER\/Library\/Developer\/Xcode\/UserData\/Previews\/Simulator Devices\/B5B42958-9616-46D6-A056-B44D3D125005\/data\/Containers\/Bundle\/Application\/E4B38613-49C2-4582-9650-7295F743C525\/Previewtest.app\/Previewtest",
    "name" : "Previewtest",
    "CFBundleVersion" : "1"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4534784000,
    "size" : 16384,
    "uuid" : "5d91714e-5553-3450-88be-8850c5896cb4",
    "path" : "\/Users\/USER\/Library\/Developer\/Xcode\/DerivedData\/Previewtest-haqnqqxsierjlvhdmnfotfqgnorc\/Build\/Intermediates.noindex\/Previews\/Previewtest\/Intermediates.noindex\/Previewtest.build\/Debug-iphonesimulator\/Previewtest.build\/Objects-normal\/x86_64\/ContentView.1.preview-thunk.dylib",
    "name" : "ContentView.1.preview-thunk.dylib"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4437446656,
    "CFBundleShortVersionString" : "1.0",
    "CFBundleIdentifier" : "com.apple.UIKitCore",
    "size" : 28037120,
    "uuid" : "c2258b63-cdcc-3504-a06e-8067adba9c34",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/System\/Library\/PrivateFrameworks\/UIKitCore.framework\/UIKitCore",
    "name" : "UIKitCore",
    "CFBundleVersion" : "6109.1.108"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140703131770880,
    "CFBundleShortVersionString" : "6.9",
    "CFBundleIdentifier" : "com.apple.CoreFoundation",
    "size" : 3706877,
    "uuid" : "55edff37-af14-3fed-b932-031049d0a665",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/System\/Library\/Frameworks\/CoreFoundation.framework\/CoreFoundation",
    "name" : "CoreFoundation",
    "CFBundleVersion" : "1953.1"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140703292559360,
    "CFBundleShortVersionString" : "1.0",
    "CFBundleIdentifier" : "com.apple.GraphicsServices",
    "size" : 32755,
    "uuid" : "5dad91c5-e70d-3f9a-88f2-2d1ed7c8dd24",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/System\/Library\/PrivateFrameworks\/GraphicsServices.framework\/GraphicsServices",
    "name" : "GraphicsServices",
    "CFBundleVersion" : "1.0"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4432740352,
    "size" : 393216,
    "uuid" : "638f8a1f-2a32-396d-8389-8d7a60b96b8d",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/usr\/lib\/dyld_sim",
    "name" : "dyld_sim"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 4638285824,
    "size" : 442368,
    "uuid" : "71febccd-d9dc-3599-9971-2b3407c588a8",
    "path" : "\/usr\/lib\/dyld",
    "name" : "dyld"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140704013840384,
    "size" : 49144,
    "uuid" : "b5454e27-e8c7-3fdb-b77f-714f1e82e70b",
    "path" : "\/usr\/lib\/system\/libsystem_pthread.dylib",
    "name" : "libsystem_pthread.dylib"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140704013475840,
    "size" : 229376,
    "uuid" : "8cc28466-fd2f-3c80-9834-9525b7beac19",
    "path" : "\/usr\/lib\/system\/libsystem_kernel.dylib",
    "name" : "libsystem_kernel.dylib"
  },
  {
    "source" : "P",
    "arch" : "x86_64",
    "base" : 140703135944704,
    "CFBundleShortVersionString" : "6.9",
    "CFBundleIdentifier" : "com.apple.Foundation",
    "size" : 9637885,
    "uuid" : "353e6739-fc3a-3636-89f2-194adba7203b",
    "path" : "\/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/iPhoneOS.platform\/Library\/Developer\/CoreSimulator\/Profiles\/Runtimes\/iOS.simruntime\/Contents\/Resources\/RuntimeRoot\/System\/Library\/Frameworks\/Foundation.framework\/Foundation",
    "name" : "Foundation",
    "CFBundleVersion" : "1953.1"
  }
],
  "sharedCache" : {
  "base" : 140703128616960,
  "size" : 2998861824,
  "uuid" : "3140e7f6-3cc2-3fac-81dd-5fbfbbe796e1"
},
  "vmSummary" : "ReadOnly portion of Libraries: Total=803.7M resident=0K(0%) swapped_out_or_unallocated=803.7M(100%)\nWritable regions: Total=575.2M written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=575.2M(100%)\n\n                                VIRTUAL   REGION \nREGION TYPE                        SIZE    COUNT (non-coalesced) \n===========                     =======  ======= \nActivity Tracing                   256K        1 \nCG raster data                       8K        1 \nColorSync                           88K        5 \nCoreAnimation                       20K        1 \nFoundation                          16K        1 \nKernel Alloc Once                    8K        1 \nMALLOC                           177.5M       30 \nMALLOC guard page                   32K        8 \nMALLOC_NANO (reserved)           384.0M        1         reserved VM address space (unallocated)\nSTACK GUARD                       56.0M        7 \nStack                             11.0M        7 \nVM_ALLOCATE                       1052K        4 \n__DATA                            11.3M      398 \n__DATA_CONST                      36.7M      402 \n__DATA_DIRTY                        26K       12 \n__FONT_DATA                        2352        1 \n__LINKEDIT                       358.0M       33 \n__OBJC_RO                         28.3M        1 \n__OBJC_RW                          880K        1 \n__TEXT                           445.8M      410 \ndyld private memory               1280K        2 \nmapped file                      206.8M       11 \nshared memory                       16K        1 \n===========                     =======  ======= \nTOTAL                              1.7G     1339 \nTOTAL, minus reserved VM space     1.3G     1339 \n",
  "legacyInfo" : {
  "threadTriggered" : {
    "queue" : "com.apple.main-thread"
  }
},
  "trialInfo" : {
  "rollouts" : [
    {
      "rolloutId" : "63582c5f8a53461413999550",
      "factorPackIds" : {

      },
      "deploymentId" : 240000002
    },
    {
      "rolloutId" : "60f8ddccefea4203d95cbeef",
      "factorPackIds" : {

      },
      "deploymentId" : 240000021
    }
  ],
  "experiments" : [

  ]
}
}


Here is my code:

import SwiftUI
import Foundation

@main
struct PreviewtestApp: App {
    var body: some Scene {
        WindowGroup {
            AppContainerView()
        }
    }
}
import SwiftUI
import Foundation

struct AppContainerView: View {
    @StateObject var router = Router()
    var body: some View {
        NavigationStack(path: $router.navigationPath) {
            ContentView()
                .navigationDestination(for: Route.self) { route in
                    switch route {
                    case .ContentView:
                        ContentView()
                    }
                }
        }.environmentObject(router)
    }
}

import SwiftUI
import Foundation

struct ContentView: View {
    @EnvironmentObject private var router: Router
    var body: some View {
        VStack {
            Text("Hello, world!")
                .onTapGesture {
                    router.pushView(route: .ContentView)
                }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

import Foundation
import SwiftUI

enum Route: Hashable {
    case ContentView
}

final class Router: ObservableObject {
    @Published var navigationPath = NavigationPath()
    
    func pushView(route: Route) {
        navigationPath.append(route)
    }
    
    func popToRootView() {
        navigationPath = .init()
    }
    
    func popToSpecificView(k: Int) {
        navigationPath.removeLast(k)
    }
}

CodePudding user response:

@StateObject aren't init during previewing so that is probably causing the crash. You could workaround it by previewing AppContainerView instead of ContentView.

By the way, @StateObject is for when you need to store a reference type in a @State but in this example don't need a reference type. Also, NavigationPath is designed to hold the data values, not routes. You can use multiple navigationDestination for each kind of value, instead of one with a switch statement.

  • Related