What does the '@' symbol mean in Swift?

前端 未结 2 1696
醉酒成梦
醉酒成梦 2020-12-09 17:36

I have no experience in objective C, so I\'m having trouble with some of the notation. In my \"AppDelegate.swift\" file, there is a \"@UIMainApplication\" at the top. What d

2条回答
  •  庸人自扰
    2020-12-09 18:15

    Well, you picked a rather complicated one. The @ merely means that this is an attribute - a special marker or signal to the compiler, as Apple explains here. But @UIApplicationMain is a particularly profound attribute! It substitutes for the entire UIApplicationMain implementation that lies at the heart of a C / Objective-C application, in the main.m file (as I explain here):

    int main(int argc, char *argv[])
    {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil,
                                     NSStringFromClass([AppDelegate class]));
        }
    }
    

    That is the main entry point of the app, implementing the entire launch-and-run code that is the running application. You can do something like that in Swift — a main.swift file with the Swift equivalent of that code — but Swift saves you the trouble by letting you designate your app delegate class with the @UIApplicationMain attribute.

    If you start a project as an Objective-C or Objective-C++ project, the template will give you a main file containing the main implementation, so there's no need to do anything special in this regard.

提交回复
热议问题