Incredible simple code (SwiftUI) is not working anymore - swiftui

I participate in the Catalina and Xcode beta test program.
Some weeks ago, I was working with a simple macOS Cocoa/SwiftUI app (not smartphone as most users do). Yesterday it just stopped working and crashed instead. This can of course happen for a beta tester, but I am a bit surprised and annoyed about this, because it prevents me for going on. And I don't understand why it suddenly just don't work anymore.
To go more into detailed. The error is very easy to reproduce:
Create a new Cocoa/SwiftUI project
Add a #State variable to the ContentView
Add a Toggle view to the ContentView's body
Bind the #State variable to the Toggle
Run
It then crashes immediately (not when clicking the Toggle, but when run).
Hope someone can verified this for me. Especially that it is not a fault on my installation/system, but in the latest beta software.
I am running Xcode Version: 11.0 beta 2 (11M337n). And Catalina version 10.15 Beta (19A501i).
To solve the problem, I have tried to clean the build folders.
The code below produces the error, although not using a Toggle, but a TabbedView.
import SwiftUI
struct ContentView : View
{
#State var selection: Int = 1
var body: some View
{
TabbedView(selection: $selection)
{
Text("Tab 1!").tabItemLabel(Text("Tab 1")).tag(1)
Text("Tab 2!").tabItemLabel(Text("Tab 2")).tag(2)
}
}
}
Log in output window when it crash:
dyld: lazy symbol binding failed: Symbol not found: _$s7SwiftUI5StateV13delegateValueAA7BindingVyxGvg
Referenced from: /Users/asl/Library/Developer/Xcode/DerivedData/aaa-byucivzkosyehgdhjnjxicgrjnbi/Build/Products/Debug/aaa.app/Contents/MacOS/aaa
Expected in: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI
dyld: Symbol not found: _$s7SwiftUI5StateV13delegateValueAA7BindingVyxGvg
Referenced from: /Users/asl/Library/Developer/Xcode/DerivedData/aaa-byucivzkosyehgdhjnjxicgrjnbi/Build/Products/Debug/aaa.app/Contents/MacOS/aaa
Expected in: /System/Library/Frameworks/SwiftUI.framework/Versions/A/SwiftUI

Related

SwiftUI: 'some' return types are only available in macOS 10.15.0 or newer - on MacOS Ventura

I have some strange error in my SPM.
Code example:
import SwiftUI
struct ExampleView: View {
var body: some View {
Text("Stub")
}
}
Error:
'some' return types are only available in macOS 10.15.0 or newer
But I have macOS Ventura 13.0.1, & XCode 14.1. What could be the problem?
Thanks.
The code itself looks fine. As #Timmy mentioned, there's probably something wrong with the project settings. Try upping the deployment target...
Go to your project settings by selecting your project in Project Navigator.
Select your project
And change the deployment target
Problem solved.
I needed to change simulator from Mac (by Default) to iPhone.

SwiftUI's Settings window for macOS does not show up on a Catalyst app

I have a simple app with an iOS target that is configured for iPhone, iPad, and Mac, making it a Catalyst app. On Xcode 12.0 Beta 1, I tried adding a Settings scene to my app. The documentation page says it is compatible with both macOS 11.0+ and
Mac Catalyst 14.0+. The entirety of my app is this:
import SwiftUI
#main
struct TestSettingsApp: App {
#SceneBuilder var body: some Scene {
WindowGroup {
Text("Hello, world!").padding()
}
#if os(macOS)
Settings {
Text("Settings here.")
}
#endif
}
}
Based on the WWDC 2020 What's new in SwiftUI video (4:50), I would expect this to automatically add a "Preferences..." menu option, which would show "Settings here." However, this option never shows up. I also tried replacing #if os(macOS) with #if targetEnvironment(macCatalyst), which had no effect.
It turns out that Apple's documentation was either incorrect early on in the beta period, or they decided not to implement Settings for Mac Catalyst. The documentation page I referred to earlier has since been updated to remove "Mac Catalyst 14.0+".

SwiftUI Preview fails with linker errors

I've trying to add a SwiftUI view to an existing UIKit + Storyboards based tvOS project. The actual build works fine and I can run in the simulator & see my SwiftUI view, but when I resume the SwiftUI preview I get 'Failed to build MyClass.swift' with the following diagnostics:
I saw this similar SO post regarding the -bundle and -bitcode_bundle cannot be used together error, but my configuration is already the way the answer suggests (setting the Mach-O type to Executable and ensuring no Bundle Loader is set).
There's also a warning about /Applications/Xcode.app/Contents/SharedFrameworks-appletvsimulator not being found. That directory certainly does not exist, but nowhere in my project is SharedFrameworks-appletvsimulator referenced that I can find. Also, if I create a new SwiftUI project it doesn't complain about that.
The SwiftUI code is literally just a new "Hello World" SwiftUI file from the Xcode template - I haven't modified it at all.
Does anyone have any ideas what's going on here or how to fix it?
For me the fix was disabling the code coverage option:
Go to your scheme menu -> select edit scheme -> go to the "Test" section on the left side menu -> uncheck the Code Coverage mark
I'm using Xcode 12.5
iOS minimum target 13.4
There are diffrent reasons for the preview to fails and several posts about it , for example:
This Question

