Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iPhoneAppDev

macrumors newbie
Original poster
Sep 27, 2009
2
0
I am going to "Your First iPhone Application" tutorial on Apple's web site. Can someone explain me this line, PLEASE:

MyViewController *myViewController;

Why is this there? What does this line mean: MyViewController *myViewController; ???

Here is a full code:

Code:
#import <UIKit/UIKit.h>
 
@class MyViewController;
 
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    MyViewController *myViewController;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) MyViewController *myViewController;
 
@end

http://developer.apple.com/iphone/l...hone101/Articles/03_AddingViewController.html
 
As the previous poster said:
MyViewController *myViewController;

creates an instance of the class MyViewController - that instance is named myViewController.

Note that the class starts with a capital "M" and the instance starts with a lower-case "m". That's not required but is a convention so that you can look at your code and if a name starts with a capital letter, you know that it's a class name.

Similarly (as further information for you), the preceding line:
UIWindow *window;

creates an instance of the UIWindow class - that instance is named window.

If you don't know what an instance or a class is, you may want to get that straightened out before continuing.
 
Guys, I understand what you are saying and I thank you. But, what I don't understand is:

I don't see what the purpose of that line is? If you take a look at this link: http://developer.apple.com/iphone/l...hone101/Articles/03_AddingViewController.html you will notice that everything else was explained 100% well except that single line. Why in the God's name do we need this line? I understand it line creates an instance variable, named myViewController, of class MyViewController but, what does it create in my application? I don't get it.

@property (nonatomic, retain) MyViewController *myViewController;


What does the line "do" to "HelloWorld" application?
 
in the header files (.h files) usually the lines dont "do" anything. they are merely declaration of variables, telling the compiler how it is supposed to interpret them. so at compile time, that line tells the *compiler* something like "i want to introduce to you the variable myViewController, which is of type MyViewController, and the variable window, which is of type UIWindow". if you want to see what the program *does* with these variables, look into the corresponding .m file.
 
Think of the header file like a table of contents for a scholarly text. It kind of gives you a road map and an outline of where the text itself is going to take you and it can be helpful to switch back and forth between them.
 
Why in the God's name do we need this line?
Try this: comment out the line and rebuild and see what happens. Then you'll start to see how important it is.

Really, if you don't understand the importance of instance variables, you should spend some time learning about their usefulness. They are a key concept in the OOP world.
 
Do you know what a variable is?

Do you know what the address of a variable is?

Do you know what the name of a variable is?

Do you know what a C data structure is?

Do you know what a variable type declaration is?

Do you know what a compiler is?

Answer "no" to any of the above, and you need to go back to studying some basic concepts.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.