5

I'm working in an Objective-C project and trying to introduce Swift. I've got bridging headers working so that the code compiles, however none of the Objective-C classes are being picked up by autocomplete.

I've tried:

  1. Quitting Xcode / Restarting Computer
  2. Deleting the DerivedData folder at (~/Library/Developer/Xcode/DerivedData)
  3. Removing the file at ~/Library/Caches/com.apple.dt.Xcode
  4. Changing Simulator type

However, these suggestions didn't work for me.

Autocomplete works fine for UIKit, etc., and for my other Swift code. It's only the Objective-C code exposed by the bridging header that will not autocomplete.

Any suggestions?

5
  • Does it compile and run? Commented Nov 5, 2015 at 18:28
  • Yeah, it will compile and run fine. Commented Nov 5, 2015 at 20:12
  • Well, I just tried it on my machine and I can tell you it should work; sometimes Xcode gets funny about autocomplete, maybe restart your computer (that always fixes things, right? :) ) Commented Nov 5, 2015 at 21:27
  • I had never problems with autocomplete of ObjC, I think re-install Xcode or even OS X is the only thing you can try. Commented Nov 5, 2015 at 21:36
  • Try ctrl+Space, for non-apple keyboard and Esc for apple keyboard Commented Nov 6, 2015 at 9:51

3 Answers 3

5

I think I figured this one out:

Our project has multiple targets, and most of the files belong to multiple targets. If you want autocompletion, the header you are importing has to be imported in the bridging header for every target the file belongs to.

When I imported the header I wanted in each bridging header, autocompletion started working as expected.

Update: Seems like you can consolidate down to one bridging header if that setup works for your project. That would prevent you having to update multiple headers every time you wanted to add an import.

Sign up to request clarification or add additional context in comments.

Comments

2

Thanks to joel.d answer, I've fixed the same problem in my project.

In bridging-header I had line:

#import "BTData.h"

Please note that it was some sdk from cocoa pod, and recently we have updated all pods, so probably thats when autocompletion problems has started. Replacing above line with this one below fixed the issue and now all obj-c classes are autocompleting in swift files.

#import <Braintree/BTData.h>

Comments

1

I recently ran into this issue with a large mostly-objc project. Hopefully this helps somebody.

For me autocomplete had been working before in this project but then began to fail for all objective c classes / methods. The project still compiled with no issues though.

I ended up commenting out all of the existing imports in the bridging header and adding a simple test class, for which auto complete worked. Then uncommenting each of the other imports until I isolated which one caused the problem.

For example my header basically looked like this:

#import "MyClass.h"
#import "MyOtherClass.h"
#import "SomeThirdPartyModule.h"
etc...

I did this:

#import "SimpleTestClassWithOneMethod.h"
// #import "MyClass.h"
// #import "MyOtherClass.h"
// #import "SomeThirdPartyModule.h"

And autocomplete started working for SimpleTestClass when used from Swift.

Then started uncommenting other classes from the bridging header until it worked. The import that caused the problem was some third party framework, not sure why it caused an issue but I just pulled out what I needed from that particular header for my swift code and imported it separately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.