iosobjective-cconstantsheader-filescgsize

How do I create constants of type CGSize in header file in iOS?


I want to create constants in header file of type CGSize so I can use this size anywhere in my app just using constantName.height and constantName.Width.

I would appreciate if you provide syntax for both cases: 1) fixed height and width, 2) height and width as passing value

Thank you


Solution

  • For fixed height and width

    1. #define MAXSIZE CGSizeMake(320, 480)

    For passing values, you can give the value MySizeType i.e defined as CGSize. But for Constant why do you want to pass values.

    1. typedef CGSize MySizeType;

    EDIT

    After few comments not to use Macros I am elaborating my answer over here.

    Using MACROS the drawback is that your debugger cannot know the constant.

    And also there more ways to create a constant depends on the scope of your constant you want,

    1. For internal class only

    static CGSize const MAXSIZE = {320, 480};

    1. For outside class

    In .h file

    extern CGSize const MAXSIZE;
    

    In .m file

    CGSize const MAXSIZE = {320,480};