IOS starts with the main function

Source: Internet
Author: User

The entry for the app, which is also the main function

The main function is int main (int argc, char * argv[]), the C language is mostly like this, argc is the command line total number of arguments, argv is an array of parameters, it is worth mentioning that the first parameter in argv is the path of the app + full name.

And then the code in main.

@autoreleasepool {

Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));

}

Autoreleasepool automatically releases the pool, nothing to say, starting with the return of the Uiapplicationmain analysis.

Uiapplicationmain's declaration is

Uikit_extern int uiapplicationmain (int argc, char *argv[], NSString * __nullable principalclassname, NSString * __nullable Delegateclassname);

First of all, talk about Uikit_extern,

#ifdef __cplusplus

#define Uikit_extern EXTERN "C" __attribute__ ((Visibility ("default" ))

#else

#define Uikit_extern EXTERN __attribute__ ((Visibility ("default" ))

#endif

Macro definition #ifdef __cplusplus, if the macro defines __cplusplus, then the # define Uikit_extern EXTERN "C" __attribute__ ((Visibility ("default")) , or

#define Uikit_extern EXTERN __attribute__ ((Visibility ("default" ))

  Differentiate between whether the __cplusplus,__cplusplus is defined in a very intuitive translation of CPP, which is C + +. The __cplusplus identifier is used to determine whether the program was compiled with C or C + + compilers. When compiling a C + + program, this identifier is defined and is not defined when compiling the C program. Then to the whole sentence understanding, if the macro has defined the __cplusplus (that is, the current source code is treated as C + + source code. Otherwise the current source code is processed by C source code, then extern "C" __attribute__ ((Visibility ("default")).

  extern "C" very well understood, at the beginning of the C + + invention, in order to be compatible at that time in the mainstream C language, in accordance with the C compilation method to compile the role. It can be understood that the extern "C" is to tell the compiler (that is, Xcode) to compile the (global) functions and variables according to the original C language when compiling.

C + + is an "imperfect face object language", compared to C + + two ways of compiling, C + + support overloading, so that the compilation of functions must not be different from the compilation of C, a chestnut, there is a function, update student information of void updatastudentinfo (int, int); C Way to compile this function, do not special processing function name, compiled function named _updatastudentinfo, anti-C + + way of compiling, in order to support overloading, Updatastudentinfo function will become similar _updatastudentinfo The _int_int function name, and the same void updatastudentinfo (float, int) are compiled into _updatastudentinfo_float_int similar function names. These can all be viewed in the. obj file. Here, for the deep learning and use of the integrated mutual tuning of C + +, do not analyze, I am just an iOS, in the compilation after the search function name and so many can say some, but it is not familiar, it is not fraught.

Next is __attribute__ ((Visibility ("default")), and for the C-system language, __attribute__ is used to set properties, including functions, variables, and types, where we use the properties of the set function, __attribute__ Listen to rumors that is a self-test weapon, also in C, or C + +, as an entry-level ioser, understanding can be. The visibility property is to set the visibility of the function of this item as a library when it is used.

__ATTRIBUTE__ ((Visibility ("default")) is set, and the public property of the function is externally visible.

So, summed up in a sentence, Uikit_extern is to modify the function to be compatible with previous C-compile methods, with extern attributes (out-of-file visibility), public-decorated methods, or properties that are still visible outside the library of variables.

Continue parsing int uiapplicationmain (int argc, char *argv[], NSString * __nullable principalclassname, NSString * __nullable delegate ClassName);

The first two parameters for main, starting from the third NSString * __nullable principalclassname, a string type of parameter principalclassname, literal translation as the main class, Must be a uiapplication or its subclass, representing the current app itself. And if this parameter is nil, the default is @ "UIApplication".

The fourth parameter, Delegateclassname, is a proxy class. There is a delegate variable in UIApplication, delegate the life cycle of the program responsible for the Uiapplicationdelegate protocol. UIApplication receives all the system events and life cycle events, it will pass the event to uiapplicationdelegate for processing, as for why not let UIApplication himself to achieve, involving the God class, Framework class, too deep, do not speak.

In general, Uiapplicationmain is mainly responsible for three things.

1. Initializes the application object from the given class name, which is an instance of initializing the UIApplication or subclass object, and if you are given nil here, the system defaults to the UIApplication class, which is primarily the class that controls and coordinates the operation of the application. In subsequent work, you can use the static method sharedapplication to get the handle of the application.

2. Initializes an application delegate from the given application delegate class. and set the delegate to the application's delegate, here is if the passed parameter is nil, the function is called to access the Info.plist file to find the main nib file, get the application delegate.

3. Start the main event loop and start receiving events.

End

PS 1: Say __nullable and __nonnull. In Swift, you can use! and? To indicate whether an object is optional or non-optional, such as view? and view!. There is no such distinction in objective-c, and view can indicate that the object is optional or non-optioanl. When Swift and OC are mixed, the Swift compiler does not know whether a Objective-c object is optional or non-optional, So in this case the compiler implicitly treats the Objective-c object as a non-optional. To solve this problem, Apple introduced a new feature of Objective-c in Xcode 6.3: nullability annotations. At the heart of this new feature are two different types of annotations: __nullable and __nonnull. We can literally guess that __nullable indicates that an object can be null or nil, while __nonnull means that the object should not be empty. When we do not follow this rule, the compiler gives a warning.

You can use __nullable and __nonnull in any place where you can use the Const keyword, but these two keywords are limited to use on pointer types. In the declaration of the method, we can also use nullable and nonnull that are not underlined.

This can be used in attribute declarations: @property (nonatomic, copy, nonnull) NSString * name;

You can also use this: @property (nonatomic, copy) NSString * __nonnull name;

Ns_assume_nonnull_begin and Ns_assume_nonnull_end. In the code between the two macros, all simple pointer objects are assumed to be nonnull. Nullable can be specified separately in the code between two macros.

PS 2:nsstringfromclass

In normal terms,

id my = [[Nsclassfromstring (@ "Wohenshuai") alloc] init];

And

id my = [[Wohenshuai alloc] init];

is the same. However, if you do not have the Wohenshuai class in your program, the following is an error, and the above notation simply returns an empty object.

Therefore, in some cases, you can use Nsclassfromstring to initialize classes that you are unsure about.

The benefits of nsclassfromstring are:

1 weakens the connection, and therefore does not link the absence of the framework to the program.

2 Import is not required because the class is dynamically loaded and can be loaded as long as it exists. So if you do not have a header file definition for a class in your toolchain, and you are sure that the class is available, you can use this method.

The following functions are similar:

Nsclassfromstring
Nsgetsizeandalignment
NSLog
Nslogv
Nsselectorfromstring
Nsstringfromclass
Nsstringfromselector

IOS starts with the main function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.