55

After installing the CocoaLumberjack' log compressor class I've been getting this annoying behavior: Xcode complains that there are many undeclared identifiers and gives me many errors (not warnings but errors with the red icon).

The thing is that I can compile and run my iPad app just fine but Xcode won't do any autocompletion. I tried cleaning the build folder (Product > option + Clean), and also deleting derived data. I've also rebooted to no avail.

As you can imagine this is a pain to work with. I did have this behavior happen before on a previous version of Xcode; it had something to do with stuff in my precompiled headers file but using the solution above would always fix it. I'm currently using Xcode 4.4 (4F250).

Sample error I'm getting:

Semantic Error: use of undeclared identifier 'DDTTYLogger'

The above happens even with classes that I wrote myself and that have not changed since installing the CocoaLumberjack compressor class.

Julian
  • 8,808
  • 8
  • 51
  • 90

18 Answers18

93

I finally solved this after MANY attempts using the following:

Remove the last #import from my Prefix.pch and build again. Errors would happen (obviously). Put the line back and build again. No errors would show and after 10 seconds or so, errors would come back again.

Repeat the above except instead of the last #import, remove the last TWO imports, then three, four, etc. I did this until I removed five imports and when I put them back and waited, Xcode stopped complaining.

Note that this didn't occur to me at all. I read this solution on a blog somewhere.

Weird bug...

Julian
  • 8,808
  • 8
  • 51
  • 90
  • Thanks for the help on this! It was having the same issue and starting to get really annoyed :) – Mark Armstrong Oct 26 '12 at 19:26
  • Not really sure why it is doing this but works instantly . thanks! – doozMen May 03 '13 at 08:20
  • +1. Thanks for sharing, this bug happens occasionally. Hilarious fix though. – ArkReversed May 24 '13 at 13:52
  • Wow. Terrible bug in XCode. Bizarre solution. But it works. – aroth Jan 02 '14 at 06:57
  • Still works, strange how Xcode gets in this state. Thanks for the answer – StuartM Apr 16 '14 at 20:12
  • I only have two `@import`s and one `#import` in my pch, so I just removed them all, compiled (with erros) and added them back again. Thanks! – Nicolas Miari Apr 17 '14 at 01:47
  • 5
    Holy crap. I posted this almost two years ago and people are still encountering this issue. Xcode bugfixing epic fail. – Julian Apr 17 '14 at 21:59
  • 1
    This trick (touching .pch file) even fixes the same problem in Xcode 6 beta2! Thx a million :-) – Golden Thumb Jun 18 '14 at 13:32
  • 1
    yup 2 years passed by still the bug occurs.. thanks for fix btw. – otakuProgrammer Aug 04 '14 at 11:35
  • 1
    I only had to remove and reinsert the last #import. Didn't need to go back and do them all one-by-one. – bkbeachlabs Aug 06 '14 at 17:06
  • copy then remove all imports from PCH and build. Paste back and build errors gone – Hassy Oct 28 '16 at 08:03
  • 1
    Just saved my bacon in Xcode 8. How is this even still A Thing? – Andrew Ebling Jan 07 '17 at 05:55
  • Still happening on Xcode 8.3. Dude, you're a wicked genius! :) – Ashraf Tawfeeq Aug 07 '17 at 12:10
  • 7
    STILL happening with Xcode 9.1. This is unbelievable. – Tamás Sengel Nov 09 '17 at 16:58
  • This has bugged me for a couple of years. This worked for me in Xcode 9.1 when I subsequently built, but after I ran the build on device, the errors popped back in again! – Cloov Feb 13 '18 at 12:29
  • Every time that Apple announces optimizations in the LLVM, be aware that this is what they are doing. They are completely destroying any trust that developers have in their compiler, going on and ignoring errors or the other way around: having a successfuly compiled project but still checking for errors. It's like navigating blindfold... they are killing any reliability that a developer can have in their compiler/linker just to announce that it compiles a few seconds faster. – Ramy Al Zuhouri Dec 09 '21 at 10:54
