Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

-Alloc [[]Init] vs. New

I'm not sure I'm getting this correctly, but is there a difference between a initializing a receiver with alloc/Init vs. using +(id)new


Where does the class name argument fit into the +(id)new method?


In either case, once a new instance is returned, does that mean that during runtime the instance of the object will be where the command pointer goes next? And then that instance continues to be available during run until deallocated?


What if you want to have more than one instance of the class available during runtime? Is it better just to initialize one, use it, destroy it, and initialize another, etc, in rapid succession, or better to initialize a quantity of instances, if needed during runtime (if possible)?

iMac

Posted on Jun 18, 2012 8:32 AM

Reply
Question marked as Best reply

Posted on Jun 18, 2012 8:35 AM

Create as many instances of a class that you need at the time you need them.


Depends entirely on your requirements.

49 replies

Jun 19, 2012 8:00 AM in response to msuper69

Any instance. But now that you asked, I see that the intiialization can be done wherever it's needed, which was the same discussion that we already were having.


So I guess I'm trying to find out where an application starts. Does the .main document point to the first starting meathod? Or do you code the first meathod in .main? Do you have to decalre, impliment, allocate and initialize

-(void) main { } class and function?

Jun 19, 2012 8:09 AM in response to mark133

The message "

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions"


is sent to your app when iOS has finished preparing to run your app.


What happens after that... well it's your app. That's where the fun begins!


(The main function in an Xcode project is normally left as generated by Xcode).

Jun 19, 2012 9:12 AM in response to msuper69

Well, I mean if the implementation impliments all the starting values of any methods that are initialized in the implimentation (Is that what happens?),


then all the values and all the states that are in the initialized parts of the implimentation code will be set, whether values for the graphic, or values for variables, etc, and the implimentation itslelf will then be waiting, after implementation, for the next signal from the user which will create the dominoe-switching effect throughout the implemenatation code that will cause the computer and all the declared values and memory/buffer locations to contain the new values, or change of state for the application?

Jun 19, 2012 10:03 AM in response to mark133

The code is converted by the Xcode compiler into what is called object code.

The the Xcode linker resolves all external references in your code resulting in the creation of a .app file which is what gets loaded into the Mac's RAM by the operating system when you double-click on the app's icon.

There is no squential loading.


Your program then starts running from main.

Jun 19, 2012 10:30 AM in response to msuper69

OK, thanks again for the replies. It appears that initialization itself is a rather invovled topic, where there are many important considerations to make regarding what to intialize, when and how, (and also what to allocate, when and how).


So the question about the [[* alloc] init] message that I started with on this thread is actually the beginning of the topic, rather than the end of it!

Jun 19, 2012 10:58 AM in response to mark133

It's a long story.


In the beginning (yes, that far back), people wrote procedural programs. The main() function ran, read command-line inputs, read input from the screen or files, performed some operations, and then quit.


The modern application is a completely different beast. It is much closer to real-time programming. There really is no startup and shutdown that you can directly access. There is only an event loop, which you also can't (easily) directly access. At certain times, when the runtime feels that it is ready, it may try to call some delegate methods like awakeFromNib, applicationDidFinishLaunching, etc. Your apps sits and waits for those to be called to initialize itself. Initialization can be tricky because not all parts of the runtime system are available at different points in the initialization. When the program is running, it will handle all of the user interaction and perform any actions that you have setup.


If you are learning how to program, it is much, much better to start with procedural programs run from the Terminal on the command-line. That way you can learn about how variables and classes work. A modern iOS or MacOS X application is really a pretty advanced task.

Jun 19, 2012 12:17 PM in response to etresoft

It makes sense to me that entire application runs as a continuous loop, and now also that makes it clear why intitialization is a more complicated topic than just sending one message.


I'm not having much trouble with the other aspects of development, so far, and I don't see where more ability with a more linear execution is really going to help me overcome the need to learn more about initialization. So far teh topic doesn't seem particularly friendly, but as smooth as discovering the other parts of Cocoa development have gone, I'm either over-estimating the difficulty or rightly in for a little more work at reading, studying on the topic.


I'm keeping your suggestion front and center, though, as launching a C document is clearly the best path to take when developing methods. Can I copy and paste Core Framework meathods into C documents to test them out?

Jun 19, 2012 12:55 PM in response to mark133

Just start a "Foundation" project (Application > Command Line Tool > Foundation) and it will give you a template using Objective-C and a top-level autorelease pool (or maybe some funky ARC code). You can write procedural programs that use standard I/O and most of Cocoa. If you really want to get clever, you can run the NSApplicationLoad() function instead of NSApplicationMain(). That will give you most, if not all, of Cocoa's runtime functionality that you can use in a procedural program. I wrote a command-line spell-checker using Cocoa's spell checking logic with this feature.

-Alloc [[]Init] vs. New

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.