Xcode and SwiftUI gets error "cannot preview in this file" when running the apple SwiftUI tutorial Landmarks

Just starting with Xcode and the Apple SwiftUI tutorial. My 2017 iMac is remotely connected via RealVnc to my PC. The iMac is running Ventura 13.6 and Xcode 15.2. Xcode was successfully downloaded and created the example Landmarks as IOS App with SwiftUI and Swift language.

When it completes creating the app, it shows "Preview Paused".

Running it reports, "Build Succeeded" and the iPhone 15 Pro simulator opens, but the preview window shows, "preview paused". Then, when I hit the retry icon in the preview window, the error, "cannot preview in this file" pops up.


This is my first venture into Xcode so I could use a little help here.



[Edited by Moderator]


iMac 21.5″ 4K, macOS 13.6

Posted on Mar 8, 2024 12:56 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 11, 2024 1:55 PM

I've just checked a 3.4 GHz i5 16 GB SSD iMac 2017 on macOS 13.6.4 with Xcode 15.2, and it works*.


It's ~slow, and the 8 GB HDD configuration will be exceedingly slow, but it does work*.


Given your posting, I'd wonder if there are add-ons beyond the VNC and the rest–what you've mentioned there tends to also include other add-ons such as add-on anti-malware and add-on cleaners and the rest, and your reply indicates a VPN (maybe a typo for VNC?) and those can all definitely mess with Xcode and the local environment.


Clear all that off, all of it, and try that SwiftUI tutorial again.


Or—since most of us don't have access to that tutorial—maybe clear off all those add-on apps and try the following, and see if this more-accessible-to-others example works for you:

Hello, SwiftUI | Apple Developer Documentation




*I am getting the usual barrage of app crashes related to running the simulator, and I had a development-enabled iPhone connected when Xcode was launched.



Similar questions

11 replies
Question marked as Top-ranking reply

Mar 11, 2024 1:55 PM in response to PickyBikers

I've just checked a 3.4 GHz i5 16 GB SSD iMac 2017 on macOS 13.6.4 with Xcode 15.2, and it works*.


It's ~slow, and the 8 GB HDD configuration will be exceedingly slow, but it does work*.


Given your posting, I'd wonder if there are add-ons beyond the VNC and the rest–what you've mentioned there tends to also include other add-ons such as add-on anti-malware and add-on cleaners and the rest, and your reply indicates a VPN (maybe a typo for VNC?) and those can all definitely mess with Xcode and the local environment.


Clear all that off, all of it, and try that SwiftUI tutorial again.


Or—since most of us don't have access to that tutorial—maybe clear off all those add-on apps and try the following, and see if this more-accessible-to-others example works for you:

Hello, SwiftUI | Apple Developer Documentation




*I am getting the usual barrage of app crashes related to running the simulator, and I had a development-enabled iPhone connected when Xcode was launched.



Mar 11, 2024 2:18 PM in response to PickyBikers

PickyBikers wrote:

I will remove macOS and start fresh with a clean install.

That's only going to help if you have certain 3rd party system modifications that can't be uninstalled properly otherwise. There are lots of such things. But it can be tricky. You have to be careful about restoring from backup. You can't restore any apps, software, system settings, or "other files". Otherwise you are just restoring it back the way it was before.


Also, the last time I had to do a restore (very recently due to a forced upgrade to Sonoma), my restore was corrupt. I can't bring myself to tell people not to use Time Machine, but I'm not comfortable recommending erase and reinstalls anymore.

And yes, it is RealVNC.

VNC is really tricky. At best, it can be really slow. Depending on the implementation, is can be extraordinarily slow and significantly impact the overall performance of the computer. This is true across all operating systems. VNC is just a minefield. I use Microsoft Remote Desktop to remote into my Linux computer. When I was using a remote Mac, I used Apple Screen Sharing. I don't think either option is viable going from a PC to a Mac. Check your CPU usage while running VNC.

Mar 8, 2024 5:11 PM in response to PickyBikers

PickyBikers wrote:

I disconnected the realvpn. Rebooted the iMac and I tried it with a real iPhone 15 Pro connected to the iMac and disconnected. It doesn't make any difference.

If you were running on the device, then you wouldn't be using previews at all.


Previews is kind of worthless. I removed those things from every file. Of course, later on, I got rid of all the SwiftUI stuff. It just isn't ready. Give it 5 or 6 more years. If Apple hasn't forgotten about it by then, then it might be useable.


Otherwise, that may be more than a 2017 iMac can handle. I never used the simulator when I still had an intel machine. It just wasn't worth the time. It's not bad with Apple Silicon though. In fact, that's another reason not to bother with Previews. On Apple Silicon, there's no point. The simulator is faster. I think Previews was just a gimmick to get people to adopt SwiftUI until Apple Silicon came out.


For now, just remove all the Previews code. You won't miss it.

Mar 10, 2024 1:23 PM in response to PickyBikers