11

Open build settings and set "Precompile Prefix Header" to "No", that solved my problem.

Kudos for: https://stackoverflow.com/a/7035492/936957

Community
  • 1
  • 1
Yunus Nedim Mehel
  • 12,089
  • 4
  • 50
  • 56
  • 1
    The downside to this is that depending on how much stuff you have in the Prefix.pch file, this could mean a lot of extra compile time. In many cases a LOT of time. – Julian Jul 11 '17 at 18:22
  • 1
    In my case I changed it from "No" to "Yes" and it worked! Thanks – DJTano Feb 17 '21 at 16:34
9

I've been running into these issues constantly on all the latest versions of Xcode, in both Objective-C and Swift.

I noticed today that I was getting the errors in one particular class file. I removed it from some extra targets it was in and the errors finally went away!

I think Xcode has some fundamental bugs with it's handling of multiple targets right now. My theory is that if the other target is not built, you will essentially see errors from that target. Anyway hope this helps someone.

Ben Baron
  • 14,496
  • 12
  • 55
  • 65
  • 1
    This is the correct answer. If you add a new Swift class that you access in Objective-C code, in order for the classes to be recognized by the compiler you need to compile the target. Once you compile the -Swift.h file is autogenerated and you'll see the build succeed. However, the Xcode bug you describe causes Xcode to display errors of this type from your *other* targets on the current (successfully building) target. The solution is to switch to the other targets and build those as well. – awolf Apr 20 '17 at 20:46
4

Not bad, If you follow these Steps-

1-Clean Xcode(Cmd+Shif+K).

2- Clear Derived Data(Cmd+Shift+G).

Enter this path( ~/Library/Developer/Xcode/DerivedData/).

3- Quit and open again Xcode.

iDeveloper
  • 2,339
  • 2
  • 24
  • 38
  • 1
    Had the same issue in Xcode 10.2.1 after clearing Derived Data it worked for the first time then displayed those errors again. Strange but after restarting my Mac it got resolved. – rshankar May 01 '19 at 12:22
4

This problem can cause by setting "Target Membership" for some files are not the same.

Example:

A class XYZ put in file "a.swift" and it's used in file "b.swift". But "Target Membership" setting of "a.swift" is not the same "Target Membership" setting of b.swift.

Check "Target Membership" setting as below:

enter image description here

enter image description here

alphaplus
  • 344
  • 1
  • 3
  • 9
3

I was having issues with a library installed via cocoapods. Going to Build Settings and searching for 'Allow Non-modular Includes In Framework Modules' then setting it to Yes did the trick.

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
Adam
  • 147
  • 4
  • 11
  • This is a double edged sword solution as it solves the issue stated in the question but leads to loss of modularity of your framework, which can lead to a whole bunch of other problems. Use this solution if you know what you're doing – ErikBrandsma May 15 '19 at 08:58
3

I had it on Xcode 10.1 when I accidentally pressed:

Cmd+Shift+U - ( build for Testing )

try Clean (Cmd+Shift+K) and then:

Cmd+Shift+R ( build for Running )

Aviram Netanel
  • 12,633
  • 9
  • 45
  • 69
1

After update to Xcode 11, I met the same problem. I tried all the mentioned advices (cleaning folders, turning on/off different settings, restarting xCode), but nothing helped. Also, I have a big project in C, so, I'd like to keep using precompiled headers.

Finally I found that simple restart of Mac OS solves the problem! It's really weird behaviour, but I'm happy anyway that I found a solution – it's hard to code when lots of colourful error messages float around.

AivanF.
  • 1,134
  • 2
  • 23
  • 52
1

