Saturday, 15 February 2014

What is the best way to create constants in Objective-C -


I am creating a Reddit client for learning purposes. It needs to be filmed with a fixed file. I was thinking about importing a file in the reddit-prefix.pch file to create an available constant for all files. Is this a good way to do the job? In addition, I did my research and found several methods to create constants, but I do not know who will use:

  • #define const
  • static const
  • extern const Li>
  • enum

    How is the preferred way? What is the conference? I know that "it depends" but my question is more specifically: What are the use cases for all those solutions?

    In addition, if you are using external consultant , do I need to import the file, or import the file without stabilizing the globally Will it be available?

    One thing I can logically conclude is that Enum is the best option when defining something like a custom error domain (am I really right ?) But what about others?

    The first question is whether you want a constant, which is actually two questions:

    • Is this a specific one for a single class, or is it meant to understand them on all these applications?
    • If they are class-specific, are they for the use of class clients, or only within the class?

      If they are internal and specific to a single class, then declare them as constitute constant at the top of the .m file, such as: Fixed NSString * const MyThingNotificationKey = @ "MyThingNotificationKey";

      If they belong to one class but should be public / used by other classes, declare them as extern in the written form on top and in them Define. Me: // External Extension NSString * const MyThingNotificationKey; //.m NSString * const MyThingNotificationKey = @ "MyThingNotificationKey";

      If they should be global, declare them in a header and define them in the related module, especially for those constants.

      You can keep them in different modules, continuously with different levels, how much global you want, and for different global constants that you do not meet with each other , With each their own header, if you want. >

      Why not #define ?

      The old answer is "There is no type of information in macros", but today the composers are smart.

      The modern answer is because the debugger will not know about your macro. You can not say in the [myThing addObserver: self forKey: MyThingNotificationKey] command if a MyThingNotificationKey is a macro; The debugger can only know about it that it is a variable.

      Why not enum ?

      OK, rmaddy has defeated me in this comment: Code> Enum can only define an integer constants such as serial identifier number, bit-mask, four-byte code etc. .

      For those purposes, enum is great and you should use it at all. (Even better, use.) For other things, you should use something else; enum does nothing to integers.

      and other questions

      I was thinking about importing the file in the Reddit-Prefix.pch file to make available the constants for all files to make it work Is there a good way?

      Maybe harmless, but possibly excessive.

      What are the cases of using those solutions?

      • #defined : very limited I honestly do not know that there is a good reason to use it for constants.
      • const : Besides the best for the local constraint, you have to use it to declare it in a header and now it's defining it.
      • Static Constance : The best for file-specific (or class-specific) constants. / Li>
      • External Constance : It should be used when exporting constantly in the header.

        In addition, if you are using extern

        You need to import the file, either each file In the case where you import the file, use it or in the prefix header.

No comments:

Post a Comment