Best way to Define Project Wide Static Strings?

  • Hi All,

    I have a number of Constant Strings that are project wide. I started
    off defining them at the top of a .m file but as the project has
    grown over the weeks I've pasted them into each new .m file as and
    when they are needed. I'm now tidying up my code and was wondering
    the best way in which to make them project wide and how to reference
    them.

    Here is an example way I have them defined at present:

    static NSString    kString1 = @"some string";
    static NSString    kString2 = @"some other string";

    What is a good way to handle this? Is there a preferred method?

    Thanks a lot
    All the Best
    Dave
  • On 08/04/2010, at 9:23 PM, Dave wrote:

    > I have a number of Constant Strings that are project wide

    > static NSString    kString1 = @"some string";

    If they are project-wide they should not be 'static', which has the scope only of the file that they are declared in. On the other hand if that's what you want, the best (and only) place for them is the file where they're used.

    > What is a good way to handle this? Is there a preferred method?

    Constant strings have to exist somewhere, so they may as well exist in the file where they are most relevant. If you need to refer to them elsewhere, in the header for that file you can use 'extern'. Include that header wherever you need the string.

    e.g.

    // foo.h

    extern NSString* kString1;

    // foo.m

    NSString* kString1 = @"bar";

    Dunno if this is the preferred method but it's what I do. What I don't generally do is centralise all my strings in one file, on the basis that if I reuse a class in another project, any strings it uses go with it.

    --Graham
  • On 08 Apr 2010, at 15:32, Graham Cox wrote:

    >
    > On 08/04/2010, at 9:23 PM, Dave wrote:
    >
    >> I have a number of Constant Strings that are project wide
    >
    >> static NSString    kString1 = @"some string";

    that is NOT a constant string.

    NSString * const kString1 = @"lol";

    is a constant string.

    >
    >
    > If they are project-wide they should not be 'static', which has the scope only of the file that they are declared in. On the other hand if that's what you want, the best (and only) place for them is the file where they're used.
    >
    >> What is a good way to handle this? Is there a preferred method?
    >
    >
    > Constant strings have to exist somewhere, so they may as well exist in the file where they are most relevant. If you need to refer to them elsewhere, in the header for that file you can use 'extern'. Include that header wherever you need the string.
    >
    > e.g.
    >
    > // foo.h
    >
    > extern NSString* kString1;
    >
    > // foo.m
    >
    > NSString* kString1 = @"bar";

    To make your string constant and available everywhere:

    extern NSString * const kString1;

    NSString * const kString1 = @"lol";

    >
    >
    > Dunno if this is the preferred method but it's what I do. What I don't generally do is centralise all my strings in one file, on the basis that if I reuse a class in another project, any strings it uses go with it.
    >
    > --Graham
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today