看到了这个,然后测试一下
原文永久地址:http://blog.it985.com/16722.html
iOS工程文件main.m其实就是一个OC一个类的.m文件,为什么我们看到这个文件的时候不能立刻知道这个文件是一个OC类的.m文件呢?
因为
1.OC类一般首字母大写
2.OC类一般都有.h文件
先说在Xcode8.0新建工程里的main.m文件
//载入UIKit框架里的UIKit.h文件,其实只为一个UIApplication.h里的UIApplicationMain C语言函数
#import <UIKit/UIKit.h>
//载入AppDelegate.h文件,其实只为一个OC方法NSStringFromClass需要的参数
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
/// 控制台输出信息
/// argc = 1
NSLog(@"argc = %d",argc);
/// argv[] count = 1
NSLog(@"argv[] count = %lu",sizeof(argv)/sizeof(argv[0]));
/// argv[0] = /Users/tt/Library/Developer/CoreSimulator/Devices/E90D0F9D-1CC5-43FE-A053-4133247FDF4F/data/Containers/Bundle/Application/BAA9D5B6-37BD-4BFD-BE14-64B102BF2A3D/demo.app/demo
NSLog(@"argv[0] = %s",argv[0]);
/// bundlePath = /Users/tt/Library/Developer/CoreSimulator/Devices/E90D0F9D-1CC5-43FE-A053-4133247FDF4F/data/Containers/Bundle/Application/BAA9D5B6-37BD-4BFD-BE14-64B102BF2A3D/demo.app
NSLog(@"bundlePath = %@",[NSBundle mainBundle].bundlePath);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
//UIApplicationMain返回一个int类型的数字,NSStringFromClass([AppDelegate class])其实这个方法返回值就是一个NSString对象@"AppDelegate",完全可以直接写成@"AppDelegate"
想深入了解argc,argv可参考以下网址
控制台信息:
2016-10-26 15:09:04.624 demo[8891:242907] argc = 1
2016-10-26 15:09:04.636 demo[8891:242907] argv[] count = 1
2016-10-26 15:09:04.637 demo[8891:242907] argv[0] = /Users/tt/Library/Developer/CoreSimulator/Devices/E90D0F9D-1CC5-43FE-A053-4133247FDF4F/data/Containers/Bundle/Application/44460D6C-8E77-459A-9805-26740F250C79/demo.app/demo
2016-10-26 15:09:04.641 demo[8891:242907] bundlePath = /Users/tt/Library/Developer/CoreSimulator/Devices/E90D0F9D-1CC5-43FE-A053-4133247FDF4F/data/Containers/Bundle/Application/44460D6C-8E77-459A-9805-26740F250C79/demo.app
超级精简版main.m(上文提到的替换)(测试可正常运行程序)
#import <UIKit/UIApplication.h>
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, @"AppDelegate");
}
}
运行成功后的效果图
网友评论