.h and .m in Objective-c

Today I am looking for knowledge.

I am working on some code to work, and after the review I had full Brain Fart and can’t remember why we are doing something with .h and .m files in objective-c

  • I was wondering why we import files and declare method names in .h files? (I know this is due to processing, but cannot remember details).
  • Why do we sometimes import files directly into a .m file, rather than into .h files in some cases.
  • When you declared a method in an .h file, do you do this because the class and / or libraries you use do not perform the function in which you need them too?
  • Is there ever a case when you declared a method that does not exist in any of your libraries or in your .h file in your .m file?

Thanks to everyone, I hope everything made sense.

+6
source share
2 answers
  • It is used to separate between the public and private parts of the class. The .m file is implementation . Here, all the logic goes, all the data is processed and stored, etc. The .h file is the interface this class. This is literally like an API for your own class. He tells other classes how to use it and how to interact with it.

  • You import a class when you reference it in a file. If you refer to something (i.e. Property) in the interface, then you import it into .h. If you only reference it in the implementation, you import it into .m.

  • Any methods declared in .h exist so that other classes know that they can run it. that is, they are publicly available methods. Try deleting the ad and then calling this method. You will receive a warning.

  • No, that doesn't make sense. If you did not participate in writing your program and did not announce it for testing purposes.

+15
source
  • Because the .h file is the class public interface
  • Since import transfer to the .m file makes them confidential and saves the dependencies from the .h file (prevents excessive import of dependencies and potential rounding)
  • You do this to publicly declare that you provide this service
  • You should not, if it does not exist, you cannot use it (although you can declare what, as you hope, will exist and verify before trying to use them).

Bonus extra for 2. In your .h file, you should aim for a minimal amount of import (just a superclass and any protocols that you publicly implement) and use @class for everything else. This minimizes the effects of addiction.

+3
source

Source: https://habr.com/ru/post/949053/


All Articles