开发者

Where to store fixed NSString variables in Cocoa Application?

I have different classes, that have NSString fixed variables (like @"/Users/user/media", or xml commands). I think to store everything in class is a bad design. What is the best place to store fixed NSStrings? Might something like preferences file?

P.S. I'm new in Cocoa, please describe it with details, how I can store, and re开发者_运维百科ad that values. Thanks.


They are many ways, but I like to do this in my class implementation files:

NSString *const ABConstantStringname = @"name";

Where AB is the name spacing you're using (if you are using one), and ConstantStringName is some meaningful name for the constant.

Then later, you can just use the ABNameIdentifier variable whenever you like. You don't release it.


Depends on the scope - if you want to just une in implementation you can just put in .m, but if you want to expose publically to the consumers of the class (outside your .m implementation) you need to also extern it.

In header:

#import <Cocoa/Cocoa.h>

extern NSString* const BNRTableBgColorKey;
extern NSString* const BNREmptyDocKey;

@interface PreferenceController : NSWindowController
{
    ...

In implementation:

- (id)init
{
    NSLog(@"init");

    NSString * const BNRTableBgColorKey = @"TableBackgroundColor";
    NSString * const BNREmptyDocKey = @"EmptyDocumentFlag";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