Domanda

I have a NSString that should be constant in my class. I used the following code to accomplish this:

@interface DefinitionViewController ()
@end

static NSString *preamble;

@implementation DefinitionViewController {

}

+(void)initialize {
  if (self == [DefinitionViewController class]) {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"preamble" ofType:@"html"];
    preamble = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding
    error:nil];
  }
}

It seems to work fine. I worry about using a file read inside an initialize. Is there a more appropriate means to accomplish the same goal (shared static string)? I could bury this inside my code, but it was much easier to maintain the somewhat large string as an external file.

Thanks for any advice.

È stato utile?

Soluzione

"I worry about using a file read inside an initialize".

Don't (worry). The fact that it is, for example, a class method is utterly irrelevant. It is code. It runs and does its job. It is sound code, it runs coherently at a coherent time, and your app bundle is a real thing that really contains the resource. There's no problem here.

If you want to postpone creation of the string, and make assurance doubly sure that the string is not initialized twice, you could instead use a singleton pattern so that the string value is not generated until the first time it is explicitly requested:

+ (NSString*) preamble {
    static NSString* preamble = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"preamble" ofType:@"html"];
        preamble = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    });
    return preamble;
}

But there is no special need for this. (EDIT: But see, to the contrary, the comment below of @bbum, who Really Knows Stuff.)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top