SwiftUI app crash on run in macOS environment [duplicate]

I build an app with the 2nd beta of Xcode 11, using SwiftUI, and everything worked completely fine, but now, with the 3rd beta, the app isn't even build. I get errors like "Use of undeclared type 'View'", "Unknown attribute 'State'" etc. What can I do? Is the problem in my code or is it just a bug?
The problem is when I'm trying to build the app to run on my Mac (with UIKit for Mac)
#State var score = 0
The error is "Unknown attribute 'State'"
struct ContentView : View {
/*...*/
}
The error is "Use of undeclared type 'View'"
Got same issue on Xcode 11.2. Found out that the reason was that somewhere in same module I declared protocol State {}. This breaks compiler without any reasonable errors.
This is from the Xcode 11 Beta 3 Release Notes.
Known Issues
Xcode 11 doesn’t support working with SwiftUI in an iPad app brought to macOS. (41416222, 51201699)
I downloaded the Xcode 11 Beta 3 myself just to confirm the errors you mentioned, and I had the same issues when trying to build a SwiftUI project for Mac.
I would suggest going back to using Xcode Beta 2 if possible. Unfortunately, Apple doesn't provide download links to previous betas, so hopefully you have a Time Machine backup or Beta 2 still installed on your computer. Otherwise, you might be out of luck until the next beta is released.
I was getting the same "Unknown attribute 'State'" issue in Beta 5. But I was doing "#state" and not "#State". Case matters. It's hard to notice because both ways are shown as purple.
If you are doing a Kotlin multi-platform project, check if your shared module has a component named "State".
Instead #main, please replace #UIApplicationMain.
I've attached screenshots.
https://prnt.sc/vimvwn
I got the same error while trying to compile a Swift project created with Xcode 13 on Xcode 11. The problem was that the newer version uses the SDK 12.1 while the older one uses the SDK 10.15. To let the project compile on the older Xcode/SDK I just performed these steps:
remove the #main attribute from the swift source code
add Cocoa.framework in the section Frameworks, Libraries, and Embedded Content
add a file named main.m to the project (no bridge header) with this content:
#include <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
return NSApplicationMain(argc, argv);
}

SwiftUI: Automatic Preview Updating Error "Failed to build ContentView.swift"

I'm trying to run the Automatic Preview in Xcode 11, however even though the project builds successfully, the automatic preview fails giving me the error Failed to build ContentView.swift. Failed to code sign ContentView.swift.
I have just created the project without any modifications to the auto generated code. Even the code signing has also been set properly. Note I'm using the Free Apple Developer Account. Could this cause any problem?
Following is the code from ContentView.swift file:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
First select the Xcode using following command
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
Then run
sudo xcodebuild -license
Hope it is helpful
I solved that problem by going to the Xcode - Preferences -> Locations and assigning the Command Line Tools to Xcode 11. Also keep in mind that it is a beta, so it is unstable.
I had this problem as I was using // to comment beside my curly brackets to check I had them all in the right spot in my screen code. Xcode seems to cope with a few but once the screen gets too large and too many comments......
Now I comment on the line below and no problems!
Xcode SwiftUI Previews may occasionally fail with an error message about a modified .h file, such as “file Header.h has been modified since the module file Module.pcm was built: mtime changed.” (85938686)
Workaround: Delete the Clang module cache by running the following command in Terminal:
rm -rf "$TMPDIR/../C/clang/ModuleCache"
Then try to preview the file again.
https://developer.apple.com/documentation/Xcode-Release-Notes/xcode-13_3-release-notes
I had this problem too. I solved it by moving the file location of the file I was trying to preview. When the file was created it was down below the resources folder, so I moved it up and then preview worked.
I am currently having this problem with XCode 12.2, it says "Failed to build ContentView.swift." and when you click on the information's button it says "Extra tokens at end of #SourceLocation".
Apparetly that was because i wrote a "// Comment" in the same line, next to a variable definition, so i solved this only moving the comment to the next line.
I don't know who i can help this, but just wanted to share it.
Regards!
Change your iOS version to the latest one. It will solve your issue.
Renaming main project solved same issue on my Mac.
Rename the blue file in your project navigation.
I encountered this problem too. I download Xcode beta-1 before I download macOS 10.15. Then after my system upgrade to 10.15, the preview function didn't work. It turns out that Apple has released Xcode beta-2 version recently, so I downloaded and it worked. So, KEEP THE NEWEST VERSION OF XCODE!!!!