Fréderic Cox
Fréderic Cox

Reputation: 321

Autocomplete in Xcode not working as expected (from using other IDE's)

First off let me state that I am an absolute newbie regarding Objective-C and Xcode as I'm coming from a Flash Builder (IDE) / AS3 background. I have recently started learning Objective-C and using Xcode.

I'm now writing my first program and notice that the autocomplete function in Xcode is not working as I would expect it. It is showing me all kinds of values seemingly unrelated to the type of Class I'm using.

If I declare:

NSFileManager *manager;

then if I type manager = [NSFileManager and type command+space here I expect defaultManager to be one of the suggestions. And the suggestions should only be methods and properties of the NSFileManager class. This is how I was used to it in Flash Builder.

Is this a bug in Xcode, some misconfiguration or just not as Xcode is supposed to work?

Autocomplete

Upvotes: 18

Views: 22780

Answers (12)

Rob Glassey
Rob Glassey

Reputation: 2257

Xcode is answering the question of what is available as best as it can.

Part of the problem is down to the 'things are decided at runtime' nature of Objective-C - the compiler is giving you all of the possibilities that are possible, which is usually considerably wider than what you'd expect. This isn't just superclasses (going all the way to NSObject usually), but other things in the same namespace, or possibly in the same namespace. With Objective-C things aren't set in stone at compile time - like with many other languages, a great deal is left until run-time, and the compiler isn't always able to determine what is available - some of the time it is just trusting that you really do know what you are doing. This means you can end up being presented with a wide range of options.

Auto-complete gets better with each new version of Xcode - but there is still plenty of room for improvement! It is tricky however, when you can't remember the exact name of the method you wish to use (or even the first letter) - in these cases I just fall back to the docs.

Basically, you just need to keep typing until it narrows the possibilities down. It can also be tripped up with syntax errors in your code - methods following the error suddenly become invisible to auto-complete.

Looking again at your issue, it does seem that auto-complete is rather broken on your system - that is above and beyond the usual. It could be that Xcode's indexing or something is messed up. Have a look at some old answers here, and here for inspiration, as it may be you're suffering something related. It's unlikely to simply be caused by a setting as it is trying to work, so either something's corrupt, or something in your configuration is confusing matters.

Upvotes: 2

Hugh
Hugh

Reputation: 443

I have another explanation on why this might be happening. I recently had this problem with an older set of code. I tried deleting DerivedData like many have suggested, but it didn't help. And I verified that the property names are globally unique. I also verified that cleaning the project and restarting Xcode did not help. I tried it both on Xcode 6 and Xcode 7.

In the end, I figured out that it is probably tied to the fact that my Xcode project requires iOS 6 compatibility and the class @property variables that I needed to reference could not be declared in the .m file. By moving them to the header file, the problem was solved.

With older versions of the iOS SDK, you had to declare your @property vars in the header, but I got used to putting them in the .m file (in newer projects) to keep the header tidy. Hence, Xcode refused to recognize them with autocomplete, but it would successfully build.

Hope this helps someone. I know it's a less common scenario.

Upvotes: 0

Dan
Dan

Reputation: 5173

In OSX CMD+Space typically pulls up Spotlight Search via Finder, not auto-fill in Xcode. What happens when you try hitting ESC instead? ESC is the default auto-fill key within Xcode. If I use your code and hit ESC for my autofill options the first option I get is defaultManager.

I hope that helps!

EDIT:

In Xcode open Preferences, click on the Text Editing Tab and make sure Use Escape key to show completion suggestions is checked.

EDIT 2016: Please note this answer was provided for circa Xcode 4.x. If you want to downvote it please provide a comment that it is no longer relevant and I will remove it. Thanks!

Upvotes: 1

Jacobo Koenig
Jacobo Koenig

Reputation: 12514

After upgrading to a new Xcode version I was having the same problem. For some reason, what worked for me was going to Preferences > Text Editing > Suggest Completions while typing and de-selecting it, and then re-selecting it.

Hope this helps.

Upvotes: 3

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

This issue could happen when your project isn't properly indexed.

To start, be sure you have indexing enabled:

Applications -> Utilities -> luanch Terminal and type:

defaults write com.apple.dt.XCode IDEIndexDisable 0 

Then go the the DerivedData folder to remove files.

Open Finder and navigate to ~/Library/Developer/Xcode/DerivedData and type:

rm -rfv *

After that, close and re-open Xcode, go to Product -> Clean and wait for the indexing..

Upvotes: 2

Warpzit
Warpzit

Reputation: 28152

In my case autocomplete didn't work because I used real folders for grouping... Not pleased, not pleased.

Xcode 7.3 (7D175)

Upvotes: 1

teki
teki

Reputation: 2062

Same thing just happened, brand new project, auto complete was not working... Xcode picked the 10.10 SDK to use and then set the deployment target to 10.11. It even showed a warning in the toolbar about the problem. Setting deployment target to compiler default fixed the problem.

Upvotes: 0

Maciek Czarnik
Maciek Czarnik

Reputation: 6181

Sometimes autocompletion gots messy when you have more than one of the same file included in the same target/project (review your project for duplicate files, check also Project > Build Phases > Compile Sources and eliminate duplicates)

Upvotes: 0

Adam
Adam

Reputation: 1

I found that some of the project is was working on, hard the completions working. I found that the solution to this problem was that my new projects were in 8.1 and i hadn't downloaded the documentation for the new version. When i changed the project back to an 8.0 build, the completions came back. Go into preferences and downloads to see the downloads that you haven't got ..... not sure why that isn't part of an update

Upvotes: 0

Viesturs Knopkens
Viesturs Knopkens

Reputation: 604

As mentioned here there are many reasons and solutions.

For me helped this solution: Go to Preferences > Locations > Derived Data > click the arrow to open in Finder. Delete folder named "Derived Data". And everything works again.

Upvotes: 9

pjphilc
pjphilc

Reputation: 161

I upgraded Xcode 4 to 5 whilst in the middle of a project and found any new files I created in subgroups (equating to subfolders in the project) didn't autocomplete whilst new files in the top folder did autocomplete. I fixed this by setting:

Build Settings ==> Search Paths ==> Always Search User Paths to Yes. All files now autocomplete.

Upvotes: 16

Mecki
Mecki

Reputation: 132929

First of all a little tip: You never have to import Foundation and Cocoa within the same file. Cocoa is a "meta framework" (Apple calls that an "umbrella framework"), that means it is no framework on its own, but instead importing Cocoa is the same as importing Foundation, AppKit, and CoreData (it's just a shortcut - import one, get three).

Now regarding your issue: Could it be that you have not added Cocoa or Foundation to your Xcode project? Xcode will only search for completions in your source code files and in the frameworks that are referenced by the Xcode project. You can always import the headers of any framework, the compiler will find them (as it always searches for frameworks in the System/Library/Frameworks directory of the selected SDK), but Xcode itself only searches the headers of frameworks directly referenced by a project when performing code completion. If you don't have Foundation referenced, Xcode will not know the object declaration of NSFileManager and thus also has no idea that NSFileManager actually is an Obj-C object or which methods it has. When you add a framework to the linking phase of your build process, Xcode should automatically add a framework reference to the project. Can you actually build your App code without a linker error?

Upvotes: 1

Related Questions