1

E.g. 'once' would be instance-function specific, the @ directive would be an initializer of sorts?

-(void)mightBeCalledMoreThanOnce {
  @BOOL once = YES;

  if (once) {
    once = NO;
    NSLog(@"Hurray");
  }
}

This is very different than a static-global from C or a static-global dispatch from GCD.

seo
  • 1,959
  • 24
  • 18
  • By inspiring you of what we do for creating share instance with: ` static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{NSLog(@"Hurray");});` could do the trick. – Larme Feb 27 '14 at 17:44
  • @Larme That is globally-scoped, which works fine for singleton methods. I'm looking for a function-scoped ivar. – seo Feb 27 '14 at 17:49
  • @Alladinian Again, static is globally scoped and not instance bound. – seo Feb 27 '14 at 17:52
  • 2
    possible duplicate of [Objective-C - iVar Scoped Method Variables?](http://stackoverflow.com/questions/11998887/objective-c-ivar-scoped-method-variables) – jscs Feb 27 '14 at 20:05
  • Using `static` does _not_ create a globally-scoped variable. No variable declared in a method is globally scoped in any case. – jscs Feb 27 '14 at 20:52
  • @Josh Caswell. Good catch, my error. Meant to say something like "shared among all instances of classes or w/e else that accesses the same lexical-scope", but yeah, global-scoping is not the correct word to use. – seo Mar 01 '14 at 18:52

1 Answers1

1

No, this sort of this isn't supported directly in Obj-C. An actual instance variable (or property) on the object is how you would idiomatically accomplish this kind of scoping.

If you really want to do something like this without an ivar, look into "associated objects", which is a way of attaching arbitrary data to an instance, that you could do within the method. But that's fairly verbose and generally only used when you don't have access to the implementation of a class (ie in a category).

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204