Swift Compiler Error: Cannot find 'ClassName.h' in scope - Xcode 13
Asked Answered
P

3

8

For starters, this only became an issue after upgrading to Xcode 13. I have an app that was written in Objective-C and am utilizing some files written in Swift. In order to access classes that were created in the app in Objective-C, I do have a bridging-header named TargetName-Bridging-Header.h, where all of the classes in question are being imported like so:

#import "ClassName.h"

In the target Build Settings, Objective-C Bridging Header's value is set to the path of the bridging-header file (ProjectName/TargetName-BridgingHeader.h), and the app seems to have no problem finding it. Precompile Bridging Header is set to Yes. Install Objective-C Compatibility Header is set to Yes. Furthermore, the app builds and runs and seems to have no problem accessing those Objective-C built classes, yet the compiler continues to throw errors stating that it cannot find those classes. I've tried deleting derived data and cleaning. What am I missing? Is this perhaps a new Xcode bug that anyone else has experienced?

The exact error is "Swift Compiler Error: Cannot find 'ClassName.h' in scope"

Pathogen answered 24/9, 2021 at 18:43 Comment(1)
If it is only one Swift file, try removing it as well as removing the bridging header (store that outside of the project), and then re-add the Swift file.Nelia
M
6

Had the same problem, I removed the specific import statement from the bridging header, put it back and saved...next compilation went smooth without any errors. weird bug...but avoidable..

Millet answered 29/9, 2021 at 6:8 Comment(4)
Unfortunately, that does not work for mePathogen
It worked for me. Commented everything in the bridging header built then uncommented and build again the project ranClubhaul
commenting it out building then uncommenting and building also worked for me!Chamness
I am so happy you posted this and so irritated that it works. Thanks!Allie
H
1

It seems that Xcode caches the bridging imports. Updating the bridging header will reset the cache

Hoyle answered 3/7, 2023 at 4:44 Comment(0)
R
0

Faced similar but not the same problem. Error was raised when attempting to build for ios in github workflow.

A snippet from action steps

  - name: Build package for iOS
    run: |
      flutter build ios --release --no-codesign \

In our case it was the XCode version compatibility issue. The runner on which it was running

jobs:
  build_ios:
    runs-on: macos-latest

And at the time of writing this answer, latest was pointing to macos-12 and for which the XCode version is 14.2.0 However for our app, we needed version >= 14.3.0

So, our first option was to use https://github.com/marketplace/actions/setup-xcode-version and set the XCode explicitely using below

jobs:
  build:
    runs-on: macos-latest
    steps:
    - uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: latest-stable

But the problem here is for macos-latest which was pointed to macos-12, this set-xcode action didn't have XCOde version > 14.2.0

So eventually the solution which worked for us is to use the macos-13 runner which comes with XCode 14.3.1

jobs: build_ios: runs-on: macos-13

And no need tp use the maxim-lobanov/setup-xcode@v1

As said earlier, my problem was similar and not same, because our app is in Flutter, but behind the scene it generates code compatible for ios (in swift) and for deployment it needs XCode so the solution here is to double check the XCode compatibility for your app.

Roo answered 1/3 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.