In my case (mixed objc/swift project) at least part of errors were caused by absence of imports for some used frameworks, e.g. "import UIKit". Project was compiled successfully because frameworks were anyway included in headers in Prefix.pch file. But errors were shown, for example about not finding method defined in UILabel extension, and yes, this extension was without "import UIKit". So I think these errors in most cases depends on Prefix.pch precompilation/updating. Touching Prefix.pch, cleaning, removing derived data, closing/opening XCode sometimes helps, but not always.

Vladimir
  • 7,670
  • 8
  • 28
  • 42
0

For me it helped cleanning the project. XCode->Product->Clean

im-a-teapot
  • 398
  • 4
  • 12
0

I got the similar type of issue.

Alternate option to fix this is Open organizer and delete the derived data of your project or delete all the projects in organizer projects tab. It works fine..

RAJA
  • 1,214
  • 10
  • 13
  • I tried deleting the derived data to no avail. Haven't tried to delete all the projects from the Organizer. It seems like overkill but then again, the solution I found doesn't make much sense either! – Julian Feb 12 '14 at 16:45
0

I just had the same thing in Xcode 5.1.

I fixed it by making sure there were no blank lines between #import's

Luke Rhodes
  • 293
  • 3
  • 13
0

I have removed some extra spaces and extra lines from .pch file and it xcode stopped complaining

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
0

This happened to me as well, but cleaning didn't fix it. What did was quitting and reopening XCode. Afterwards, all the phantom errors were gone. For those wondering, the tabs you have open when you close will still be open when you reopen.

shoe
  • 952
  • 1
  • 20
  • 44
0

I had this issue recently. It can be remedied in some cases by deleting the ModuleCache folder inside DerivedData, along with the project folder in DerivedData. Note that Xcode must be quit before doing this.

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
0

Running on Xcode Version 10.1 (10B61), I set the build setting "Increase Sharing of Precompiled Headers" to NO. I was working in an .xcworkspace with many projects sharing the same frameworks, and no Objective-C bridging header (meaning I've added no obj-c code myself). I'm not sure when Xcode did away with .pch files by default, but I didn't have any of those in my project.

Xcode Build Settings: Increase Sharing of Precompiled Headers = NO

nteissler
  • 1,513
  • 15
  • 16
0

Open up a terminal and create a nice little function accessible via the command line...

nano ~/.bashrc

add (making the necessary substitutions between the pointy braces)

cycle() {
  git stash save "BACKUP"
  git checkout <<SOME OTHER BRANCH>>
  git branch -D $1
  xcodebuild -allowProvisioningUpdates -workspace <<YOUR WORKSPACE>>.xcworkspace -scheme <<YOUR SCHEME>> -configuration Release clean
  git checkout $1
}

^X and save it by following the prompts, then enter source ~/.bashrc to make it visible to the current terminal session.

Make sure your branch is pushed to origin, cause we're going to delete it :)

Call the function using cycle <<MY BRANCH>> (once it's run you might want to call git stash pop to restore any working copy changes)

Hope it works for you! Xcode, get on your game!

Tom Howard
  • 4,672
  • 2
  • 43
  • 48
0

Using SPM, it can be that the Module you import, which you use at the location of the errors, has missing dependencies itself.

In my case I had an error about a missing initializer, XCode trying to use a different one than the one already defined in the other Module. But the project compiled fine! Nothing worked at first. Then I tried first cleaning, and then Product -> Perform Action -> Compile "myfile.swift" and it showed what the missing dependency in the dependency was! Note: This goes recursive. In multiple places a depenendency may have not been declared in the package.swift -> compile the same file repeatedly until it compiles successfully.

So.. it wasn't that the module with the error had a missing dependency, but rather that the direct dependency did miss a Module-dependency declaration in its package.swift declaration.

My explanation is that to generate the Error-output of XCode, Modules get compiled alone on their own. Import errors then show up. But when Modules are built as part of a larger Module, then that large module may import the missing dependency of the broken sub-Module already, making the build pass.

Fabian
  • 5,040
  • 2
  • 23
  • 35