Conformstoprotocol: @ protocol ()

Source: Internet
Author: User
  1. @ Protocol myprotocol
  2. -(Void) Dosomething;
  3. @ End
  4. @ Interface myclass: nsobject <myprotocol>// Directly compliant with the Protocol Class
  5. {
  6. }
  7. @ End
  8. @ Implementation myclass
  9. -(Void) Dosomething {
  10. }
  11. @ End
  12. @ Interface mytherclass: myclass// Inherits the protocol-compliant class, that is, its parent class complies with the protocol.
  13. {
  14. }
  15. @ End
  16. @ Implementation mytherclass
  17. -(Void) Dosomething {
  18. }
  19. @ End
  20. IntMain (IntArgc,Const Char* Argv [])
  21. {
  22. NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
  23. Myclass * obj_one = [myclassNew];
  24. BoolOne_conforms = [obj_one conformstoprotocol: @ protocol (myprotocol)];
  25. Mytherclass * obj_two = [mytherclassNew];
  26. // Obj_two is the Instance Object of the class and is related to the parent class. If its parent class complies with the Protocol, it also complies.
  27. BoolTwo_conforms = [obj_two conformstoprotocol: @ protocol (myprotocol)];
  28. Nslog (@"Obj_one conformstoprotocol: % d", One_conforms );// Output: Yes
  29. Nslog (@"Obj_two conformstoprotocol: % d", Two_conforms );// Output: Yes
  30. [Pool drain];Return0;
  31. }
  32. // Output:
  33. Obj_one conformstoprotocol: 1
  34. Obj_two conformstoprotocol: 1
  35. // Whereas:
  36. Mytherclass * obj_two = [mytherclassNew];
  37. // Class_conformstoprotocol only checks that the current class operator does not comply with the Protocol and has nothing to do with its parent class.
  38. BoolConforms_two = class_conformstoprotocol ([obj_twoClass], @ Protocol (myprotocol ));
  39. Nslog (@"Obj_two conformstoprotocol: % d", Conforms_two );// Output: No
  40. // Output:
  41. Obj_two conformstoprotocol: 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.