为什么自动完成在xcode中新创建的协议头文件中不起作用?

时间:2021-06-06 23:07:22

I just create a protocol header file by doing:

我只是创建一个协议头文件:

File -> New -> File -> (Cocoa Touch) Objective-C Protocol -> Next -> input protocol name -> Next -> choose my Target -> Create

文件 - >新建 - >文件 - >(Cocoa Touch)Objective-C协议 - >下一步 - >输入协议名称 - >下一步 - >选择我的目标 - >创建

when I trying to create a method list in the protocol header file. I noticed Dynamic Typing doesn't work. When I type "NSStr" in other files, usually a Class list will automatically popup and all classes are marked with colors. why it doesn't work in protocol header file?

当我尝试在协议头文件中创建方法列表时。我注意到动态打字不起作用。当我在其他文件中键入“NSStr”时,通常会自动弹出类列表,并且所有类都标有颜色。为什么它在协议头文件中不起作用?

BTW, I tried putting protocol code in existing class file, that works. why it doesn't work in separated file?

顺便说一下,我尝试将协议代码放在现有的类文件中,这有效。为什么它不能在分隔文件中工作?

Some key words will work, such as "void". the popup list will show. but other key words like "NSString" will not be recognized.

一些关键词会奏效,例如“无效”。弹出列表将显示。但其他关键词如“NSString”将无法识别。

My protocol is like this:

我的协议是这样的:

#import <Foundation/Foundation.h>

@protocol PanToSwitchViewDelegateProtocol <NSObject>

@end

2 个解决方案

#1


14  

I just experimented and I see exactly the same thing. I think the problem is that the compiler will not scan the file unless it is imported into a file that is scanned.

我只是尝试了,我看到了完全一样的东西。我认为问题是编译器不会扫描文件,除非它被导入到扫描的文件中。

If you import your protocol header into a .m file and compile it for luck, you should start getting the autocomplete on NSString.

如果您将协议标头导入.m文件并编译运气,那么您应该开始在NSString上获取自动完成功能。

EDIT

It's clear from the comments on my answer and elsewhere that jazou2012 is under a misapprehension.

从我对答案和其他地方的评论中可以清楚地看出,jazou2012处于一种误解之中。

Autocomplete has nothing to say about whether a module is correct or not. Autocomplete may fail to find an identifier for several reasons, even if that identifier has been correctly defined. For instance, I was able to replicate Jazou's problem with NSString in a protocol even though Foundation.h was imported.

自动完成无关于模块是否正确无话可说。由于多种原因,自动填充可能无法找到标识符,即使该标识符已正确定义。例如,即使导入了Foundation.h,我也可以在协议中复制Jazou的NSString问题。

However, as soon as I imported my protocol header into .m file, the autocomplete started recognising NSString. My guess is that autocomplete only starts indexing files if they are visible to the compiler.

但是,只要我将协议标头导入.m文件,自动完成就会开始识别NSString。我的猜测是,如果编译器可以看到自动完成只开始索引文件。

If you want to catch compilation errors, you need to compile the code. If the compiler says "no errors or warnings", your code is OK regardless of what autocomplete says.

如果要捕获编译错误,则需要编译代码。如果编译器说“没有错误或警告”,那么无论自动填充说明什么,您的代码都可以。

#2


1  

Either import into the class header or the implementation file or in another place where it will be visible, like another header of imports or the precompiled header. The protocols in Foundation are visible always because they're imported with Foundation. Cocoa includes Foundation so view classes will include Cocoa protocols. Core Animation and WebKit is not included by default so you need to import those to see any protocols they include.

可以导入到类标题或实现文件中,也可以导入到可见的另一个位置,例如导入的另一个标头或预编译的标头。 Foundation中的协议始终可见,因为它们是使用Foundation导入的。 Cocoa包含Foundation,因此视图类将包含Cocoa协议。默认情况下不包括Core Animation和WebKit,因此您需要导入它们以查看它们包含的任何协议。

#1


14  

I just experimented and I see exactly the same thing. I think the problem is that the compiler will not scan the file unless it is imported into a file that is scanned.

我只是尝试了,我看到了完全一样的东西。我认为问题是编译器不会扫描文件,除非它被导入到扫描的文件中。

If you import your protocol header into a .m file and compile it for luck, you should start getting the autocomplete on NSString.

如果您将协议标头导入.m文件并编译运气,那么您应该开始在NSString上获取自动完成功能。

EDIT

It's clear from the comments on my answer and elsewhere that jazou2012 is under a misapprehension.

从我对答案和其他地方的评论中可以清楚地看出,jazou2012处于一种误解之中。

Autocomplete has nothing to say about whether a module is correct or not. Autocomplete may fail to find an identifier for several reasons, even if that identifier has been correctly defined. For instance, I was able to replicate Jazou's problem with NSString in a protocol even though Foundation.h was imported.

自动完成无关于模块是否正确无话可说。由于多种原因,自动填充可能无法找到标识符,即使该标识符已正确定义。例如,即使导入了Foundation.h,我也可以在协议中复制Jazou的NSString问题。

However, as soon as I imported my protocol header into .m file, the autocomplete started recognising NSString. My guess is that autocomplete only starts indexing files if they are visible to the compiler.

但是,只要我将协议标头导入.m文件,自动完成就会开始识别NSString。我的猜测是,如果编译器可以看到自动完成只开始索引文件。

If you want to catch compilation errors, you need to compile the code. If the compiler says "no errors or warnings", your code is OK regardless of what autocomplete says.

如果要捕获编译错误,则需要编译代码。如果编译器说“没有错误或警告”,那么无论自动填充说明什么,您的代码都可以。

#2


1  

Either import into the class header or the implementation file or in another place where it will be visible, like another header of imports or the precompiled header. The protocols in Foundation are visible always because they're imported with Foundation. Cocoa includes Foundation so view classes will include Cocoa protocols. Core Animation and WebKit is not included by default so you need to import those to see any protocols they include.

可以导入到类标题或实现文件中,也可以导入到可见的另一个位置,例如导入的另一个标头或预编译的标头。 Foundation中的协议始终可见,因为它们是使用Foundation导入的。 Cocoa包含Foundation,因此视图类将包含Cocoa协议。默认情况下不包括Core Animation和WebKit,因此您需要导入它们以查看它们包含的任何协议。