ios - About .h and .m files (how to manage) -


hey experienced programmers!

id ask clean code :)

id keep code clean , make nice, thats questions:

1)where should put #imports? principles: (and dont think good)

  • #import frameworks should in .h
  • #import .h files should in .m
  • .h files should have @class, not imports (excluding frameworks uikit, etc)
  • delegates should in .m

    @interface viewcontroller() <uialertviewdelegate>  @end 

2)where should put instance variables?

  • private , protected variables must in .m
  • public must in .h

3)where should put methods?

  • public in .h
  • private in .m (and yeah know "private methods" not private, hidden)

(btw thats pretty obvious)

4)what #defines?

  • global - in .h
  • used in class - in .m

5)where should put nsnotification global identifiers , how organize them

  • #define nsnotificationdatasourcedidloaddata @"nsnotificationdatasourcedidloaddata" in .h file in class send notification

but ....

  • apple has lot of private things in .h file
  • in cases .h files just.. empty :)

a year ago had situation - everything in .h, think bad also

what do? principles using? thank you!

thats questions coding style, not "how make compilable"

here's how it:

  • the usual #import <foundation/foundation.h> or #import <uikit/uikit.h> go .pch file. no header or implementation file doing these imports again (makes sharing code between ios , mac os x tiny bit easier).
  • in .h files:
    • other .h files imported if absolutely necessary, example because define types other classes (enums, structs, typedef) used in current .h file.
    • classes forward declared (@class foo;) avoid pulling in other .h files.
    • interfaces contain stuff other classes may access! no private stuff here!
  • in .m files:
    • include .h files need. not more ;-)
    • private instance variables , properties defined in class extension (@interface foo ()).
    • private methods don't need "forward declared" more, don't that.

in rare case class has stuff few classes should access "normal" users shouldn't, create foo_protected.h headers class category defines "protected" methods.

a word on #defines: avoid them if possible. use const variables or enums instead if possible, additional type information can compiler catch more mistakes way. example, using enums has nice advantage warning if you've got switch didn't handle values of enum. if add new value you'll warnings everywhere forgot handle them don't #defines.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -