i have an issue with Xcode Previews. If I create a new project, previews is working fine but on my active application that's was build with Mojave on SwiftUI isn't working.
I even made a new Project and dragged all files to the new project but something seems to be wrong with some file that this happens.
I always get this error in Previews:
Failed to build active scheme
See this:
Check out the Xcode Report navigator for more information about the issue.
Omg after spending almost 1:30 hours on it I found out that in one fine I had this:
#State private var tappedBarIndex = 0 {
didSet {
print(self.tappedBarIndex)
}
}
Its doesn't work if you have didset
Related
See this simple example. Putting DatePicker in VStack has weird layout issue and causes all the content in VStack to not show up (I'm using XCode 13.2 + iOS15.2).
struct ContentView: View {
#State var date: Date = Date()
var body: some View {
Form {
VStack {
DatePicker("Date", selection: $date)
Text("date: \(date)")
}
}
}
}
Replacing the DatePicker with other views (e.g. TextField) works fine. Removing the VStack also works fine. However, I need to have the VStack to make subviews inside it to show up in a single cell.
I googled but wasn't able to find any discussion about this issue. I tried to wrap DatePicker in HStack or ZStack but neither worked. Does anyone know how to work around this issue? Thanks.
A big thank to #Yrb, whose suggestion helps me to find the root cause of the issue.
TL;DR It's a simulator issue. Resetting the simulated device resolved the issue.
More details: I first noticed the issue when I tested my app on the simulator. I thought it was a bug with iOS, especially after I was able to reproduce the issue with the above code in a fresh project. Since #Yrb wasn't able to reproduce the issue, I was about to upgrade my system. However, his suggestion reminded me to try it again on other simulator (I usually use iPhone SE2). It was surprising that other devices didn't have the issue. What's more, while I could reproduce the issue consistently on iPhone SE2 simulator, the preview in XCode just worked fine. So I reset the iPhone SE2 simulator and restarted it (BTW, just restarting it didn't work), the code worked fine!
It's still a mystery what exactly happened in the simulator. But I'm glad to know it's neither my code issue nor an iOS issue.
It took me more than half a day to investigate the issue :( Lessons learned when running into similar weird issue in future: 1) reset simulator 2) try it on physical device also.
My SwiftUI App is extremly lagging and I have no Idea why. Even if I only compile this code, my CPU Power goes up to 100%, even if I dont touch anything:
#main
struct Order_Corner_V8App: App {
var body: some Scene {
return WindowGroup {
TabView{
Text("").tabItem {
Text("click")
}
Text("").tabItem {
Text("click")
}
}
}
}
}
I need the TabView. I put my whole Files in an other new Project, the same thing happend again. Maybe its usefull to know that im working with Firebase.
I'm targeting iOS.
I've already watched to my App in 'Instruments', it only says that the problem is my main View.
Please help me.
Here is some image that shows my CPU Data
Thanks, Boothosh
Okay I got the solution: I tried the App on an other phone and everything works fine. I think this is an mistake of my phone, but I don't know what I can do against that.
Thanks, Boothosh
In my case, I found Xcode source control caused the problem
To fix this problem Xcode -> Preferences -> Source Control uncheck Enable Source control. Then restart Xcode, everything will be fine
In my SwiftUI app, I have put some sample model objects in Xcode's "Preview Content" folder. I only access them in my SwiftUI PreviewProviders. My understanding is that the code/assets in this folder are only used for previewing in the Canvas. My previews work properly. I'm using Xcode 11.6 (11E708).
However, when I go to Archive my app, it fails because it cannot find these sample model objects. I was under the impression that PreviewProvider code is not build into the final binary as of Xcode 11. See this question and this screenshot from the Xcode 11 release notes.
(Curiously, I am able to compile with the Release configuration. It's just Archive that fails.)
As a workaround, I can put the #if DEBUG/#endif wrappers around my PreviewProvider but the above screenshot indicates that shouldn't be necessary.
Do I misunderstand how the "Preview Content" folder works?
Edit: Nevermind, this approach "works" but the preview code is still in the final archive, so it doesn't really help anything.
I put all my preview test data in its own `PreviewProvider` so that I can access it in other Previews. The Archive still succeeds, and the test data will get stripped in the final Archive. No `#if DEBUG` is needed.
Example (specific to my own use case):
struct PreviewData: PreviewProvider {
static var previews: some View {
Text("This is test data for use in other previews.")
}
// Use enums for namespacing if desired
enum Choices {
static let deals = Choice(id: UUID().uuidString, text: "I want the best deals.", iconName: "bestDealsIcon")
static let technology = Choice(id: UUID().uuidString, text: "I love technology!", iconName: "technologyIcon")
}
Then I can access PreviewData.Choices.deals in other PreviewProviders.
I had a very similar issue occur, only I was building an App Clip for an existing app. I couldn't compile with the release scheme or archive for distribution with neither versions of Xcode 12.5.1 or 13.0.
The solution was to include the Preview Content directory under the development assets of the parent app's target, not the App Clip target itself. As a result, I got it to work in all scenarios, including SwiftUI previews, release builds and archiving the parent app for distribution.
So I created a macOS cocoa project called "test" using swift ui in Xcode 11 beta and got problems at the very beginning.
Sorry in advance if my question is dumb.
The first problem is that the automatic preview says it is in pause. If I click resume, I got an error.
So I tried to run the code Xcode generated (I changed nothing in the code.) and it build successfully. When I tried to run the code freshly build, I come across the following error: EXC_BAD_INSTRUCTION
The line highlighted in red is the .frame line in the following piece of code:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
I'm on macOS Catalina Public Beta 1 so this is not a problem with the version of the system....
So, did I miss something? The tutorials I found online (I mostly looked on Youtube and Apple Dev websites) either show examples for iOS or show examples that are not starting from scratch ( For example on Apple websites, when they are demo-ing codes for macOS apps ).
Thanks in advance
EDIT
I'm adding this picture, it might help:
Actual screen when the app is running with the info of the threads section
Final answer:
Just needed an update Xcode to beta 2 :) .
I know there is a similar question but the OP seems to think his problem was only on the simulator and so not such an issue... But I think mine is in both.
I was having a problem adapting an app for iCloud storage (when trying to observe for NSUbiquityIdentityDidChangeNotification). I could not see what the problem was with my code and so...
...after hours of banging my head against a wall I have set up a new project in Xcode (v6.1 6A1046a) to try and narrow it down. It seems it must be a very basic problem indeed...
The only things I have done in this new projects are:
Enable iCloud capabilities for 'Key-value storage' and for 'iCloud
Documents'(using the default container) - no error messages, appears to be set up fine.
Add a label and a button to the provided view controller in IB.
Add an IBOutlet for both and hook up an IBAction to change the label text when pressed.
For clarity, the only code I added to an empty 'Single View Application' template is:
#IBOutlet weak var label: UILabel!
#IBOutlet weak var button: UIButton!
#IBAction func buttonPressed(sender: AnyObject) {
self.label.text = "You pressed the button"
}
So if I run the project from Xcode in the simulator or on the device, or if I run the project directly on the device, it launches fine and the button works properly - as you would expect...
The problem is:
If I am running on the device from Xcode & I go to the Settings->iCloud->iCloud Drive on the device and toggle the switch for my app (from/to either state) the app crashes. I'm getting no feedback except this:
If I am running the app directly on the device and try and toggle iCloud Drive setting, when I go back into the app it appears to restart (the view is being reset, along with the label.text - neither of which happen if I just visit settings without touching the switch).
It also has the habit of freezing my device completely, which is mildly irritating.
I'm new to iCloud development so I guess I may be missing something really basic...
As I say it's an empty project with only a couple of lines of additional code so it's probably not something I HAVE done. Which limits it to something I HAVEN'T done. I am trying to follow as many tutorials as I can find and as much apple documentation as well. I just can't see any obvious steps I may have overlooked.
Unless it's the expected behaviour, or an Apple issue I guess.
Thanks for any help - it's driving me nuts!
iOs kill your app when you change iCloudDrive state. This is standard iOs behavior you can check this with build in applications(Safari for example).