Xcode doesn't pick up files changed by pre-action script - swift

In my project I need to generate a file that will be used in one of my Swift Packages.
I added a script to generate it as a pre-action to make sure it happens before anything else is build - and it works.
File is generated properly - it appears in Xcode project itself.
But Xcode doesn't pick it up when compiling this Package for the first time.
Next time I compile it - it catches it properly, but all further changes are not picked up again on first time it gets modified...
I found this thread with similar issue, but none of the solution works for me.
Doing it as a Step in Build Phases doesn't solve the problem, making it even worse.
Is there any reasonable solution to run some code generate that will be properly picked up by Xcode ?

Related

getting module not found after importing packages in swift

I've done a bit of swift, but I've never tried to use a third party package before. I created a brand new swift app, with Mac/iOS version and tests from the wizard. I went to xcode, right clicked on the sidebar, and chose add packages, and put in a package by url:
https://github.com/witekbobrowski/EPUBKit.git
It goes away for a little bit - and three package dependencies appear on my screen:
and when I look at my project settings, I can see them there:
(I actually added the zip package separately after the first one didn't work, just to test it, wondering whether something was wrong with the package)
Anyway - I can now see the packages, browse into them, see code or whatever - and the package found its own dependencies, so everything looks like it's working fine. But if I go into my swift app main, and add
import EPUBKit
or
import Zip
it just says "No such module ...".
I've obviously missed a step - what am I doing wrong - how do I fix it? I don't even know how to investigate it, because everything I look at just looks completely fine and I don't get any errors. I tried cleaning builds, restarting Xcode, using the real version of Xcode 13.2.1, using Xcode 13.3 beta - and nothing works.
NEW LEARNINGS
so - I tried changing how I'm running it to produce an IOS app - and it's working fine - but when I produce a Mac app, it's like the package isn't even there? Why would that be, and how do I make it work for Mac as well as IOS?
Author of EPUBKit here, thanks for taking interest in my library!
The issue is reproducible in a new project which kinda makes sense but don't at the same time. I am glad Xcode is not adding a package to every single target in my project but at the same time why does it assume that I want it in the iOS target? Anyway, I know that this question was already answered in the comment and the problem is solved but I might as well just drop more detailed instructions. Maybe it will help someone.
1. Select Project from Project Navigator
Select te target that is not working for you, in this case it's the macOS target. In the General tab you will see the Frameworks, Libraries, and Embedded Content section. If the package is missing from the list, tap the plus button at the bottom.
2. Add Missing Library
Look for the one you are missing, in this case its of course EPUBKit and tap the Add button.
3. Success
Now the library is successfully added to your target. Go ahead and build the project to run and enjoy your app :)

I am having trouble with TDD with Swift

I am trying to write a test for a new function that I want to add. However, when I run the test the project won't build for a completely unrelated reason. A normal build works fine. As you can see below, the errors are stating that the struct WeatherDataOutput is not declared. It is declared. The test class is empty. I'm not sure why I'm getting this error in Xcode. I restarted Xcode and I cleaned the project, but nothing is working for me. I would appreciate any help on this.
Thanks
After you've cleaned everything, then perform build for testing, sometimes the build succeeds and an error still shows in the source code interface which shouldn't be there. It's a bug in Xcode.
When this happens: put your cursor at the end of the offending line and hit enter, then delete (add a new line then remove it).
I've had to do this countless times in my own projects. Really, just modifying the offending line slightly then changing it back is the idea. New lines at the beginning or end of the offending line seems to be most effective in my experience.
OR
Remove the AppDataStorage source file from your test target.
If you want AppDataStorage in a separate file in your test suite, add the import WeatherDemoApp to that file. If the file also contains a test case class, use #testable import WeatherDemoApp

Xcode is marking all symbols as 'undeclared type' yet they are visible and the project compiles fine

Just to get some context here is a screenshot, though you probably gathered this from the title:
Xcode is marking all symbols as undeclared, usually this would be because it doesn't exist or isn't imported, but in this case it was working fine 20 minutes ago and weirdly the project compiles and runs with no problems.
When I compile the errors go away but there is no syntax highlighting, as soon as I start to edit the file the errors all come back. I can work if I remember the exact signatures for anything that I need to use but with the lack of code completion and errors this is very hard.
Notably this is only a problem in a few source files, its not even the whole project. Nothing has changed that I am aware of that could cause this.
Before anyone suggests it, I have tried cleaning, deleting derived data/build folder, restarting Xcode etc...
My colleague is seeing the exact same issue on his machine.
So I managed to fix the offending source files, I just compared their attributes to those of a working source file and it turns out that the problem files included the tests project in their target membership and the working file did not. I'm not sure why this is causing the issue as other projects use automated testing without a problem. Nonetheless this solved it for me, at least until I start writing test cases...

Xcode 4 - renamed/deleted file, yet get 'file not found' error

I just used Xcode's refactoring tool to rename a core data class from Player to Person. After fixing about 100 errors resulting from using dot notation that Xcode missed in the rename, I got to the last one:
Player.m - Lexical or Preprocessor issue - 'Person.h' not found
Player.m no longer exists in the project, and shows up as deleted (empty icon) in the error. Obviously, since I got rid of it, I don't want it to exist anymore!
To fix this, I have tried re-adding an old copy of Player.h and Player.m, and also cleaning. I have checked the Build Phases->Compile Sources section, and Person.m is there, Player.m is not. Person.h does in fact exist.
Not sure if it matters, but I am using mogenerator to generate my core data classes. Also, this is my third xcdatamodel version. Versions 1 and 2 used Player.h.
Here is a screenshot of the error:
Try clean build, if that doesn't work, restart Xcode, if that doesn't, delete the DerivedData directory, maybe even reboot.
Xcode sometimes gets confused.
You can try to do a super clean (not sure what the real term is) by hitting Cmd+Alt+Shift+K. This performs a clean and removes everything from the build folder.
Well I had a very similar problem and solve it removing the missing compile source of my project settings. to do that follow these steps
Click at the project page. This page contains the general settings
of your app
Select Build phases.
In Build Phases section, select compile sources.
If there is missing some class or file or something that needs to be compiled.
Select water mark file and click the minus sign at the bottom of this section.
Just click and the project may run. I hope this help others.

In Xcode What does it mean when a linked static library turns red when switching build configurations?

I think the title is pretty clear. I have ran into this twice now.
The first time it indicated a problem, and my project wouldn't compile.
The second time everything still worked.
Is it just a fluke if my code still builds when this turns red?
Anything in the files and groups list that turns red indicates a missing file or directory. This is not necessarily cause for concern, because in many instances the file is expected to be built as part of your build process.
In the case of your linked static library, you are probably depending on a second target or project to build the static library. When you changed build configurations, Xcode noticed that it had not yet been built, so it shows it as red. When you built the project and the dependent library was itself built, it turned black again.
The first time it indicated a problem, and my project wouldn't compile. The second time everything still worked.
That suggests that you don't have your build dependencies set up correctly, which will bite you every time you do a clean build. You need to make the target(s) that need the static library have an explicit build dependency on the target that builds that library.