Xcode 中的自动完成未按预期工作(使用其他 IDE)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17717028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:51:09  来源:igfitidea点击:

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

xcodeautocomplete

提问by Fréderic Cox

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.

首先让我声明我是一个关于 Objective-C 和 Xcode 的绝对新手,因为我来自 Flash Builder (IDE) / AS3 背景。我最近开始学习 Objective-C 并使用 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.

我现在正在编写我的第一个程序,并注意到 Xcode 中的自动完成功能没有像我期望的那样工作。它向我展示了似乎与我使用的 Class 类型无关的各种值。

If I declare:

如果我声明:

NSFileManager *manager;

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

那么如果我manager = [NSFileManager在这里输入 并输入 command+space 我希望defaultManager成为建议之一。并且建议应该只是NSFileManager类的方法和属性。这就是我在 Flash Builder 中习惯的方式。

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

这是 Xcode 中的错误、一些错误配置还是 Xcode 不应该工作?

Autocomplete

自动完成

回答by pjphilc

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:

我在项目中间将 Xcode 4 升级到 5,发现我在子组中创建的任何新文件(相当于项目中的子文件夹)没有自动完成,而顶部文件夹中的新文件会自动完成。我通过设置解决了这个问题:

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

Build Settings ==> Search Paths ==> Always Search User PathsYes。所有文件现在自动完成。

回答by Viesturs Knopkens

As mentioned herethere 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.

对我而言,此解决方案有所帮助: 转到首选项 > 位置 > 派生数据> 单击箭头以在 Finder 中打开。删除名为“派生数据”的文件夹。一切都恢复了。

回答by Jacobo Koenig

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 typingand de-selectingit, and then re-selectingit.

升级到新的 Xcode 版本后,我遇到了同样的问题。出于某种原因,对我有用的是在键入取消选择时转到首选项 > 文本编辑 > 建议完成,然后重新选择它。

Hope this helps.

希望这可以帮助。

回答by Rob Glassey

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

Xcode 正在尽可能地回答可用的问题。

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.

部分问题归结为 Objective-C 的“事情在运行时决定”的性质——编译器为您提供了所有可能的可能性,这通常比您预期的要广泛得多。这不仅仅是超类(通常一直到 NSObject),而是同一个命名空间中的其他东西,或者可能在同一个命名空间中。使用 Objective-C 的东西在编译时并不是一成不变的——就像许多其他语言一样,在运行时还有很多事情要做,而且编译器并不总是能够确定什么是可用的——有时它只是相信你真的知道你在做什么。这意味着您最终可能会看到多种选择。

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.

Xcode 的每个新版本都会使自动完成变得更好 - 但仍有很大的改进空间!然而,当您不记得要使用的方法的确切名称(甚至第一个字母)时,这很棘手 - 在这些情况下,我只是回到文档。

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 herefor 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.

再次查看您的问题,似乎您的系统上的自动完成功能相当糟糕 - 这超出了通常的范围。可能是 Xcode 的索引或某些东西搞砸了。看看这里的一些旧答案,并在这里寻找灵感,因为您可能正在遭受一些相关的痛苦。它不太可能仅仅由尝试工作的设置引起,因此要么某些内容已损坏,要么您的配置中的某些内容令人困惑。

回答by Mecki

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

首先有一点提示:您永远不必在同一个文件中导入FoundationCocoaCocoa是一个“元框架”(苹果称之为是一个“伞状框架”),这意味着它是对自己没有任何框架,而是进口Cocoa是一样的进口FoundationAppKit以及CoreData(它只是一个快捷方式-输入一个送三) .

Now regarding your issue: Could it be that you have not added Cocoaor Foundationto 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/Frameworksdirectory 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 Foundationreferenced, Xcode will not know the object declaration of NSFileManagerand thus also has no idea that NSFileManageractually 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?

现在关于您的问题:可能是您没有添加Cocoa或添加Foundation到您的 Xcode 项目中吗?Xcode 只会在您的源代码文件和 Xcode 项目引用的框架中搜索完成项。您可以随时导入任何框架的头文件,编译器会找到它们(因为它总是System/Library/Frameworks在所选 SDK的目录中搜索框架),但 Xcode 本身在执行代码完成时只搜索项目直接引用的框架的头文件。如果您没有Foundation引用,Xcode 将不知道 的对象声明,NSFileManager因此也不知道NSFileManager实际上是一个 Obj-C 对象或它有哪些方法。当您将框架添加到构建过程的链接阶段时,Xcode 应自动向项目添加框架引用。你真的能在没有链接器错误的情况下构建你的应用程序代码吗?

回答by Dan

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.

在 OSX 中,CMD+Space 通常通过 Finder 拉出 Spotlight 搜索,而不是自动填充 Xcode。当您尝试按 ESC 时会发生什么?ESC 是 Xcode 中默认的自动填充键。如果我使用您的代码并为我的自动填充选项点击 ESC,我得到的第一个选项是defaultManager.

I hope that helps!

我希望这有帮助!

EDIT:

编辑:

In Xcode open Preferences, click on the Text EditingTab and make sure Use Escape key to show completion suggestionsis checked.

在 Xcode 中打开Preferences,单击Text EditingTab 并确保选中Use Escape key to show completionRecommendations 。

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!

编辑 2016:请注意,此答案是为大约 Xcode 4.x 提供的。如果您想否决它,请提供一个评论,说明它不再相关,我将删除它。谢谢!

回答by Warpzit

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

在我的情况下,自动完成不起作用,因为我使用了真实的文件夹进行分组......不高兴,不高兴。

Xcode 7.3 (7D175)

Xcode 7.3 (7D175)

回答by Alessandro Ornano

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

当您的项目未正确编制索引时,可能会发生此问题。

To start, be sure you have indexing enabled:

首先,请确保您已启用索引:

Applications-> Utilities-> luanch Terminaland type:

Applications-> Utilities-> luanch Terminal并输入:

defaults write com.apple.dt.XCode IDEIndexDisable 0 

Then go the the DerivedData folder to remove files.

然后转到 DerivedData 文件夹以删除文件。

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

打开 Finder 并导航到~/Library/Developer/Xcode/DerivedData并键入:

rm -rfv *

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

之后,关闭并重新打开 Xcode,转到Product -> Clean并等待索引..

回答by Adam

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

我发现一些项目正在进行中,努力完成工作。我发现这个问题的解决方案是我的新项目在8.1,我没有下载新版本的文档。当我将项目改回 8.0 版本时,完成工作又回来了。进入首选项和下载以查看您还没有下载的下载..... 不知道为什么这不是更新的一部分

回答by Maciek Czarnik

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)

当您在同一目标/项目中包含多个相同文件时,有时自动完成会变得混乱(检查您的项目是否有重复文件,同时检查项目 > 构建阶段 > 编译源并消除重复项