SwiftUI Preview launches macOS App from root - swiftui

I can successfully launch the following preview.
import SwiftUI
struct SwiftUIView: View {
var body: some View {
Text("Hello, World!")
}
}
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}
But the entire macOS App also launches as an independent external simulator. It's very annoying because it sits on top of Xcode and any hot reload event pushes it back in front of Xcode.
Any help on how to prevent this would be appreciated.
Xcode 14.1 (14B47b)
macOS 13.0 (22A380)

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 displays error - Cannot preview in this file -- current target needs adjusted build settings

I'm using Xcode 11.1 and all of my swift previews show the following errors:
Cannot preview in this file -- current target needs adjusted build settings
along with
"ProjectTests.xctest' failed to get build
settings:
unsupportedProductType("com.apple.product-type.bundle.unit-test", <Xc..."
Clicking the diagnostics button generates the following popup:
Open file has supported build settings
"ProjectTests.xctest" failed to get build settings: unsupportedProductType("com.apple.product-type.bundle.unit-test", <Xcode3TargetProduct:0xblahblahblahProjectTests.xctest blueprint:< Xcode3Target:0xblahblahProjectTests>>)
My previews still render fine, but is there some way to remove this error?
Change Destination to some Simulator instead of "iOS Generic Device" or in preview provider specify preview device explicitly, like
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("iPhone 11")
}
}
Xcode Version 11.1 (11A1027)
This error no longer exists in Xcode Version 11.2 (11B52).

'init(systemName:)' is unavailable in macOS

I would like to use SF Symbols in my macOS project. How to implement one?
Button(action: {}) {
Image(systemName: "star") //Error: 'init(systemName:)' is unavailable in macOS
}
It is nativly supporting from the macOS 11 beta or later, then it works as usual, otherwise you have to export a template and import it to the assets catalog, then you can use it as a normal image. So:
if #available(OSX 11.0, *) {
Image(systemName: "trash.fill")
} else {
Image("trash.fill") // Imported as a supporting format like PDF (not SVG)
}
Another way is to use the symbol directly in the text:
Text("􀈒") // The symbol itself can not be shown on the markdown of the StackOverflow
Remember that you should embed the font in your application or the destination should have the SF Symbols App installed
Apple's Human Interface Guidelines state:
You can use SF Symbols in apps running in iOS 13 and later, watchOS 6 and later, and tvOS 13 and later.
No Mac support at this time. ☚ī¸
Before using this code you should install SF Symbols app
I make Image for macOS like below
that's how it looks on macOS

Incredible simple code (SwiftUI) is not working anymore

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