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.

4
  • 1
    Properties are the new way. Gives you automatic getters and setters and works well with ARC. May 29, 2014 at 5: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?
    – E. Rivera
    May 29, 2014 at 7:31
  • You can make properties "public" by putting them in the @interface in the header file. May 29, 2014 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.
    – E. Rivera
    May 30, 2014 at 1:37

1 Answer 1

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.

4
  • 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. May 29, 2014 at 5: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.
    – E. Rivera
    May 29, 2014 at 7: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?
    – E. Rivera
    May 29, 2014 at 7:37
  • @Rivera conflict in linking, yes. 2 is extern and externs 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: stackoverflow.com/questions/843389/the-pimpl-idiom-in-practice
    – 9dan
    May 29, 2014 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.