Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the '@' symbol mean in Swift?

Tags:

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 does this mean?

Also, if possible, could you please give me the "equivalent" statement (if it exists) in C++?

Thanks.

like image 236
dfsdf Avatar asked May 06 '15 15:05

dfsdf


People also ask

What is the at symbol in Swift?

The @... represents an attribute, from SWIFT documentation: Attributes provide more information about a declaration or type. There are two kinds of attributes in Swift, those that apply to declarations and those that apply to types.

What does percentage symbol mean in Swift?

Note. The remainder operator ( % ) is also known as a modulo operator in other languages. However, its behavior in Swift for negative numbers means that, strictly speaking, it's a remainder rather than a modulo operation.

What does _: mean in Swift documentation?

The underscore indicates that there is no external parameter name for the function. Apple's Swift Documentation talks about this concept in terms of when you're writing your own functions.

What does hashtag mean in Swift?

Sep 21, 2015 at 16:56. In Swift 2, the hashtag (#) for second and onward parameters are kind of implicitly there to tell the external label and local name, unless you suppress the need of external label with underscore (_).


1 Answers

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.

like image 71
matt Avatar answered Sep 19 '22 22:09

matt