PickyBikers wrote:

I am trying to follow along with a book, "SwiftUI Cookbook". It makes extensive use of previews.

Apple has its own tutorials. But being Apple, they are really hard to find. You would think they would be listed somewhere prominently on the developer site. Instead, you have to have already seen them somehow and know to Google them.


Here is one site, for example: Introducing SwiftUI | Apple Developer Documentation


Ordinarily in these situations, one might say "start from there", but again, this is Apple. While are tutorials on that page, they don't seem to link to this other, much more extensive tutorials page: Develop apps for iOS | Apple Developer Documentation. The "Start here" buttons seem to be the best way to go.


I can't really fault people for pushing Previews. Apple certainly does that too. But then Apple pushes lots of things that are really bad ideas. You're welcome to try them if you want. I did too. Your experience sounds pretty typical. I sure don't miss them.

If I turn off all the previews, then I am left with the simulator and compiling after every change?

Yes and no.


By default, Xcode is constantly compiling. It wasn't so bad before, if you had a fast Apple Silicon computer. But recent versions of Xcode 14 and 15 have effectively broken that. I recommend going into Xcode Settings and turning off "live preview" (no relation to the other previews that you've been talking about) so that it only builds when you try to build and run.


Otherwise, two things happen. 1) You'll start getting lots of phantom errors. You know the code it correct. It compiles and runs perfectly. But after doing that, it complains about errors. You can clear the errors, but they'll come right back. You can search the internet for a solution. Many people report this problem. Many people claim to have found a solution. No solution exists.


2) You actually do have an error. Xcode doesn't care. It will run the app anyway. It runs the previous version, which is obviously useless. Whether this is the simulator, a real device, or a Mac, it takes a few seconds to realize it is happening, wait for it, then quit. It gets annoying after a few dozen times.

I think that would take forever on my 2017 iMac.

Anything with modern Apple software is going take forever on a 2017 iMac. I think I've mentioned this before, but I don't even know which iMac you have. Some of those 2017 iMacs aren't just "not Apple Silicon", they are horribly, horribly slow. They will struggle with basic, built-in apps. Xcode? No más, no más!

I'm just not sure about where to go from here.
Should I find a book on IOS programming that does not use SwiftUI? Most of what is out there says use SwiftUI.

It's hard to say. SwiftUI is definitely what all the hip kids are using now. But what isn't often mentioned is that it follows the "react" principle. If you aren't familiar with the levels of absurdity that computer science PhD's can descend to, then you probably don't realize what that means. It isn't something made for humans. It's really twisted.


Above, you mentioned compiling after every change, as if that was a bad thing. That's how SwiftUI works. It runs your entire program every time any data point changes. Redraws every screen. Sets everything up fresh. Every time the user presses a key or clicks a button. Just so you know.

Should I buy a new M3 based iMac? If so, how much memory do I need for programming?

You've got a 2017 iMac. It's simply a question of how much you want to punish yourself. Personally, I would recommend one of those new 15" Mac Book Airs. I have the previous M2 generation and it is noticeably faster than my older, but still very fast 2021 M1 MacBook Pro. My old 2017 top-of-the-line MacBook Pro is painfully slow compared to either of these.


16 GB of RAM is fine. It's more the SSD speed than anything else. For all I know, your iMac still has a mechanical hard drive.

It is really disappointing to find out that creating the basic IOS hello world app using SwiftUI fails to run as expected and it could cost more than $1000 do get it to run previews.

Suggestions on a better way to start programming iPhones are welcome.

It's a brutal world. There are millions of developers, just for Apple platforms. Neither Apple, nor anyone else, needs any more developers. It's amazing that there are as many tutorials and support for beginners as there is. Of course, the idea is that you'll work cheaper and complain less than us greybeards.


But what they neglect to mention is that it takes years to gain minimal competency. You can build simple apps with SwiftUI. You can even do it on a 2017 iMac. But you have to stick with it. And wait for it. And put up with more bugs than you can imagine. You aren't doing anything wrong. You just shouldn't believe the marketing shtick or what people say on the internet. This is as good as it gets.



Mar 10, 2024 12:23 PM in response to etresoft

I am trying to follow along with a book, "SwiftUI Cookbook". It makes extensive use of previews.


If I turn off all the previews, then I am left with the simulator and compiling after every change? I think that would take forever on my 2017 iMac.


I'm just not sure about where to go from here.

Should I find a book on IOS programming that does not use SwiftUI? Most of what is out there says use SwiftUI.

Should I buy a new M3 based iMac? If so, how much memory do I need for programming?


It is really disappointing to find out that creating the basic IOS hello world app using SwiftUI fails to run as expected and it could cost more than $1000 do get it to run previews.


Suggestions on a better way to start programming iPhones are welcome.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Xcode and SwiftUI gets error "cannot preview in this file" when running the apple SwiftUI tutorial Landmarks

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