Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

SwiftUI Preview - Undefined symbols for architecture arm64

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?

MacBook Pro 16″, macOS 12.6

Posted on May 15, 2024 6:43 AM

Reply
1 reply

May 15, 2024 7:23 AM in response to xlsmearlx

xlsmearlx wrote:

Is anybody else having a similar issue?

No. Before I decided to give up on SwiftUI altogether, one of the first things I did was rip out all of the preview stuff. For one thing, they are horribly slow. Once I ripped them out, I was very pleased by how fast the simulator ran (natively) on Apple Silicon. It was much better than using the Simulator on Intel. But Previews seems to give you that same Intel Simulator experience on Apple Silicon.


Also, as my views got more and more complex, it because quite a hassle to keep the previews up to date with demo code. I wasn't using them anyway, so why was I writing all that code for something I wasn't using and was only slowing me down?


Unfortunately, in the end, I kept having to add UIKit hack after UIKit hack to the point where I was afraid that some OS update or SwiftUI update would break my entire codebase and I would have to rewrite it from scratch. That was a valid concern because all my test devices were iOS15 and Apple totally rearchitected the core navigation view for iOS16 in an incompatible way. So I chucked it all, rewrote it all in UIKit, and haven't regretted it.

SwiftUI Preview - Undefined symbols for architecture arm64

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.