Beginner's Question: Where to initialize classes?

Discussion in 'iOS Programming' started by EdgelessLemming, Jan 2, 2011.

  1. EdgelessLemming macrumors newbie

    Joined:
    Mar 20, 2010
    #1
    I have some minimal exposure to object oriented programming and am wanting to get started into some iPhone programming. I understand how and where to define class and methods (.h and .m files) but am wondering where does one typically initialize them within Xcode? I noticed there is a main.m file but most of my readings doesn't even mention this file or advises against using it.

    I've been reading over this stuff trying to get it to sink in, but I'm hoping an explanation from someone on these forums will help me out.

    Thanks
     
  2. flummoxed, Jan 2, 2011
    Last edited: Jan 2, 2011

    flummoxed macrumors member

    Joined:
    Nov 27, 2010
    #2
    Your question is a bit open-ended and vague, but here is a general answer.

    You initialize any object typically at the time of creating or instantiating it in the program. It can happen anywhere or any time depending on what the class is and what you are doing with it. You basically ignore the main file. It is just a startup file for the program. All your work is done in the class files you create - typically in the ViewController classes.

    Your .h and .m files are only a blueprint or description of the class. Just having the .h and .m files in your project does not mean the class or object actually exists in the program. It only exists when you declare it as a variable of another object you are creating or when you assign it to a pointer in your program.
    Code:
    UIView *my_view =[UIView alloc]init]
    Every time you create a new instance of the class you have to alloc init the new object. It does not have any connection to any other instance that you might have created before. Well, not every time. There are cases in which you only assign an object to a pointer but it does not get allocated, such as copying some string data.
    Code:
    NSString *string = [textField stringValue];
    Objects in your Interface Builder .xib files get initialized automatically when the .xib file is loaded.

    Sorry if I went into too much obvious detail. Your question was too general so I don't know exactly what you are unsure about or what you already know.
     

Share This Page