Standard arguments in main file

2019-07-11 16:04发布

问题:

What are the standard arguments in the main file. I think I have set the wrong standard arguments in the main file, and need to change them back. I think the problem is the nil and nil in the UIApplicationMain arguments.

Here is the code I have:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

Error code:

Attaching to process 1420.
2011-09-02 14:33:04.440 HypnoTime[1420:207] *** Assertion failure in           UIApplicationMain(), /SourceCache/UIKit_Sim/UIKit-1448.89/UIApplication.m:1359
2011-09-02 14:33:04.588 HypnoTime[1420:207] *** Terminating app due to uncaught      exception 'NSInternalInconsistencyException', reason: 'Unable to instantiate the      UIApplication delegate instance. No class named AppDelegate is loaded.'
 *** Call stack at first throw:
 (
     0   CoreFoundation                      0x00dc15a9 __exceptionPreprocess + 185
     1   libobjc.A.dylib                     0x00f15313 objc_exception_throw + 44
     2   CoreFoundation                      0x00d79ef8 +[NSException      raise:format:arguments:] + 136
     3   Foundation                          0x00816341 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 101
     4   UIKit                               0x00021bcd UIApplicationMain + 962
     5   HypnoTime                           0x000027cf main + 127
     6   HypnoTime                           0x00002745 start + 53
     7   ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Current language:  auto; currently objective-c
(gdb) 

回答1:

Last argument of UIApplicationMain is the name of your UIApplicationDelegate subclass. It's required if you are creating your app programmatically, without any nib files.

int retval = UIApplicationMain(argc, argv, nil, NSStringFromClass([YourAppDelegate class]));

You need to #import "YourAppDelegate.h" for it to work that way, or you can just hardcode the name like Voloda2 suggested.

Advantage to using this way is if your change your app delegate class name, you will get a compiler warning right away, while if it's hardcoded, it will build & run without warnings, but crash.



回答2:

May be you need:

int retval = UIApplicationMain(argc, argv, nil, @"AppDelegate");


回答3:

If HypnoTime is the example from "IOS Programming: The Big Nerd Ranch Guide" then I can say that I too had odd crashing problems running in the simulator. The "nil, nil" arguments will work in main.m. I ended up removing the application entirely from Simulator, cleaning the build, then re-running and my exceptions went away. I'm using the 4.3 SDK and 4.3 simulator.

You might try doing this or completely restarting the simulator before looking to hard for coding errors.



回答4:

This is not helpful to you at all but I've had this same message in MonoTouch, and I found out I set AppDelegate.cs Build Action to Bundle Resource instead of Compile so it wasn't included in the build.



标签: ios xcode4