1

For a long time I have been using 1 and 3 below, and more recently 4 to define my variables.

However I have noticed that there are other valid definitions that my be new to Objective-C 2 and that would make code look nicer by keeping methods and their internal variables together.

Could someone please verify the assertions in the comments?

static id var1; // 1. Shared among all classes and their instances defined in this file

id var2; // 2. Same as 1?

@implementation MyClass
{
    id var3; // 3. Different variable for each instance
}

- (void)method
{
    static id var4; // 4.1. Not accessible from other methods
                    // 4.2. Different for each instance?
}

id var5; // 5. Same as 3? Same as 1/2?

static id var6; // 6.1. Same as 1?
                // 6.2. Not visible to other classes in this file?

@end

A couple of questions like this one are kinda similar but Objective-C may have some differences.

Community
  • 1
  • 1
Rivera
  • 10,182
  • 3
  • 49
  • 96
  • 1
    Properties are the new way. Gives you automatic getters and setters and works well with ARC. – CrimsonChris May 29 '14 at 05:17
  • I do use properties (read-only when needed) instead of 3 but for really private variables. So a property is equal to 3 right? – Rivera May 29 '14 at 07:31
  • You can make properties "public" by putting them in the @interface in the header file. – CrimsonChris May 29 '14 at 14:10
  • Sure, that's the usual place after all right. I was meaning privates variables. I guess the cleanest option is to add them to a private unnamed category and avoid 3 as possible. – Rivera May 30 '14 at 01:37

1 Answers1

4

static id var1;
=> only visible in the current translation unit

id var2;
=> globally visible (aka extern)

id var3;
=> instance variable. Created when a instance created ([Class alloc])

static id var4;
=> function local. Created when the process start. Same lifetime as var1 but only visible inside of the function.

id var5;
=> same as var2

static id var6;
=> same as var1

Objective-C is an faithful C variant (IMHO :).

Edited the unclear mention about var4.

9dan
  • 4,038
  • 2
  • 26
  • 42
  • 2
    The lifetime of `var4` is the same as `var1`, but the scope is different. You mention that but then undermine it by saying it's the same. The significant thing is that being inside `@implementation`…`@end` *outside of the braces (`{`…`}`)* doesn't affect variable scope or lifetime. Strictly speaking, neither does `@interface` which is a mistake I've seen people make. – Ken Thomases May 29 '14 at 05:39
  • So I can't have separate "4"'s by instance? One of my main interests was to avoid having a long list of "3"'s when they are only use by a single instance method. – Rivera May 29 '14 at 07:34
  • Also I still don't get the difference between 1 and 2. Does it mean 2 amy conflict with another variable called the same in another file when linking? Is that avoided by 1? – Rivera May 29 '14 at 07:37
  • @Rivera conflict in linking, yes. 2 is `extern` and `extern`s are for making variables accessible from another files(classes). For the purpose of hiding/encapsulating internal data structure, the PIMPL idiom is very useful and recommended by many people: http://stackoverflow.com/questions/843389/the-pimpl-idiom-in-practice – 9dan May 29 '14 at 07:44