-6
self.list = playList;

- (id)init {
    if (self = [super init]) {
        [self createDemoData];
    }
    return self;
}

static NSString *CellIdentifier = @"CellIdentifier";

Hello, I still couldn't figure out when to use these two keywords, especially static.

Michael C
  • 141
  • 1
  • 2
  • 7
  • 2
    Have you tried to Google it? I've heard it's pretty effective in that way. – Emil Jan 06 '12 at 23:36
  • 3
    You're being downvoted because you asked a very vague question about two things that are broad topics. There's no way to simply answer such an open-ended question. If you edit your question and provide further clarification of exactly what your question is, you may get better help. What is it about self and static (which aren't really related to each other at all...) that you are having trouble understanding? – Andrew Madsen Jan 06 '12 at 23:52

1 Answers1

4

Just to point you in the right direction:

Static: https://stackoverflow.com/a/1250088/267892

Issue Description:

  1. You want your ClassA to have a ClassB class variable.
  2. You are using Objective-C as programming language.
  3. Objective-C does not support class variables as C++ does.

One Alternative:

Simulate a class variable behavior using Objective-C features

  1. Declare/Define an static variable within the classA.m so it will be only accessible for the classA methods (and everything you put inside classA.m).

  2. Overwrite the NSObject initialize class method to initialize just once the static variable with an instance of ClassB.

  3. You will be wondering, why should I overwrite the NSObject initialize method. Apple documentation about this method has the answer: "The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program. (Thus the method may never be invoked if the class is not used.)".

  4. Feel free to use the static variable within any ClassA class/instance method.

Self: https://stackoverflow.com/a/2386015/267892

Using self causes your class' "setter" for this variable to be called, rather than changing the ivar directly.

self.mainViewController = aController;

is equivalent to:

[self setMainViewController:aController];

On the other hand:

mainViewController = aController;

just changes the mainViewController instance variable directly, skipping over any additional code that might be built into UIApplication's setMainViewController method, such as releasing old objects, retaining new ones, updating internal variables and so on.

Community
  • 1
  • 1
Emil
  • 7,220
  • 17
  • 76
  • 135
  • its also good to know that you can protect your instance variables if you declare them private and define your property in your .h readonly. In your implementation you define your property as read write. Even more protection is possible if you write your instance variable with an underscore _Example and synthesize it with a non underscore property @synthesize Example=_Example. One who uses your class has to go through your custom setter and your custom getter which offers a lot of protection. There are more ways for encapsulation btw. like declaring your instances in your implementation only. – markus_p Jan 07 '12 at 00:33