简体   繁体   English

conformsToProtocol不起作用

[英]conformsToProtocol doesn't work

Hi I have a problem with one protocol 嗨,我有一个协议有问题

  #import <Foundation/Foundation.h>

  @protocol StandardQuality <NSObject>

  - (NSString *)color;
  - (NSString *)fabric;
  -( NSString *)store;
  - ( NSString *)price;

  @end

I implement this protocol in a class 我在一堂课中实现了这个协议

  @interface Hat : NSObject <StandardQuality> 

  @property NSString *color;
  @property NSString *fabric;
  @property NSString *store;

  @end

It is happens what I expected . 这是我所期望的。 I received a warning message 我收到警告消息

method "price" in protocol not implemented. 协议中的“价格”方法未实现。

Anyway.I tested in main.h 无论如何,我在main.h中进行了测试

    [myHat setShop:@"Unirea"];
    NSLog(@" The  hat is selling at %@", myHat.store);

    if([myHat conformsToProtocol:@protocol(StandardQuality)]== YES)
    {
        NSLog(@"My hat respects the protocol");
    }
    else {
         NSLog(@"My hat doesn't respect the protocol");

    }


    if([myHat respondsToSelector:@selector(price)]== YES)
    {
        NSLog(@"My hat has price");
    }
    else {
        NSLog(@"My hat doesn't have price");

    }

Strangely, I recieved 奇怪的是,我收到了

My hat respects the protocol 我的帽子尊重协议

My hat doesn't have price 我的帽子没有价格

My second question is : It is a way to block the compiler to compile your program if a protocol is not respected , not just to receive a warning message? 我的第二个问题是:如果不遵守协议,这是一种阻止编译器编译程序的方法,而不仅仅是接收警告消息?

Strangely, I recieved 奇怪的是,我收到了

My hat respects the protocol 我的帽子尊重协议

My hat doesn't have price 我的帽子没有价格

What's strange? 有什么奇怪的 You get back exactly what you did. 您完全可以恢复工作。 Your class conforms to the protocol, but doesn't implement the price method. 您的类符合协议,但未实现price方法。

It is a way to block the compiler to compile your program if a protocol is not respected , not just to receive a warning message? 如果不遵守协议,这是阻止编译器编译程序的一种方法,而不仅仅是接收警告消息?

I assume you wanted to ask if there is a way. 我假设您想问是否有办法。 Well, you can turn on the -Werr compiler switch which makes the compiler bail out if a warning is encountered, then possibly turn off warnings only for unimplemented methods in a protocol (google for the appropriate compiler flag). 好了,您可以打开-Werr编译器开关,如果遇到警告,它将使编译器退出-Werr ,然后可能仅针对协议中未实现的方法关闭警告(谷歌提供相应的编译器标志)。

You can convert the warning for an incomplete protocol into an error by adding -Werror=incomplete-implementation to the "Other C Flags" in the "Language" section of your Xcode projects "Build Settings" (or you can pass that option to the compiler if invoking from the command line). 您可以通过在Xcode项目“构建设置”的“语言”部分的“其他C标志”中添加-Werror=incomplete-implementation ,将未完成协议的警告转换为错误(或者可以将该选项传递给编译器(如果从命令行调用)。

If you'd like to check at runtime whether a class actually implements a protocol, as opposed to simply declaring that it does, see addendum to this answer . 如果您想在运行时检查类是否真正实现了协议,而不是简单地声明已实现,请参阅此答案的附录

From the API docs : API文档

This method determines conformance solely on the basis of the formal declarations in header files, as illustrated above. 如上所述,此方法仅根据头文件中的形式声明确定一致性。 It doesn't check to see whether the methods declared in the protocol are actually implemented—that's the programmer's responsibility. 它不会检查协议中声明的方法是否真正实现,这是程序员的责任。

From the documentation your class conforms to the protocol because you are declaring it as such in it's @interface 文档中,您的类符合协议,因为您在@interface中声明了它

@interface Hat : NSObject <StandardQuality>

However, you haven't implemented the price method. 但是,您尚未实现price方法。

There is the concept of @required and @optional protocol methods. @required@optional协议方法的概念。 If you absolutely require an class to implement all or some of the methods of a protocol you should specify it like this: 如果您绝对需要一个类来实现协议的全部或某些方法,则应像这样指定它:

@protocol StandardQuality <NSObject>

@required
- (NSString *)color;
- (NSString *)fabric;
- (NSString *)store;

@optional
- (NSString *)price;

@end

In which case you should check that the object implementing a protocol actually implements the optional methods without calling them. 在这种情况下,您应该检查实现协议的对象是否确实实现了可选方法而不调用它们。

I think is more what you are looking for. 我认为您正在寻找更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM