//
// ContentView.swift
// HardApp
//
// Created by Besleaga Alexandru Marian on 14.06.2024.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// A SwiftUI preview.
#Preview { Use of unknown directive '#Preview'
// The view to preview.
}
I'm trying to learn Xcode and got stuck on these error that I receive when trying to apply the macro #Preview, how can declare the directive so I can use it in my own code for the preview it offers ?
Kind Regards
Xcode Previews
RSS for tagCreate an app UI and configure almost everything your users see using Xcode Previews.
Posts under Xcode Previews tag
153 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Hey Guys, a have some problens with Xcode preview for some reason I'm getting this error
LinkDylibError: Failed to build CardDetailsScreen.swift
Linking failed: linker command failed with exit code 1 (use -v to see invocation)
ld: -make_mergeable can only be used when creating a dynamic library
clang: error: linker command failed with exit code 1 (use -v to see invocation)
previews_diagnostics_summary.txt
As someone who frequently uses Preview, it would be convenient to have the option to view the preview on another display. Are there any existing methods or plans to introduce a feature that allows us to pop out the previews to another screen?
In the first Xcode 16 beta, none of my SwiftUI previews work - they all just crash on start up.
Has anybody run into this and found a workaround? Have tried all the usual steps of cleaning the project/restarting Xcode.
Hey,
I've opened an existing project with Xcode 16 Beta 1 (I'm also running macOS Sequoia).
None of my previews work anymore. They simply crash with an error I don't quite understand:
JITError
==================================
| NoBuiltTargetDescriptionCouldBeFound
|
| translationUnit: PreviewTranslationUnit(moduleNamePrefix: "Previews_ErrorView", sourceIdentifier: file:///Users/dennis/Code/Project/Project/Components/ErrorView.swift -> ErrorView.swift, parseTree: ParseTree(version: 1848, statements: 5, providers: 0), update: nil, changesContextMemoizer: PreviewsPipeline.PreviewTranslationUnit.(unknown context at $3561fdc4c).ChangesContextMemoizer(parseTree: ParseTree(version: 1848, statements: 5, providers: 0), sourceIdentifier: file:///Users/dennis/Code/Project/Project/Components/ErrorView.swift -> ErrorView.swift, cachedValue: os.OSAllocatedUnfairLock<Swift.Optional<PreviewsModel.ParseTree.PreviewChangesContext>>(__lock: Swift.ManagedBuffer<Swift.Optional<PreviewsModel.ParseTree.PreviewChangesContext>, __C.os_unfair_lock_s>)), registryDeclarationMemoizer: PreviewsPipeline.PreviewTranslationUnit.(unknown context at $3561fdbec).RegistryDeclarationMemoizer)
|
| builtTargetDescriptions:
The app builds and runs fun on a simulator and on device.
Any ideas what this error means? Is this just an early Beta 1 bug or might there be anything I can do in my project to fix this?
The WWDC24 demo projects all work fine and their previews are working as well.
Thanks!
#Preview, @available(iOS 17, *), PreviewProvider
Preview is not working.
I've tried this and that, but I can't.
Please help me.
Starting on Xcode 15.3 and 15.4 my SwiftUI Previews stopped working with the following error:
== PREVIEW UPDATE ERROR:
LinkDylibError: Failed to build ContentView.swift
Linking failed: linker command failed with exit code 1 (use -v to see invocation)
ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
Undefined symbols for architecture arm64:
"static MyLibrary.DisplayUtil.getText() -> Swift.String", referenced from:
closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o
"type metadata accessor for MyLibrary.DisplayUtil", referenced from:
closure #1 () -> SwiftUI.Text in (extension in Demo_Broken_Preview_PreviewReplacement_ContentView_1):Demo_Broken_Preview.ContentView.(__preview__body in _1BA320C8FB5388C953E1E463345C3D72).getter : some in ContentView.1.preview-thunk.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
After much trial and error, I narrowed it down to my SwiftUI views using properties or functions inside components in Swift Packages.
This is a simple example of my Swift Package:
public class DisplayUtil {
public func getText() -> String {
return "Text"
}
public static func getText() -> String {
return "Text"
}
}
And my SwiftUI View
import SwiftUI
import MyLibrary
struct ContentView: View {
var body: some View {
VStack {
Text(DisplayUtil.getText())
}
.padding()
}
}
#Preview {
ContentView()
}
The same code works fine on Xcode 15.2
Link to the sample project:
https://www.icloud.com/iclouddrive/0c00AD0esi2PwqqiRggokfwGw#Demo%5FBroken%5FPreview
Is anybody else having a similar issue?
Can anyone give me assistance on how to fix this. My preview crashed because a Fatal Error in ModelData.
This is how my ModelData looks:
import SwiftUI
struct SutraokeDetail: View {
@Environment(ModelData.self) var modelData
var sutra: Sutras
var body: some View {
@Bindable var modelData = modelData
ScrollView {
CircleImage(image: sutra.image)
.offset(y: -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
HStack{
Text(sutra.name)
.font(.title)
Spacer()
Text(sutra.text)
}
}
}
}
}
#Preview {
let modelData = ModelData()
return SutraokeDetail(sutra: modelData.sutras[0])
.environment(modelData)
}
Hello,
I use Preview to quickly test functionalities. However I found that the unified logging does not output to Preview console. So I had to do both log.debug and print. Is there a way to enable logging to Preview console?
Hello. I've been looking all over this forum and stack and done some internet searches but unfortunately have found nothing to assist in fixing an error I'm receiving on my project in Xcode.
Every view shows an error identical to the one shown above, only with that specific file name displayed instead. When I go to fix the error, this is the error detail displayed:
== PREVIEW UPDATE ERROR:
LinkDylibError: Failed to build TabBar.swift
Linking failed: linker command failed with exit code 1 (use -v to see invocation)
ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
ld: unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '/Users/brycemchose/Library/Developer/Xcode/DerivedData/Service_Square_App-dnblqgabcrxprhaizjtkcvouyzln/Build/Intermediates.noindex/Previews/iphonesimulator/Service Square App/Products/Debug-iphonesimulator/Service Square.app/Service Square'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Note that I can run the app successfully to a simulator or to my iPhone, but cannot see the preview within the Xcode canvas. I've been blindly developing the last two app updates, pushing the changes to the simulator and then tweaking the code based on the simulated version. I can keep doing this for a little while but it is significantly slower and more difficult, especially to fix small bugs and make tiny changes, so I'd really like to see if anyone has an idea for how I can get the preview back to being properly displayed. I'm brand new to this whole thing and have no idea how to fix this issue, so I'd be really grateful if someone with more experience could offer some suggestions! Thanks in advance! :)
I already filed feedback FB13777941 for this, but wanted to ask if anyone has the same problem, since I can't find anything on google or SO.
I created a new package in Xcode (15.3) and added this to it:
import SwiftUI
struct Test: View {
var body: some View {
Text("Hello world")
}
}
#Preview {
Test()
}
The package builds, tests can run. Previews don't work.
This is more or less the error I get from the diagnostics button (formatted for readability):
LoadingError: failed to load library at path ”PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct”: Optional(dlopen(PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct, 0x0000):
tried:
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),
'/System/Volumes/Preboot/Cryptexes/OSPATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')),
'/System/Volumes/Preboot/Cryptexes/OSPATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (no such file),
'PATH_TO_DERIVED_DATA/Build/Intermediates.noindex/Previews/macos/PACKAGE_NAME/Products/Debug/PackageFrameworks/PACKAGE_NAME_63097C4A372B9C87_PackageProduct.framework/Versions/A/PACKAGE_NAME_63097C4A372B9C87_PackageProduct' (mach-o file, but is an incompatible architecture (have 'arm64', need 'arm64e')))
I don't remember ever having this problem. The arm64 vs arm64e makes no sense to me - why would Xcode get that wrong... and only for previews?
I've tried restarting, deleting derived data, the usual rain dances.
Any ideas?
I'm seeing some weird behavior with conditional compilation when I use a build configuration other than "Debug" or "Release", and I'm wondering if I'm doing something wrong or if this is an Xcode bug.
The setup
Xcode version: 15.3
I have a simple SwiftUI view that takes in a model and displays an attribute of the model.
struct ContentView: View {
let model: Model
var body: some View {
VStack {
Text("Name: \(model.name)")
}
.padding()
}
}
In the model file, I have the struct definition, but also an extension that defines some sample data for use in SwiftUI previews:
struct Model {
let id: String
let name: String
}
#if DEBUG
extension Model {
static let example = Model(
id: "50fef362-f53d-4ded-9168-b887ff62e59d",
name: "John Doe"
)
}
#endif
And finally, I have a preview provider that uses this sample data:
#Preview {
ContentView(model: Model.example)
}
Normal behavior
With the default "Debug" build configuration, this works just fine. There are no compilation errors, and the preview renders as expected.
The issue
Start off by changing the name of the build configuration to, e.g. "DebugDev"
Now I start seeing seeing a mis-match between what happens and what Xcode tells me is happening.
The code still compiles and runs, and the SwiftUI preview still works, but Xcode is displaying things as if it doesn't compile.
Xcode formats the conditionally compiled code with a lower opacity color as if DEBUG isn't defined:
In the view file, I now see a compilation error when referencing the conditionally-compiled code:
Autocomplete doesn't work when trying to reference the conditionally compiled code
Additional details/observations
I have confirmed that the DEBUG active compilation condition is still defined in the build settings. All I've done is change the name of the build config
I was initially thinking this might have something to do with the fact that this appears in a preview provider, since those are treated a bit differently, but the same thing happens if I reference the conditionally compiled code directly in the view class
Another theory: since I was referencing conditionally compiled code from code that wasn't conditionally compiled, maybe Xcode was trying to tell me that wasn't valid. So, I tried placing the code that calls the conditionally compiled code (in this case, the view class) inside an #if DEBUG. This does get rid of the displayed compilation error, but auto-complete still doesn't work, and the whole class is displayed with the lower-opacity font.
Help?
I feel like I must be missing something. The only alternative I can think of is that Xcode has some logic hard-coded with the default "Debug" build config, and that would be...just silly.
On May 1 I started to see this image instead of watch UI preview. It worked well before. I didn't update Xcode, neither I deleted or installed any new simulator OS. It was harmful to my work, since simulator download is 4GB and I need to wait when it downloads, instead of reviewing PRs and closing tasks.
Did anyone had the same issue? Do you know how to prevent it?
So like the title says, when I start up Xcode the preview won;t work till I run a debug session using the simulator.
Sometimes the debug session is unable to start the simulator, which I can start manually then run a debug session.
Once all the above is done, preview works.
Any idea what is causing this behavior?
Hi All,
I have been using Xcode for less than 5 months.
Recently I get a problem that my previews does not load, even when I created a new project on a local disk.
The Project builds, but the preview does not.
The error I saw from the Diagnostics window is as below
== PREVIEW UPDATE ERROR:
HumanReadableSwiftError
CancellationError: CancellationError()
With the newest version of Xcode (15.3) I cannot get Preview to load and am getting the following error:
HumanReadableSwiftError
CancellationError: CancellationError()
I'd had this same issue a few months ago and opening up an older project's previews and then opening the project I was working on would allow Preview to show.
The last few weeks I've not been getting this error and after 3 or 4 days not being in Xcode I'm getting this same error again.
I'm relatively new to Xcode and Swift so not being able to load the darn preview and work through tutorials after seemingly doing the exact same thing I've been doing for months is quite ridiculous and frustrating.
Any ideas on how to resolve this issue would be greatly appreciated.
I referred to the code provided in the article Adding and Editing Persistent Data in Your App in the SwiftData documentation and tested the "/PreviewHelper" approach. I found that if a model class contains a property of a struct, and that struct includes a Set property or an Array property, the Preview would throw an error. Here is an example of the model:
@Model
class Book {
// ...
var someStruct: SomeStruct
// ...
}
struct SomeStruct: Codable {
// ...
var someCollection: Set<Int>
// ...
}
In actuality, this model can run and store data without any issues, but it fails to run in the Preview. Is there a problem with the implementation of this Preview or is there any other reason? Are there better ways to perform Preview in SwiftData?
Hi,
I have had this issue since I started working again in iOS this year with Xcode 15.
What happened to the SwiftUI Previews?
My problem is that they are stuck forever in the Loading screen "Preparing iPhone Simulator for Previews".
I now test the code on the actual Simulator and waiting for Xcode 16 to fix this.
The characteristics of my project are:
Using Swift Package Manager (10+ dependencies)
Using Unit Tests and Test Plans.
Using macOS Ventura (reverted from Sonoma because it's even worse)
Macbook Air M1 - 16GB of RAM.
I have tried everything including:
Erasing derived data.
Restarting.
Reinstalling the whole computer.
Reverting Sonoma to Ventura.
Reverting Xcode 15.2 to 15.1 and 15.0.1.
Using Previews_Provider and #Previews {}
Using completely empty projects to test previews.
Please help.
Thanks
Recently my live preview canvas on Xcode recently stopped accepting my keyboard inputs out of the blue when I type in a textField. Has this bug been addressed? I'm on iOS sim 17.4, Xcode 15.3
I am not able to use swiftUI preview in my project.
i have attached the diagnosed report for the same.
Please update.
previews_diagnostics_summary.txt