0

So I have the following initialize method :

+(void)initialize 
{
    static BOOL init = NO 
    if init == NO
        {
            NSDictionary *dict = @{NSFontAttributeName......};
            init = YES;
        }
}

Now, I have a method say - drawer which requires dict

-(void) drawer:(CGRect)rect 
{
    [self.string drawInRect:CGRect........... withAttributes:dict];
}

When I call the dict in drawer, Xcode says - "Use of undeclared identifier dict. Why does this happen? Can someone help me out here? How do I get this method to access dict?

gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • What's the point of the `init` variable? `initialize` is only called once per class. – rmaddy Feb 25 '14 at 03:15
  • 1
    @rmaddy: I have actually burned myself with that assumption before. If your class is involved in key-value observing, `+initialize` can actually be called twice: once in your class (for example, `VSGame`), and a second time in the private KVO class (`NSKVONotifying_VSGame`). So, depending on what you're doing in that method, you may want to take those kind of precautions... – NSGod Feb 25 '14 at 18:39

1 Answers1

2

As you may know, dict in +(void)initialize is local variable of that function. Therefore, in -(void)drawer: you can't access dict since it isn't in same scope.

If you want to use dict as global variable accessible anywhere, Declare it out of the function scope.

Kyokook Hwang
  • 2,682
  • 21
  • 36